Skip to content

Cli Parser

Spice
import "std/io/cli-parser";

CliParser struct

A command line argument parser. It owns the root subcommand and forwards configuration and parsing to it, so options, flags and subcommands can be registered before parsing the actual arguments.

Constructors

ctor

Spice
public p CliParser.ctor(string appName, string appDescription = "")

Construct a CLI parser for an application

Parameters

Name Type Description
appName string Name of the application
appDescription string Description of the application shown in the help output (default: "")

ctor

Spice
public p CliParser.ctor(const String& appName, const String& appDescription = String(""))

Construct a CLI parser for an application

Parameters

Name Type Description
appName const String& Name of the application
appDescription const String& Description of the application shown in the help output (default: String(""))

Methods

setVersion

Spice
public p CliParser.setVersion(string versionString)

Set the version string reported by the application

Parameters

Name Type Description
versionString string Version string to report

setVersion

Spice
public p CliParser.setVersion(const String& versionString)

Set the version string reported by the application

Parameters

Name Type Description
versionString const String& Version string to report

setFooter

Spice
public p CliParser.setFooter(string footer)

Set the footer text shown at the bottom of the help output

Parameters

Name Type Description
footer string Footer text

setFooter

Spice
public p CliParser.setFooter(const String& footer)

Set the footer text shown at the bottom of the help output

Parameters

Name Type Description
footer const String& Footer text

setRootCallback

Spice
public p CliParser.setRootCallback(p() callback)

Set the callback invoked when the application is run without a subcommand

Parameters

Name Type Description
callback p() Callback to invoke

allowUnknownOptions

Spice
public p CliParser.allowUnknownOptions()

Allow unknown options to be passed without aborting parsing with an error

addSubcommand

Spice
public f<CliSubcommand&> CliParser.addSubcommand<StrOrConstStrObjRef>(StrOrConstStrObjRef name, StrOrConstStrObjRef description)

Register a subcommand on the application

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the subcommand
description StrOrConstStrObjRef Description of the subcommand

Returns: CliSubcommand& — Reference to the newly created subcommand

addOption

Spice
public f<CliOption<T>&> CliParser.addOption<T, StrOrConstStrObjRef>(StrOrConstStrObjRef name, T& targetVariable, StrOrConstStrObjRef description)

Register an option that writes its parsed value into a target variable

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the option
targetVariable T& Variable the parsed value is written to
description StrOrConstStrObjRef Description of the option

Returns: CliOption<T>& — Reference to the newly created option

addOption

Spice
public f<CliOption<T>&> CliParser.addOption<T, StrOrConstStrObjRef>(StrOrConstStrObjRef name, p(const T&) callback, StrOrConstStrObjRef description)

Register an option that invokes a callback with its parsed value

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the option
callback p(const T&) Callback to invoke with the parsed value
description StrOrConstStrObjRef Description of the option

Returns: CliOption<T>& — Reference to the newly created option

addFlag

Spice
public f<CliOption<bool>&> CliParser.addFlag<StrOrConstStrObjRef>(StrOrConstStrObjRef name, bool& targetVariable, StrOrConstStrObjRef description)

Register a boolean flag that writes its presence into a target variable

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the flag
targetVariable bool& Variable set to true when the flag is present
description StrOrConstStrObjRef Description of the flag

Returns: CliOption<bool>& — Reference to the newly created flag

addFlag

Spice
public f<CliOption<bool>&> CliParser.addFlag<StrOrConstStrObjRef>(StrOrConstStrObjRef name, p(const bool&) callback, StrOrConstStrObjRef description)

Register a boolean flag that invokes a callback when present

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the flag
callback p(const bool&) Callback to invoke when the flag is present
description StrOrConstStrObjRef Description of the flag

Returns: CliOption<bool>& — Reference to the newly created flag

parse

Spice
public f<int> CliParser.parse(unsigned int argc, string[] argv)

Parse the given command line arguments

Parameters

Name Type Description
argc unsigned int Number of arguments
argv string[] Array of argument strings

Returns: int — Exit code of the parse run