slang::CommandLine class

Command line argument parser.

Arguments are parsed in the general style of GNU longopts. Each option name can be any combination of short and long form, where short forms start with a single dash and are a single letter, and long forms start with a double dash and can be multiple letters.

All options (besides boolean flags) require a value to be passed as well. Values can be passed as whitespace separated strings. For example: -f 1 --long 2

For long form names, values can be passed with an '=' sign followed by the value instead: --long=2

For short form names, values can be passed directly adjacent in the string, no whitespace required: -f1

Groups of short form names can be combined together. The final short form name in such a group can have an adjacent value: -abcf1

Options can alternatively begin with a '+', in which case they are always long form, and expect their value(s) to come separated by additional '+' signs. For example: +opt+val1+val2

A standalone double dash, "--", indicates that all further elements of the command line are to be considered positional and not parsed as options. A single dash, "-", is always considered to be a positional argument.

If a provided option on the command line does not match any registered name, an error is reported. If the name is "close" to one of the registered names, the error will provide a helpful "did you mean" hint.

Public types

struct ParseOptions
Contains various options to control parsing of command flags.

Public functions

void add(std::string_view name, std::optional<bool>& value, std::string_view desc, bitmask<CommandLineFlags> flags = {})
Register a flag with name that will be parsed as a boolean value, setting to true if no value is provided, and otherwise accepting the strings "true", "True", "false", and "False" for longform values.
void add(std::string_view name, std::optional<int32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as an int32.
void add(std::string_view name, std::optional<uint32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a uint32.
void add(std::string_view name, std::optional<int64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as an int64.
void add(std::string_view name, std::optional<uint64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a uint64.
void add(std::string_view name, std::optional<double>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a double.
void add(std::string_view name, std::optional<std::string>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a string.
void add(std::string_view name, std::vector<int32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a list of int32s.
void add(std::string_view name, std::vector<uint32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a list of uint32s.
void add(std::string_view name, std::vector<int64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a list of int64s.
void add(std::string_view name, std::vector<uint64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a list of uint64s.
void add(std::string_view name, std::vector<double>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a list of doubles.
void add(std::string_view name, std::vector<std::string>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a list of strings.
void add(std::string_view name, OptionCallback cb, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})
Register an option with name that will be parsed as a string and forwarded to the given cb callback function for handling.
void setPositional(std::vector<std::string>& values, std::string_view valueName, bitmask<CommandLineFlags> flags = {})
Set a variable that will receive any positional arguments provided on the command line.
void setPositional(const OptionCallback& cb, std::string_view valueName, bitmask<CommandLineFlags> flags = {})
Set a callback that will receive any positional arguments provided on the command line.
auto addIgnoreCommand(std::string_view value) -> std::string
Adds a command that will be ignored if encountered during argument parsing.
auto addRenameCommand(std::string_view value) -> std::string
Adds a command that will be renamed to one of the existing commands already registered.
auto parse(int argc, const char*const argv[]) -> bool
Parse the provided command line (C-style).
auto parse(std::string_view argList, ParseOptions options = {}) -> bool
Parse the provided command line (space delimited, with handling of quoted arguments).
auto parse(std::span<const std::string_view> args, ParseOptions options = {}) -> bool
Parse the provided command line (as a pre-separated list of strings).
auto getProgramName() const -> std::string_view
Gets the name of the program, parsed out of the first item on the command line.
void setProgramName(std::string_view name)
Manually set the program name for later use in help text.
auto getErrors() const -> std::span<const std::string>
Gets the set of errors that were encountered when parsing command line options.
auto getHelpText(std::string_view overview) const -> std::string
Gets a string representing program help text, based on registered flags.

Function documentation

void slang::CommandLine::add(std::string_view name, std::optional<bool>& value, std::string_view desc, bitmask<CommandLineFlags> flags = {})

Register a flag with name that will be parsed as a boolean value, setting to true if no value is provided, and otherwise accepting the strings "true", "True", "false", and "False" for longform values.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
flags additional flags that control how the option behaves

If the flag is not provided on a command line, the value will remain unset.

void slang::CommandLine::add(std::string_view name, std::optional<int32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as an int32.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain unset.

void slang::CommandLine::add(std::string_view name, std::optional<uint32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a uint32.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain unset.

void slang::CommandLine::add(std::string_view name, std::optional<int64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as an int64.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain unset.

void slang::CommandLine::add(std::string_view name, std::optional<uint64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a uint64.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain unset.

void slang::CommandLine::add(std::string_view name, std::optional<double>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a double.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain unset.

void slang::CommandLine::add(std::string_view name, std::optional<std::string>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a string.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain unset.

void slang::CommandLine::add(std::string_view name, std::vector<int32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a list of int32s.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain an empty vector.

void slang::CommandLine::add(std::string_view name, std::vector<uint32_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a list of uint32s.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain an empty vector.

void slang::CommandLine::add(std::string_view name, std::vector<int64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a list of int64s.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain an empty vector.

void slang::CommandLine::add(std::string_view name, std::vector<uint64_t>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a list of uint64s.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain an empty vector.

void slang::CommandLine::add(std::string_view name, std::vector<double>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a list of doubles.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain an empty vector.

void slang::CommandLine::add(std::string_view name, std::vector<std::string>& value, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a list of strings.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
value a value that will be set if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the value will remain an empty vector.

void slang::CommandLine::add(std::string_view name, OptionCallback cb, std::string_view desc, std::string_view valueName = {}, bitmask<CommandLineFlags> flags = {})

Register an option with name that will be parsed as a string and forwarded to the given cb callback function for handling.

Parameters
name a comma separated list of long form and short form names (including the dashes) that are accepted for this option
cb a callback that will be invoked if the option is provided
desc a human-friendly description for printing help text
valueName an example name for the value when printing help text
flags additional flags that control how the option behaves

If the option is not provided on a command line, the callback will never be invoked.

void slang::CommandLine::setPositional(std::vector<std::string>& values, std::string_view valueName, bitmask<CommandLineFlags> flags = {})

Set a variable that will receive any positional arguments provided on the command line.

Parameters
values a vector of strings that will contain all provided positional arguments
valueName for including in the help text
flags additional flags that control how the option behaves

They will be returned as a list of strings.

void slang::CommandLine::setPositional(const OptionCallback& cb, std::string_view valueName, bitmask<CommandLineFlags> flags = {})

Set a callback that will receive any positional arguments provided on the command line.

Parameters
cb a callback that will be invoked for each position argument
valueName for including in the help text
flags additional flags that control how the option behaves

std::string slang::CommandLine::addIgnoreCommand(std::string_view value)

Adds a command that will be ignored if encountered during argument parsing.

Parameters
value a string containing a single comma-separated "name,value" pair, where the name is the argument to ignore (including any leading '+' and '-' characters) and the value is an integer indicating the (possibly zero) number of arguments to ignore following it in the argument list.
Returns a string containing an error message if the value is malformed.

std::string slang::CommandLine::addRenameCommand(std::string_view value)

Adds a command that will be renamed to one of the existing commands already registered.

Parameters
value a string containing a single comma-separated "from,to" pair, where the "from" is the command that should be renamed whenever encountered in the argument list (including any leading '+' and '-' characters), and "to" is the new name it should have.
Returns a string containing an error message if the value is malformed.

bool slang::CommandLine::parse(int argc, const char*const argv[])

Parse the provided command line (C-style).

Returns true on success, false if an errors occurs.

bool slang::CommandLine::parse(std::string_view argList, ParseOptions options = {})

Parse the provided command line (space delimited, with handling of quoted arguments).

Returns true on success, false if an errors occurs.

bool slang::CommandLine::parse(std::span<const std::string_view> args, ParseOptions options = {})

Parse the provided command line (as a pre-separated list of strings).

Returns true on success, false if an errors occurs.

std::string slang::CommandLine::getHelpText(std::string_view overview) const

Gets a string representing program help text, based on registered flags.

overview text is a human friendly description of what the program does.