17 opterr = allowStderrWarnings;
26 shortOptions_(shortRules ?
xstrdup(shortRules) :
""),
33 argv_.reserve(argC+1);
34 for (
int i = 0; i < argC; ++i)
36 argv_.push_back(
nullptr);
40 for (
auto longOption = longRules; longOption->name; ++longOption)
47 CommandLine(them.argc(), them.argv(), them.shortOptions_, them.longOptions())
76 if (optId == optIdToFind) {
104 if ((optId ==
':' &&
shortOptions_[0] ==
':') || optId ==
'?') {
108 "unrecognized option or missing required argument" :
"missing required argument");
132 option({
nullptr, 0,
nullptr, 0})
137 option({
nullptr, 0,
nullptr, 0})
164 name = opt.name ?
xstrdup(opt.name) :
nullptr;
165 has_arg = opt.has_arg;
static void ResetGetopt(const bool allowStderrWarnings)
struct option RawLongOption
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
Manages arguments passed to a program (i.e., main(argc, argv) parameters).
std::vector< char * > argv_
raw main() parameters, including argv[0] and a nil argv[argc]
bool hasOption(const int optId, const char **optValue=nullptr) const
CommandLine & operator=(const CommandLine &)
const RawLongOption * longOptions() const
void forEachOption(Visitor) const
calls Visitor for each of the configured command line option
std::vector< LongOption > longOptions_
long –option rules
const char * shortOptions_
single-dash, single-letter (-x) option rules
void resetArg0(const char *programName)
replaces argv[0] with the new value
CommandLine(int argc, char *argv[], const char *shortRules, const RawLongOption *longRules)
expects main() input plus getopt_long(3) grammar rules for parsing argv
void pushFrontOption(const char *name, const char *value=nullptr)
inserts a (possibly duplicated) option at the beginning of options (just after argv[0])
bool nextOption(int &optId) const
A struct option C++ wrapper, helps with option::name copying/freeing.
void copy(const RawLongOption &)
LongOption & operator=(const LongOption &)
SBuf & Printf(const char *fmt,...) PRINTF_FORMAT_ARG2