27 const char *fn =
"CommandLine::parseLong";
33 const char *attach = strchr(name,
'=');
34 if (attach ==
nullptr) {
38 opt.
assign(name, attach-name);
43 [&opt](
const Option &x) {
return !opt.
compare(x.longName); });
46 if (! o->valueName.empty()) {
47 if (attach ==
nullptr) {
85 const char *fn =
"CommandLine::parseShort";
93 for (; *name != 0; name++) {
99 [opt](
const Option &x) {
return x.shortName == opt; });
103 if (! o->valueName.empty()) {
104 if (name[1] ==
'=') {
110 fn,
"Option '%c' requires a value", opt);
123 fn,
"Option '%c' does not take a value", opt);
154 driver_ = (argc > 0 ? argv[0] :
"");
162 for (; i < argc; i++) {
163 char *arg = argv[i], *next = (i+1 < argc ? argv[i+1] :
nullptr);
166 if (!strcmp(arg,
"--") || !strcmp(arg,
"-") || arg[0] !=
'-') {
167 i += !strcmp(arg,
"--");
200 const char *
const fn =
"CommandLine::getOption";
204 if (values !=
nullptr) {
210 for (
auto x = s.first; x != s.second; x++, n++) {
218 for (
auto x = l.first; x != l.second; x++, n++) {
221 if (! names.
empty()) { names +=
'/'; }
230 "Option %s can appear at most %lu time%s (%lu present)",
231 names, nMax, nMax ==1 ?
"" :
"s", n);
254 if (opt.shortName == shortName) {
return getOption(opt, values, nMax); }
277 if (opt.longName == longName) {
return getOption(opt, values, nMax); }
310 if (opt.shortName == shortName) {
return popOption(opt); }
327 if (opt.longName == longName) {
return popOption(opt); }
343 const char *
const fn =
"CommandLine::getInteger";
346 "Option '%c' not found", shortName);
347 return strtol(vals[0].c_str(),
nullptr, 10);
363 strtol(vals[0].c_str(),
nullptr, 10) : defValue);
380 strtod(vals[0].c_str(),
nullptr) : defValue);
396 return (
getOption(shortName, &vals, 1) ? vals[0] : defValue);
412 const char *args,
bool quiet,
bool basename)
const 415 fprintf(f,
"Usage: %s [OPTIONS] %s\n\nOptions:\n",
419 for (
auto &opt: opts) {
420 bool hasShort = ( opt.shortName !=
'\0'),
421 hasLong = (!opt.longName.empty()),
422 hasValue = (!opt.valueName.empty());
423 fprintf(f,
"%s %s%c%s%s%s%s%s%s",
425 (hasShort ?
"-" :
""), opt.shortName,
426 (hasShort && hasLong ?
", " :
""), (hasLong ?
"--" :
""),
427 (hasLong ? opt.longName.c_str() :
""),
428 (hasValue ? (hasLong ?
"=" :
" ") :
""),
429 (hasValue ? opt.valueName.c_str() :
""),
430 (hasLong || hasValue ?
"\n" :
""));
434 int indent = (hasLong || hasValue ? 8 : 4);
435 const char *line = opt.helpText.c_str();
436 while (line !=
nullptr) {
437 const char *next = strchr(line,
'\n');
438 if (next ==
nullptr) {
439 fprintf(f,
"%*s%s\n", indent,
"", line);
442 fprintf(f,
"%*s%.*s\n", indent,
"", (
int )(next-line), line);
448 fprintf(f,
"%*s(File option: *SECTION = %s::%s, keyword %s)\n",
449 indent,
"", opt.scopeName.c_str(), opt.sectName.c_str(),
450 opt.keyword().c_str());
unsigned getOption(const Option &option, Arguments *values=nullptr, unsigned nMax=-1) const
Retrieves all copies of a command line option.
Command line option specification.
std::string longName
Long option name.
std::string getString(char shortName, const std::string &defValue) const
Retrieves unique string option.
#define MU_EXCEPTION_IF(cond, src, msg,...)
Conditionally throws mutils::Exception.
T find_last_of(T... args)
size_t popOption(const Option &option)
Removes and destroys all copies of a command line option.
Declares the CommandLine class.
const std::vector< Option > * validOpts_
Recognized command line options.
bool parseLong(const char *name, const char *next)
Processes a long option (possibly with a value).
static int level
Message logging level for output to stream.
bool parseShort(const char *name, const char *next)
Processes one or more short options (the last possibly with a value).
std::string::size_type npos
static constexpr int DEBUG
Verbose information.
std::string driver_
Invoked driver program name (argv[0] value).
void init(int argc, char *argv[], const std::vector< Option > *opts)
Initializes new command line input.
const std::string nullStr(1, 0)
LongMap longOpts_
Long option map.
void usage(FILE *f, const std::vector< Option > &opts, const char *args="", bool quiet=false, bool basename=true) const
Summarizes command line usage (including options).
long getInteger(char shortName) const
Retrieves unique decimal integer option.
ShortMap shortOpts_
Short option map.
char shortName
Short option name.
double getDouble(char shortName, double defValue) const
Retrieves unique floating point option.
Arguments args_
Trailing command line arguments.