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

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.

is a comma separated list of long form and short form names

Register an option with name that will be parsed as a list of strings. If the option is not provided on a command line, the value will remain an empty vector.

(including the dashes) that are accepted for this option. desc is a human-friendly description for printing help text. valueName is an example name for the value when printing help text.

void setPositional(std::vector<std::string>& values, string_view valueName)
auto parse(int argc, const char*const argv[]) -> bool
auto parse(string_view argList) -> bool
auto parse(span<const string_view> args) -> bool
auto getProgramName() const -> string_view
Gets the name of the program, parsed out of the first item on the command line.
void setProgramName(string_view name)
Manually set the program name for later use in help text.
auto getErrors() const -> span<const std::string>
Gets the set of errors that were encountered when parsing command line options.
auto getHelpText(string_view overview) const -> std::string

Function documentation

void slang::CommandLine::setPositional(std::vector<std::string>& values, string_view valueName)

Set a variable that will receive any positional arguments provided on the command line. They will be returned as a list of strings. @valueName is for including in the help text.

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

Returns true on success, false if an errors occurs.

Parse the provided command line (C-style).

bool slang::CommandLine::parse(string_view argList)

Returns true on success, false if an errors occurs.

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

bool slang::CommandLine::parse(span<const string_view> args)

Returns true on success, false if an errors occurs.

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

std::string slang::CommandLine::getHelpText(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.