]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/CommandLine.h
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_COMMANDLINE_H
10 #define SQUID_COMMANDLINE_H
17 typedef struct option RawLongOption
;
19 /// A struct option C++ wrapper, helps with option::name copying/freeing.
20 class LongOption
: public RawLongOption
24 explicit LongOption(const RawLongOption
&);
25 LongOption(const LongOption
&);
26 LongOption
&operator =(const LongOption
&);
30 void copy(const RawLongOption
&);
33 /// Manages arguments passed to a program (i.e., main(argc, argv) parameters).
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
);
42 CommandLine
&operator =(const CommandLine
&);
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;
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
);
54 /// calls Visitor for each of the configured command line option
55 void forEachOption(Visitor
) const;
57 /// \returns argv[0], which is usually a program "name"
58 const char *arg0() const { return argv_
[0]; }
60 /// \returns main()'s argc, which is traditionally missing the last/nil item
61 int argc() const { return static_cast<int>(argv_
.size()) - 1; }
63 /// \returns main()'s argv[] which is traditionally const-wrong
64 char **argv() const { return const_cast<char**>(argv_
.data()); }
66 /// replaces argv[0] with the new value
67 void resetArg0(const char *programName
);
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);
73 const RawLongOption
*longOptions() const { return longOptions_
.size() ? longOptions_
.data() : nullptr; }
74 bool nextOption(int &optId
) const;
76 /// raw main() parameters, including argv[0] and a nil argv[argc]
77 std::vector
<char *> argv_
;
79 /* getopt_long() grammar rules */
80 const char *shortOptions_
; ///< single-dash, single-letter (-x) option rules
81 std::vector
<LongOption
> longOptions_
; ///< long --option rules
84 #endif /* SQUID_COMMANDLINE_H */