21 static void printError(
const char* msg1,
const option::Option& opt,
const char* msg2)
23 fprintf(stderr,
"%s", msg1);
25 fprintf(stderr,
"%s", msg2);
30 if (msg) printError(
"Unknown option '", option,
"'\n");
39 if (msg) printError(
"Option '", option,
"' requires an argument\n");
45 if (option.
arg != 0 && option.
arg[0] != 0)
48 if (msg) printError(
"Option '", option,
"' requires a non-empty argument\n");
55 if (option.
arg != 0 && strtol(option.
arg, &endptr, 10)){};
56 if (endptr != option.
arg && *endptr == 0)
59 if (msg) printError(
"Option '", option,
"' requires a numeric argument\n");
64 enum optionIndex { UNKNOWN, HELP, OPTIONAL, REQUIRED, NUMERIC, NONEMPTY };
66 { UNKNOWN, 0,
"",
"", Arg::Unknown,
"USAGE: example_arg [options]\n\n" 68 { HELP, 0,
"",
"help",
Arg::None,
" \t--help \tPrint usage and exit." },
69 { OPTIONAL,0,
"o",
"optional",
Arg::Optional,
" -o[<arg>], \t--optional[=<arg>]" 70 " \tTakes an argument but is happy without one." },
71 { REQUIRED,0,
"r",
"required",Arg::Required,
" -r <arg>, \t--required=<arg> \tMust have an argument." },
72 { NUMERIC, 0,
"n",
"numeric", Arg::Numeric,
" -n <num>, \t--numeric=<num> \tRequires a number as argument." },
73 { NONEMPTY,0,
"1",
"nonempty",Arg::NonEmpty,
" -1 <arg>, \t--nonempty=<arg>" 74 " \tCan NOT take the empty string as argument." },
77 " example_arg --unknown -o -n10 \n" 78 " example_arg -o -n10 file1 file2 \n" 79 " example_arg -nfoo file1 file2 \n" 80 " example_arg --optional -- file1 file2 \n" 81 " example_arg --optional file1 file2 \n" 82 " example_arg --optional=file1 file2 \n" 83 " example_arg --optional= file1 file2 \n" 84 " example_arg -o file1 file2 \n" 85 " example_arg -ofile1 file2 \n" 86 " example_arg -unk file1 file2 \n" 87 " example_arg -r -- file1 \n" 88 " example_arg -r file1 \n" 89 " example_arg --required \n" 90 " example_arg --required=file1 \n" 91 " example_arg --nonempty= file1 \n" 92 " example_arg --nonempty=foo --numeric=999 --optional=bla file1 \n" 93 " example_arg -1foo \n" 94 " example_arg -1 -- \n" 95 " example_arg -1 \"\" \n" 97 { 0, 0, 0, 0, 0, 0 } };
99 int main(
int argc,
char* argv[])
101 argc-=(argc>0); argv+=(argc>0);
106 option::Option options[stats.options_max], buffer[stats.buffer_max];
121 if (options[HELP] || argc == 0)
123 int columns = getenv(
"COLUMNS")? atoi(getenv(
"COLUMNS")) : 80;
128 for (
int i = 0; i < parse.optionsCount(); ++i)
131 fprintf(stdout,
"Argument #%d is ", i);
138 fprintf(stdout,
"--optional with optional argument '%s'\n", opt.
arg);
140 fprintf(stdout,
"--optional without the optional argument\n");
143 fprintf(stdout,
"--required with argument '%s'\n", opt.
arg);
146 fprintf(stdout,
"--numeric with argument '%s'\n", opt.
arg);
149 fprintf(stdout,
"--nonempty with argument '%s'\n", opt.
arg);
158 for (
int i = 0; i < parse.nonOptionsCount(); ++i)
159 fprintf(stdout,
"Non-option argument #%d is %s\n", i, parse.nonOption(i));
Determines the minimum lengths of the buffer and options arrays used for Parser.
This is the only file required to use The Lean Mean C++ Option Parser. Just #include it and you're se...
static ArgStatus None(const Option &, bool)
For options that don't take an argument: Returns ARG_NONE.
const char * name
The name of the option as used on the command line.
A parsed option from the command line together with its argument if it has one.
Functions for checking the validity of option arguments.
Checks argument vectors for validity and parses them into data structures that are easier to work wit...
Describes an option, its help text (usage) and how it should be parsed.
int index() const
Returns Descriptor::index of this Option's Descriptor, or -1 if this Option is invalid (unused)...
The argument is acceptable for the option.
The namespace of The Lean Mean C++ Option Parser.
const char * arg
Pointer to this Option's argument (if any).
static ArgStatus Optional(const Option &option, bool)
Returns ARG_OK if the argument is attached and ARG_IGNORE otherwise.
int namelen
The length of the option name.
The argument is not acceptable and that's fatal.
void printUsage(OStream &prn, const Descriptor usage[], int width=80, int last_column_min_percent=50, int last_column_own_line_max_percent=75)
Outputs a nicely formatted usage string with support for multi-column formatting and line-wrapping...
ArgStatus
Possible results when checking if an argument is valid for a certain option.