Util--  1.1
Public Member Functions | List of all members
utilmm::command_line Class Reference

#include <utilmm/configfile/commandline.hh>

Public Member Functions

 command_line (const char *options[])
 
 command_line (const std::list< std::string > &description)
 
 ~command_line ()
 
void parse (int argc, char const *const argv[], config_set &config)
 
std::list< std::string > remaining () const
 
void setBanner (std::string const &banner)
 
void usage (std::ostream &out) const
 

Detailed Description

command_line handling based on getopt_long

Description

The command_line class allows you to parse user-provided command line options and fill a Config object with them.

For each command line option, you must provide a long option (–option_text), may provide a short one (-option_character), and each option may have one (optional) argument. Eventually, you can give a help string (not used yet).

During the parsing, a key/value pair is added to a Config object for each option encountered. The value of the entry is either the option's argument or a boolean value of true if no option is specified.

Usage

The full syntax is

[!*][config_key]:long_name[,short_name][=value_type[,default]|?value_type,default][:help]

where value_type is one of: int, bool, string

When an option is found, an entry is added to a Config object with the config_key key. The value associated is:

If default is given, the option is set to default if it is not found

If the option has a mandatory argument, add =value_type after the option names. If it is optional, use the ?value_type syntax. The 'int' and 'bool' value types are checked by the command_line object and an error is generated if the user-provided value dos not match.

Multiplicity (* and ! options)

When the same option is provided more than once on the command line, the normal behaviour is to use the value of the latest. However, you can also get all the values by adding * at the front of the description line. In that case, the config value will a list of values in the config object

For instance, an option like the -I option of gcc will be described using
*:include,I=string|include path. The result of gcc -I /a/path -I=/another/path can then be retrieved with list<string> includes = config.get< list<string> >('include');

If the ! flag is set, the option is required.

Examples

The classical –help option will be given using :help|display this help and exit

The -r and –recursive options of grep are described using :recursive,r|equivalent to –directories=recurse

The -m and –max-count options of grep are described using :max-count,m=int|stop after NUM matches

Constructor & Destructor Documentation

◆ command_line() [1/2]

utilmm::command_line::command_line ( const char *  options[])

Builds an object with a null-terminated string list

Parameters
optionsthe option list, null-terminated

◆ command_line() [2/2]

utilmm::command_line::command_line ( const std::list< std::string > &  description)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ ~command_line()

utilmm::command_line::~command_line ( )

Member Function Documentation

◆ parse()

void utilmm::command_line::parse ( int  argc,
char const *const  argv[],
config_set config 
)

Parses the command line option

Parameters
argcthe argument count
argvthe argument value
configthe Config object the option values will be written to
Exceptions
commandline_error

◆ remaining()

std::list<std::string> utilmm::command_line::remaining ( ) const

Remaining command line options After all options are matched, and if no error has occured, this function will return all non-option arguments (input files for instance)

◆ setBanner()

void utilmm::command_line::setBanner ( std::string const &  banner)

Sets the first line to appear in usage()

◆ usage()

void utilmm::command_line::usage ( std::ostream &  out) const

Outputs a help message to out

Referenced by utilmm::operator<<().


The documentation for this class was generated from the following file:

Generated on Wed Oct 17 2018 09:34:19 for Util-- by doxygen 1.8.13
SourceForge.net Project Page