]>
Commit | Line | Data |
---|---|---|
28bca1f7 EB |
1 | /* |
2 | * Copyright (C) 1996-2018 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_COMMANDLINE_H | |
10 | #define SQUID_COMMANDLINE_H | |
11 | ||
12 | #if HAVE_GETOPT_H | |
13 | #include <getopt.h> | |
14 | #endif | |
15 | #include <vector> | |
16 | ||
17 | typedef struct option RawLongOption; | |
18 | ||
19 | /// A struct option C++ wrapper, helps with option::name copying/freeing. | |
20 | class LongOption : public RawLongOption | |
21 | { | |
22 | public: | |
23 | LongOption(); | |
24 | explicit LongOption(const RawLongOption &); | |
25 | LongOption(const LongOption&); | |
26 | LongOption &operator =(const LongOption &); | |
27 | ~LongOption(); | |
28 | ||
29 | private: | |
30 | void copy(const RawLongOption &); | |
31 | }; | |
32 | ||
33 | /// Manages arguments passed to a program (i.e., main(argc, argv) parameters). | |
34 | class CommandLine | |
35 | { | |
36 | public: | |
37 | /// expects main() input plus getopt_long(3) grammar rules for parsing argv | |
38 | CommandLine(int argc, char *argv[], const char *shortRules, const RawLongOption *longRules); | |
39 | CommandLine(const CommandLine &them); | |
40 | ~CommandLine(); | |
41 | ||
42 | CommandLine &operator =(const CommandLine &); | |
43 | ||
44 | /// \returns whether the option with optId identifier is present | |
45 | /// When returning true, sets non-nil optValue to the found option's value. | |
46 | /// For letter options (-x) and their --long synonyms, the letter is the ID. | |
47 | /// For long-only --options, the ID is the configured options::val value. | |
48 | bool hasOption(const int optId, const char **optValue = nullptr) const; | |
49 | ||
50 | /// A callback function for forEachOption(); receives parsed options. | |
51 | /// Must not call pushFrontOption(), hasOption() or forEachOption() -- getopt(3) uses globals! | |
52 | typedef void Visitor(const int optId, const char *optValue); | |
53 | ||
54 | /// calls Visitor for each of the configured command line option | |
55 | void forEachOption(Visitor) const; | |
56 | ||
57 | /// \returns argv[0], which is usually a program "name" | |
58 | const char *arg0() const { return argv_[0]; } | |
59 | ||
60 | /// \returns main()'s argc, which is traditionally missing the last/nil item | |
61 | int argc() const { return static_cast<int>(argv_.size()) - 1; } | |
62 | ||
63 | /// \returns main()'s argv[] which is traditionally const-wrong | |
64 | char **argv() const { return const_cast<char**>(argv_.data()); } | |
65 | ||
66 | /// replaces argv[0] with the new value | |
67 | void resetArg0(const char *programName); | |
68 | ||
69 | /// inserts a (possibly duplicated) option at the beginning of options (just after argv[0]) | |
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 | ||
76 | /// raw main() parameters, including argv[0] and a nil argv[argc] | |
77 | std::vector<char *> argv_; | |
78 | ||
79 | /* getopt_long() grammar rules */ | |
80 | const char *shortOptions_; ///< single-dash, single-letter (-x) option rules | |
81 | std::vector<LongOption> longOptions_; ///< long --option rules | |
82 | }; | |
83 | ||
84 | #endif /* SQUID_COMMANDLINE_H */ | |
85 |