template<class ConsoleT = Console>
class evo::CommandLineT< ConsoleT >
Process command-line arguments.
- Use
add*()
methods to add support for options/arguments/commands:
- addflag() adds a flag option
- addopt() adds an option with value, returns an Option object to further customize this option
- addsep() adds an option separator in usage help, a blank line before the next option
- addarg() adds an argument, returns an Option object to further customize this argument
- addcmd() adds a sub-command, returns a Command object to further customize this command
- This handles
-h
or --help
flags automatically to show well formatted usage help and exit
- If addver() is called, this also handles the
--version
flag automatically
- Usage help shows options in the same order they're added
- By default this will exit (terminate) on error or on help/version output, unless set_noexit() is called
- Use parse() to parse and process a command-line and populate a command-line map
- This uses Console by default for output, which is not thread safe – thread safety isn't normally needed here anyway
- Template Parameters
-
ConsoleT | Console type to use (used for testing, leave default) |
- Example
Here's a simple example:
const char* HELP = "Test program description for command-line argument parsing. Long lines will wrap automatically as needed.\n\
\nBullet list of things:\n\
- First thing\n\
- Second thing";
const char* EPILOG = "This is the epilog, which gives additional description and is shown last.";
int main(int argc, const char* argv[]) {
cmdline.set_epilog(EPILOG);
cmdline.addver("Program version 1.0");
cmdline.addflag("-f, --flag", "Flag option");
cmdline.addopt("-o, --opt", "Option with a value");
cmdline.addarg("file", "File to use").required().multi();
cmdline.parse(args, argc, argv);
c.
out <<
"Results:" <<
NL;
c.
out <<
' ' << iter->key() <<
'=' << iter->value() <<
NL;
c.
out <<
"Flag is set" <<
NL;
if (!value.null())
c.
out <<
"Opt: " << value <<
NL;
while (tok.next(';'))
c.
out <<
"File: " << tok.value() <<
NL;
return 0;
}
Output with: ./program -h
Usage: program [options] file...
Test program description for command-line argument parsing. Long lines will wrap automatically as
needed.
Bullet list of things:
- First thing
- Second thing
Options:
-f, --flag Flag option
-o, --opt <value> Option with a value
-h, --help Show this usage help
--help-general Show general argument processing help
--version Show version information
Arguments:
file File to use
This is the epilog, which gives additional description and is shown last.
Output with: ./program -f -o=val file1 file2
Results:
file=file1;file2
flag=1
opt=val
Flag is set
Opt: val
File: file1
File: file2
|
| CommandLineT () |
| Default constructor. More...
|
|
| CommandLineT (const String &description) |
| Constructor with help description. More...
|
|
Option & | add (const String &str) |
| Parse input string and add an global option or argument as described. More...
|
|
Option & | addarg (const String &key, const String &helpname, const String &help) |
| Add a positional argument. More...
|
|
Option & | addarg (const String &key, const String &help) |
| Add a positional argument. More...
|
|
CommandInfo & | addcmd (const String &name, const String &help) |
| Add a sub-command argument with it's own options and arguments. More...
|
|
void | addflag (const String &names, const String &key, const String &help) |
| Add a global option flag. More...
|
|
void | addflag (const String &names, const String &help) |
| Add a global option flag. More...
|
|
Option & | addopt (const String &names, const String &key, const String &helpname, const String &help) |
| Add a global option that stores a value. More...
|
|
Option & | addopt (const String &names, const String &helpname, const String &help) |
| Add a global option that stores a value. More...
|
|
Option & | addopt (const String &names, const String &help) |
| Add a global option that stores a value. More...
|
|
void | addsep () |
| Add a global options separator in usage help. More...
|
|
This & | addver (const String &version_info) |
| Add version info and enable option flags to show it. More...
|
|
bool | error () const |
| Check whether an error occurred while parsing arguments. More...
|
|
ConsoleT & | get_con () |
| Get reference to console object used. More...
|
|
const NewlineValue & | get_newline () const |
| Get current newline value used for output. More...
|
|
template<class TMap > |
bool | parse (TMap &map, int argc, const char *argv[], int offset=0) |
| Parse and process command-line using current option and argument info. More...
|
|
This & | set_epilog (const String &text) |
| Set epilog text shown in usage help. More...
|
|
This & | set_maxline (uint maxline) |
| Set new max line length for usage help output. More...
|
|
This & | set_newline (const NewlineValue &nl) |
| Set current newline value used for output. More...
|
|
This & | set_noexit (bool val=true) |
| Set no-exit flag to prevent parse() from terminating and instead have it return false. More...
|
|
This & | set_progname (const String &name) |
| Set program name to use with usage help. More...
|
|
OutT & | show_error () |
| This writes the beginning (prefix) of an error message and returns a stream for the caller to write the error message to. More...
|
|
OutT & | show_warning () |
| This writes the beginning (prefix) of a warning message and returns a stream for the caller to write the warning message to. More...
|
|