Demonstrates handling various types of option arguments (required, numeric,...) with no dependency on the C++ standard library (only C lib).
#include <stdio.h>
#include <stdlib.h>
{
static void printError(
const char* msg1,
const option::Option& opt,
const char* msg2)
{
fprintf(stderr, "%s", msg1);
fprintf(stderr, "%s", msg2);
}
{
if (msg) printError("Unknown option '", option, "'\n");
}
{
if (msg) printError("Option '", option, "' requires an argument\n");
}
{
if (option.
arg != 0 && option.
arg[0] != 0)
if (msg) printError("Option '", option, "' requires a non-empty argument\n");
}
{
char* endptr = 0;
if (option.
arg != 0 && strtol(option.
arg, &endptr, 10)){};
if (endptr != option.
arg && *endptr == 0)
if (msg) printError("Option '", option, "' requires a numeric argument\n");
}
};
enum optionIndex { UNKNOWN, HELP, OPTIONAL, REQUIRED, NUMERIC, NONEMPTY };
{ UNKNOWN, 0,"", "", Arg::Unknown, "USAGE: example_arg [options]\n\n"
"Options:" },
{ HELP, 0,
"",
"help",
Arg::None,
" \t--help \tPrint usage and exit." },
{ OPTIONAL,0,
"o",
"optional",
Arg::Optional,
" -o[<arg>], \t--optional[=<arg>]" " \tTakes an argument but is happy without one." },
{ REQUIRED,0,"r","required",Arg::Required," -r <arg>, \t--required=<arg> \tMust have an argument." },
{ NUMERIC, 0,"n","numeric", Arg::Numeric, " -n <num>, \t--numeric=<num> \tRequires a number as argument." },
{ NONEMPTY,0,"1","nonempty",Arg::NonEmpty," -1 <arg>, \t--nonempty=<arg>"
" \tCan NOT take the empty string as argument." },
"\nExamples:\n"
" example_arg --unknown -o -n10 \n"
" example_arg -o -n10 file1 file2 \n"
" example_arg -nfoo file1 file2 \n"
" example_arg --optional -- file1 file2 \n"
" example_arg --optional file1 file2 \n"
" example_arg --optional=file1 file2 \n"
" example_arg --optional= file1 file2 \n"
" example_arg -o file1 file2 \n"
" example_arg -ofile1 file2 \n"
" example_arg -unk file1 file2 \n"
" example_arg -r -- file1 \n"
" example_arg -r file1 \n"
" example_arg --required \n"
" example_arg --required=file1 \n"
" example_arg --nonempty= file1 \n"
" example_arg --nonempty=foo --numeric=999 --optional=bla file1 \n"
" example_arg -1foo \n"
" example_arg -1 -- \n"
" example_arg -1 \"\" \n"
},
{ 0, 0, 0, 0, 0, 0 } };
int main(int argc, char* argv[])
{
argc-=(argc>0); argv+=(argc>0);
#ifdef __GNUC__
#else
#endif
if (parse.error())
return 1;
if (options[HELP] || argc == 0)
{
int columns = getenv("COLUMNS")? atoi(getenv("COLUMNS")) : 80;
return 0;
}
for (int i = 0; i < parse.optionsCount(); ++i)
{
fprintf(stdout, "Argument #%d is ", i);
{
case HELP:
case OPTIONAL:
fprintf(stdout,
"--optional with optional argument '%s'\n", opt.
arg);
else
fprintf(stdout, "--optional without the optional argument\n");
break;
case REQUIRED:
fprintf(stdout,
"--required with argument '%s'\n", opt.
arg);
break;
case NUMERIC:
fprintf(stdout,
"--numeric with argument '%s'\n", opt.
arg);
break;
case NONEMPTY:
fprintf(stdout,
"--nonempty with argument '%s'\n", opt.
arg);
break;
case UNKNOWN:
break;
}
}
for (int i = 0; i < parse.nonOptionsCount(); ++i)
fprintf(stdout, "Non-option argument #%d is %s\n", i, parse.nonOption(i));
}