Skip to content

Commandline

#include <tpau-cpp-kernal/Commandline.h>

INCLUDE FILE
#include <tpau-cpp-kernal/Commandline.h>

class Commandline

A command line parser.

It parses command line options and arguments according to the given options and arguments specification. It also provides usage and version information.

DEFINITION
class Commandline;

class Option

A command line option.

DEFINITION
class Option;

constructor Option

Create a command line option with a long variant and an argument.

DEFINITION
Option ( std::string  name_,
std::string  argument_name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
argument_name_
description_

constructor Option

Create a command line option with a long variant and no argument.

DEFINITION
Option ( std::string  name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
description_

constructor Option

Create a command line option with both short and long variants and an argument.

DEFINITION
Option ( std::string  name_,
char  short_name_,
std::string  argument_name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
short_name_
argument_name_
description_

constructor Option

Create a command line option with both short and long variants and no argument.

DEFINITION
Option ( std::string  name_,
char  short_name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
short_name_
description_

method has_argumentconst

Check if the option has an argument.

DEFINITION
bool  has_argument ( ) const;
RETURNSDESCRIPTION
bool

true if the option has an argument, false otherwise.

method operator<const

DEFINITION
bool  operator< ( const Option & other ) const;
PARAMETERDESCRIPTION
other
RETURNSDESCRIPTION
bool

member argument_name

The name of the argument for the option. This is used in the usage message. Empty if the option has no argument.

DEFINITION
std::string  argument_name;

member description

The description of the option. This is used in the usage message.

DEFINITION
std::string  description;

member name

The long name of the option. This is specified on the command line by prefixing it with ` (e.g. "help" forhelp`).

DEFINITION
std::string  name;

member short_name

The short name of the option. This is specified on the command line by prefixing it with - (e.g. 'h' for -h). Empty if the option has no short name.

DEFINITION
std::optional< char >  short_name;

constructor Commandline

Create a command line parser.

DEFINITION
Commandline ( std::vector< Option >  options,
std::string  arguments,
std::string  header,
std::string  footer,
std::string  version );
PARAMETERDESCRIPTION
options

The list of options supported by the command line parser.

arguments

The description of the positional arguments. This is used in the usage message.

header

The header of the usage message.

footer

The footer of the usage message.

version

The version of the program.

method add_option

Add an option to the command line parser.

DEFINITION
void  add_option ( Option  option );
PARAMETERDESCRIPTION
option

The option to add.

method parse

Parse the command line arguments and options.

The command line arguments are expected as a list of C strings with argv[0] being the program name. This is the same format as the arguments passed to the main() function.

DEFINITION
ParsedCommandline  parse ( int  argc,
char *const  argv,
bool  allow_exit = true );
PARAMETERDESCRIPTION
argc

The number of command line arguments.

argv

The command line arguments.

allow_exit

If true, the program will exit if the command should not run. Otherwise, exit_code will be set in the returned ParsedCommandline.

RETURNSDESCRIPTION
ParsedCommandline

The parsed command line.

method usage

Print the usage message to the given FILE.

DEFINITION
void  usage ( bool  full = false,
FILE * fout = stdout );
PARAMETERDESCRIPTION
full

If true, print the full usage message. If false, print a brief usage message.

fout

The FILE to print the usage message to.

member arguments

The description of the positional arguments. This is used in the usage message.

DEFINITION
std::string  arguments;

member footer

The footer of the usage message.

DEFINITION
std::string  footer;

member header

The header of the usage message.

DEFINITION
std::string  header;

member options

The list of options supported by the command line parser.

DEFINITION
std::vector< Option >  options;

member program_name

The name of the program.

DEFINITION
std::string  program_name;

member version

The version of the program.

DEFINITION
std::string  version;

class Option

A command line option.

DEFINITION
class Option;

constructor Option

Create a command line option with a long variant and an argument.

DEFINITION
Option ( std::string  name_,
std::string  argument_name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
argument_name_
description_

constructor Option

Create a command line option with a long variant and no argument.

DEFINITION
Option ( std::string  name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
description_

constructor Option

Create a command line option with both short and long variants and an argument.

DEFINITION
Option ( std::string  name_,
char  short_name_,
std::string  argument_name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
short_name_
argument_name_
description_

constructor Option

Create a command line option with both short and long variants and no argument.

DEFINITION
Option ( std::string  name_,
char  short_name_,
std::string  description_ );
PARAMETERDESCRIPTION
name_
short_name_
description_

method has_argumentconst

Check if the option has an argument.

DEFINITION
bool  has_argument ( ) const;
RETURNSDESCRIPTION
bool

true if the option has an argument, false otherwise.

method operator<const

DEFINITION
bool  operator< ( const Option & other ) const;
PARAMETERDESCRIPTION
other
RETURNSDESCRIPTION
bool

member argument_name

The name of the argument for the option. This is used in the usage message. Empty if the option has no argument.

DEFINITION
std::string  argument_name;

member description

The description of the option. This is used in the usage message.

DEFINITION
std::string  description;

member name

The long name of the option. This is specified on the command line by prefixing it with ` (e.g. "help" forhelp`).

DEFINITION
std::string  name;

member short_name

The short name of the option. This is specified on the command line by prefixing it with - (e.g. 'h' for -h). Empty if the option has no short name.

DEFINITION
std::optional< char >  short_name;

class ParsedCommandline

The parsed command line options and arguments.

DEFINITION
class ParsedCommandline;

class OptionValue

A parsed command line option.

DEFINITION
class OptionValue;

constructor OptionValue

Create a parsed command line option with the given name and argument.

DEFINITION
OptionValue ( std::string  name_,
std::string  argument_ );
PARAMETERDESCRIPTION
name_
argument_

member argument

The argument of the option. Empty string for options without argument.

DEFINITION
std::string  argument;

member name

The name of the option.

DEFINITION
std::string  name;

method add_argument

Add an argument to the parsed command line.

DEFINITION
void  add_argument ( std::string  name );
PARAMETERDESCRIPTION
name

The argument to add.

method add_option

Add an option to the parsed command line.

DEFINITION
void  add_option ( std::string  name,
std::string  argument );
PARAMETERDESCRIPTION
name

The name of the option.

argument

The argument of the option. Empty string for options without argument.

method find_firstconst

Find the first occurrence of the option with the given name.

DEFINITION
std::optional< std::string >  find_first ( std::string_view  name ) const;
PARAMETERDESCRIPTION
name

The name of the option to find.

RETURNSDESCRIPTION
std::optional< std::string >

The argument of the option, or {} if the option was not found.

method find_lastconst

Find the last occurrence of the option with the given name.

DEFINITION
std::optional< std::string >  find_last ( std::string_view  name ) const;
PARAMETERDESCRIPTION
name

The name of the option to find.

RETURNSDESCRIPTION
std::optional< std::string >

The argument of the option, or {} if the option was not found.

member arguments

The parsed command line arguments.

DEFINITION
std::vector< std::string >  arguments;

member exit_code

The exit code of the command. If this is set, the command should not be run and the program should exit with this code.

DEFINITION
std::optional< int >  exit_code;

member options

The parsed command line options.

DEFINITION
std::vector< OptionValue >  options;

class OptionValue

A parsed command line option.

DEFINITION
class OptionValue;

constructor OptionValue

Create a parsed command line option with the given name and argument.

DEFINITION
OptionValue ( std::string  name_,
std::string  argument_ );
PARAMETERDESCRIPTION
name_
argument_

member argument

The argument of the option. Empty string for options without argument.

DEFINITION
std::string  argument;

member name

The name of the option.

DEFINITION
std::string  name;