Commandline
#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.
class Option
A command line option.
constructor Option
Create a command line option with a long variant and an argument.
| Option | ( | std::string name_, | |
| std::string argument_name_, | |||
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
argument_name_ |
|
description_ |
constructor Option
Create a command line option with a long variant and no argument.
| Option | ( | std::string name_, | |
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
description_ |
constructor Option
Create a command line option with both short and long variants and an argument.
| Option | ( | std::string name_, | |
| char short_name_, | |||
| std::string argument_name_, | |||
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
short_name_ |
|
argument_name_ |
|
description_ |
constructor Option
Create a command line option with both short and long variants and no argument.
| Option | ( | std::string name_, | |
| char short_name_, | |||
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
short_name_ |
|
description_ |
method has_argumentconst
Check if the option has an argument.
| bool has_argument | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method operator<const
| bool operator< | ( | const Option & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
| RETURNS | DESCRIPTION |
|---|---|
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.
member description
The description of the option. This is used in the usage message.
member name
The long name of the option. This is specified on the command line by prefixing it with ` (e.g. "help" forhelp`).
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.
constructor Commandline
Create a command line parser.
| Commandline | ( | std::vector< Option > options, | |
| std::string arguments, | |||
| std::string header, | |||
| std::string footer, | |||
| std::string version | ); |
| PARAMETER | DESCRIPTION |
|---|---|
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.
| void add_option | ( | Option option | ); |
| PARAMETER | DESCRIPTION |
|---|---|
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.
| ParsedCommandline parse | ( | int argc, | |
| char *const argv, | |||
| bool allow_exit = true | ); |
| PARAMETER | DESCRIPTION |
|---|---|
argc |
The number of command line arguments. |
argv |
The command line arguments. |
allow_exit |
If |
| RETURNS | DESCRIPTION |
|---|---|
ParsedCommandline |
The parsed command line. |
method usage
Print the usage message to the given FILE.
| void usage | ( | bool full = false, | |
| FILE * fout = stdout | ); |
| PARAMETER | DESCRIPTION |
|---|---|
full |
If |
fout |
The |
member arguments
The description of the positional arguments. This is used in the usage message.
member footer
The footer of the usage message.
member header
The header of the usage message.
member options
The list of options supported by the command line parser.
member program_name
The name of the program.
member version
The version of the program.
class Option
A command line option.
constructor Option
Create a command line option with a long variant and an argument.
| Option | ( | std::string name_, | |
| std::string argument_name_, | |||
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
argument_name_ |
|
description_ |
constructor Option
Create a command line option with a long variant and no argument.
| Option | ( | std::string name_, | |
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
description_ |
constructor Option
Create a command line option with both short and long variants and an argument.
| Option | ( | std::string name_, | |
| char short_name_, | |||
| std::string argument_name_, | |||
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
short_name_ |
|
argument_name_ |
|
description_ |
constructor Option
Create a command line option with both short and long variants and no argument.
| Option | ( | std::string name_, | |
| char short_name_, | |||
| std::string description_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
short_name_ |
|
description_ |
method has_argumentconst
Check if the option has an argument.
| bool has_argument | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method operator<const
| bool operator< | ( | const Option & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
| RETURNS | DESCRIPTION |
|---|---|
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.
member description
The description of the option. This is used in the usage message.
member name
The long name of the option. This is specified on the command line by prefixing it with ` (e.g. "help" forhelp`).
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.
class ParsedCommandline
The parsed command line options and arguments.
class OptionValue
A parsed command line option.
constructor OptionValue
Create a parsed command line option with the given name and argument.
| OptionValue | ( | std::string name_, | |
| std::string argument_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
argument_ |
member argument
The argument of the option. Empty string for options without argument.
member name
The name of the option.
method add_argument
Add an argument to the parsed command line.
| void add_argument | ( | std::string name | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name |
The argument to add. |
method add_option
Add an option to the parsed command line.
| void add_option | ( | std::string name, | |
| std::string argument | ); |
| PARAMETER | DESCRIPTION |
|---|---|
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.
| std::optional< std::string > find_first | ( | std::string_view name | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
name |
The name of the option to find. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< std::string > |
The argument of the option, or |
method find_lastconst
Find the last occurrence of the option with the given name.
| std::optional< std::string > find_last | ( | std::string_view name | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
name |
The name of the option to find. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< std::string > |
The argument of the option, or |
member arguments
The parsed command line 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.
member options
The parsed command line options.
class OptionValue
A parsed command line option.
constructor OptionValue
Create a parsed command line option with the given name and argument.
| OptionValue | ( | std::string name_, | |
| std::string argument_ | ); |
| PARAMETER | DESCRIPTION |
|---|---|
name_ |
|
argument_ |
member argument
The argument of the option. Empty string for options without argument.
member name
The name of the option.