yat
0.14.5pre
|
Class for parsing the command line. More...
#include <yat/utility/CommandLine.h>
Public Member Functions | |
CommandLine (std::string str="Available options are:") | |
default constructor More... | |
virtual | ~CommandLine (void) |
Destructor. | |
void | add (Option &) |
Function to add an option. | |
void | allow_free_args (size_t n) |
Allow at most n free arguments. More... | |
const std::vector< std::string > & | free_args (void) const |
Arguments not associated with an Option. More... | |
void | parse (int argc, char *argv[]) |
parse the commandline More... | |
bool | parsed (void) const |
has the commandline been parsed already More... | |
std::string | program_name (void) const |
void | sort (void) |
Sort Options how they will appear in (help) output. More... | |
template<class Compare > | |
void | sort (Compare compare) |
std::string | try_help (void) const |
Friends | |
std::ostream & | operator<< (std::ostream &os, const CommandLine &cl) |
Related Functions | |
(Note that these are not member functions.) | |
std::ostream & | operator<< (std::ostream &, const CommandLine &) |
CommandLine output operator. More... | |
Class for parsing the command line.
Provides parsing and storage of command line arguments. The class is typically used by hooking a number of Option objects to CommandLine, and then call the parse() function. Here is a short example how the class may be used:
After creation a number of Option classes are hooked up to the CommandLine object in their constructors.
Each parameter is associated to a one-character flag and/or a longer string flag. The longer flag expects to be preceded by '–' as e.g. '–help' for help. The shorter flag expects to be preceded by '-' as e.g. '-h', and can also be concatenated like "program -vf", which is equivalent to "program -v -f". or its sibblings for different types.
theplu::yat::utility::CommandLine::CommandLine | ( | std::string | str = "Available options are:" | ) |
default constructor
str | text preceeding the list of option in output |
void theplu::yat::utility::CommandLine::allow_free_args | ( | size_t | n | ) |
Allow at most n free arguments.
A free argument is an argument not associated with an Option, allowing commandlines such as
const std::vector<std::string>& theplu::yat::utility::CommandLine::free_args | ( | void | ) | const |
Arguments not associated with an Option.
void theplu::yat::utility::CommandLine::parse | ( | int | argc, |
char * | argv[] | ||
) |
parse the commandline
First the commandline is parsed to detect which options are present, then each Option parses the its relevant part of commandline (Option::parse()), and finally each Option is validated (Option::validate()).
throw cmd_error if an error is detected.
bool theplu::yat::utility::CommandLine::parsed | ( | void | ) | const |
has the commandline been parsed already
std::string theplu::yat::utility::CommandLine::program_name | ( | void | ) | const |
void theplu::yat::utility::CommandLine::sort | ( | void | ) |
Sort Options how they will appear in (help) output.
This function will sort the Options in alphabetical order. If the Option has a long_name, it is used for the sorting; otherwise, the short_name is used.
void theplu::yat::utility::CommandLine::sort | ( | Compare | compare | ) |
Like sort(void) but using compare to sort Options.
The functor Compare must be a Binary Predicate with both argument types const
Option*
.
std::string theplu::yat::utility::CommandLine::try_help | ( | void | ) | const |
|
related |
CommandLine output operator.
A typical output may look like this
Available options are: -h, --help display this help and exit -v, --verbose explain what is being done
The output starts with a descriptive line such as "Available options are:" (default) that can be set in constructor. Then follows the options described in the order they were added to Commandline. Each Option is described according to Option::print(void) function.