Helper class to parse command line arguments. More...
#include <command_line.h>
Public Member Functions | |
Construction | |
CL_CommandLine () | |
~CL_CommandLine () | |
Operations | |
void | set_help_indent (int i) |
Change the indention used for the help message, default is 18. | |
void | add_usage (const CL_String8 &usage) |
Add a usage line to the help output, usage is for example. | |
void | add_doc (const CL_String8 &doc) |
Adds extra documentation to the help output, should only be. | |
void | add_group (const CL_String8 &grouptopic) |
Starts a new group of options, the grouptopic gets printed. | |
void | add_option (int key, const CL_String8 &long_option, const CL_String8 &argument, const CL_String8 &help, bool visible=true) |
Adds a option to the parser. | |
void | parse_args (int argc, char **argv) |
Parse the options arcording to the options added with. | |
void | print_help () |
Print the help output, normaly done via a --help option. | |
bool | next () |
Goto the next option. | |
Attributes | |
| |
enum | { REST_ARG = -2 } |
int | get_key () |
Return the key of the current option. | |
CL_String8 | get_argument () |
Return the argument to the current option. |
Helper class to parse command line arguments.
The CL_CommandLine class helps to parse command line arguments, namely the argc/argv pair that you get from main(). CL_CommandLine mimics getopt_long() behaviour as closely as possible, while providing a cleaner interface and a few additional features, like automatic generation of '--help' output. CL_CommandLine can parse long arguments in the following styles:
program --file FILENAME
program --file=FILENAME
Short arguments are handled like this:
program -f FILENAME
Concatenating short arguments is also supported, so that:
program -f -a -b FILENAME
is equivalent to:
program -fab FILENAME
Non-option arguments (aka rest arguments) are supported as well:
program SOMEFILE SOMEOTHERFILE ...
To avoid ambiguity when a filename starts with '-' CL_CommandLine stops parsing arguments after the first encounter of a '--', so in
program -f -b -- -f -b
In the above example the first '-f -b' options are treated as normal while the ones after the '--' are treated as rest arguments (aka filenames in most programs).
CL_CommandLine::CL_CommandLine | ( | ) |
CL_CommandLine::~CL_CommandLine | ( | ) |
void CL_CommandLine::add_doc | ( | const CL_String8 & | doc | ) |
Adds extra documentation to the help output, should only be.
used at the beginning or at the end, to
void CL_CommandLine::add_group | ( | const CL_String8 & | grouptopic | ) |
Starts a new group of options, the grouptopic gets printed.
above the group of options in the print_help() output
void CL_CommandLine::add_option | ( | int | key, | |
const CL_String8 & | long_option, | |||
const CL_String8 & | argument, | |||
const CL_String8 & | help, | |||
bool | visible = true | |||
) |
Adds a option to the parser.
key | a letter for a short-option or a numeric value | |
key | larger than 255 that identifies the option | |
long_option | the long name of this option or "" if non | |
long_option | should be used for this option | |
argument | the type of the argument that this option | |
argument | requires (i.e. FILE, SIZE, WIDTH, etc.) or "" if no argument is | |
argument | required | |
help | the help string for this option | |
visible | true if the option should be listed in the help | |
visible | output, false will not list it in the help output which might | |
visible | be usefull for cheat-options or backward-compability options | |
visible | than would only clutter the help output |
void CL_CommandLine::add_usage | ( | const CL_String8 & | usage | ) |
Add a usage line to the help output, usage is for example.
"[OPTIONS]... FILE", the program name will get appended automatically
CL_String8 CL_CommandLine::get_argument | ( | ) |
Return the argument to the current option.
int CL_CommandLine::get_key | ( | ) |
Return the key of the current option.
bool CL_CommandLine::next | ( | ) |
Goto the next option.
void CL_CommandLine::parse_args | ( | int | argc, | |
char ** | argv | |||
) |
Parse the options arcording to the options added with.
add_option(), result of the parsing is accessible via next() and get_key()/get_argument()
void CL_CommandLine::print_help | ( | ) |
Print the help output, normaly done via a --help option.
void CL_CommandLine::set_help_indent | ( | int | i | ) |
Change the indention used for the help message, default is 18.