CommandLine.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_COMMANDLINE_H
10 #define SQUID_SRC_COMMANDLINE_H
11 
12 #if HAVE_GETOPT_H
13 #include <getopt.h>
14 #endif
15 #include <vector>
16 
17 typedef struct option RawLongOption;
18 
20 class LongOption : public RawLongOption
21 {
22 public:
23  LongOption();
24  explicit LongOption(const RawLongOption &);
25  LongOption(const LongOption&);
27  ~LongOption();
28 
29 private:
30  void copy(const RawLongOption &);
31 };
32 
35 {
36 public:
38  CommandLine(int argc, char *argv[], const char *shortRules, const RawLongOption *longRules);
39  CommandLine(const CommandLine &them);
40  ~CommandLine();
41 
43 
48  bool hasOption(const int optId, const char **optValue = nullptr) const;
49 
52  typedef void Visitor(const int optId, const char *optValue);
53 
55  void forEachOption(Visitor) const;
56 
58  const char *arg0() const { return argv_[0]; }
59 
61  int argc() const { return static_cast<int>(argv_.size()) - 1; }
62 
64  char **argv() const { return const_cast<char**>(argv_.data()); }
65 
67  void resetArg0(const char *programName);
68 
70  void pushFrontOption(const char *name, const char *value = nullptr);
71 
72 private:
73  const RawLongOption *longOptions() const { return longOptions_.size() ? longOptions_.data() : nullptr; }
74  bool nextOption(int &optId) const;
75 
77  std::vector<char *> argv_;
78 
79  /* getopt_long() grammar rules */
80  const char *shortOptions_;
81  std::vector<LongOption> longOptions_;
82 };
83 
84 #endif /* SQUID_SRC_COMMANDLINE_H */
85 
void Visitor(const int optId, const char *optValue)
Definition: CommandLine.h:52
bool hasOption(const int optId, const char **optValue=nullptr) const
Definition: CommandLine.cc:71
void forEachOption(Visitor) const
calls Visitor for each of the configured command line option
Definition: CommandLine.cc:89
const char * shortOptions_
single-dash, single-letter (-x) option rules
Definition: CommandLine.h:80
void pushFrontOption(const char *name, const char *value=nullptr)
inserts a (possibly duplicated) option at the beginning of options (just after argv[0])
Definition: CommandLine.cc:123
void resetArg0(const char *programName)
replaces argv[0] with the new value
Definition: CommandLine.cc:115
Manages arguments passed to a program (i.e., main(argc, argv) parameters).
Definition: CommandLine.h:34
std::vector< char * > argv_
raw main() parameters, including argv[0] and a nil argv[argc]
Definition: CommandLine.h:77
A struct option C++ wrapper, helps with option::name copying/freeing.
Definition: CommandLine.h:20
CommandLine & operator=(const CommandLine &)
Definition: CommandLine.cc:52
const RawLongOption * longOptions() const
Definition: CommandLine.h:73
void copy(const RawLongOption &)
Definition: CommandLine.cc:161
bool nextOption(int &optId) const
Definition: CommandLine.cc:101
LongOption & operator=(const LongOption &)
Definition: CommandLine.cc:153
const char * arg0() const
Definition: CommandLine.h:58
CommandLine(int argc, char *argv[], const char *shortRules, const RawLongOption *longRules)
expects main() input plus getopt_long(3) grammar rules for parsing argv
Definition: CommandLine.cc:24
int argc() const
Definition: CommandLine.h:61
char ** argv() const
Definition: CommandLine.h:64
std::vector< LongOption > longOptions_
long –option rules
Definition: CommandLine.h:81

 

Introduction

Documentation

Support

Miscellaneous