Skip to content

Cli Option

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

CliOption<T> struct

A single command line option of a CLI parser. An option either writes the parsed value into a target variable or invokes a callback with it, depending on the mode it was constructed with.

Constructors

ctor

Spice
public p CliOption.ctor(string name, T& targetVariable, string description)

Construct a CLI option that writes the parsed value into a target variable

Parameters

Name Type Description
name string Primary name of the option (e.g. "--output")
targetVariable T& Variable the parsed value is written to
description string Help text describing the option

ctor

Spice
public p CliOption.ctor(const String& name, T& targetVariable, const String& description)

Construct a CLI option that writes the parsed value into a target variable

Parameters

Name Type Description
name const String& Primary name of the option (e.g. "--output")
targetVariable T& Variable the parsed value is written to
description const String& Help text describing the option

ctor

Spice
public p CliOption.ctor(string name, p(const T&) callback, string description)

Construct a CLI option that invokes a callback with the parsed value

Parameters

Name Type Description
name string Primary name of the option (e.g. "--output")
callback p(const T&) Callback to invoke with the parsed value
description string Help text describing the option

ctor

Spice
public p CliOption.ctor(const String& name, p(const T&) callback, const String& description)

Construct a CLI option that invokes a callback with the parsed value

Parameters

Name Type Description
name const String& Primary name of the option (e.g. "--output")
callback p(const T&) Callback to invoke with the parsed value
description const String& Help text describing the option

Methods

getName

Spice
public const f<const String&> CliOption.getName()

Retrieve the primary name of the option

Returns: const String& — Name of the option

getDescription

Spice
public const f<const String&> CliOption.getDescription()

Retrieve the help text describing the option

Returns: const String& — Description of the option

addAlias

Spice
public p CliOption.addAlias(string optionName)

Add an alternative name (alias) for the option

Parameters

Name Type Description
optionName string Alias to add

addAlias

Spice
public p CliOption.addAlias(const String& optionName)

Add an alternative name (alias) for the option

Parameters

Name Type Description
optionName const String& Alias to add

getAliases

Spice
public const f<const Vector<String>&> CliOption.getAliases()

Retrieve the list of aliases registered for the option

Returns: const Vector<String>& — Aliases of the option

allowMultipleValues

Spice
public p CliOption.allowMultipleValues()

Allow the option to take multiple values. All arguments after the option are taken as its values, up to the next option or the positional arguments, that are still missing. Each value is passed to the option separately.

acceptsMultipleValues

Spice
public const f<bool> CliOption.acceptsMultipleValues()

Check whether the option can take multiple values

Returns: bool — true if the option can take multiple values, false otherwise

setTargetValue

Spice
public p CliOption.setTargetValue(T& value)

Write the given value into the option's target variable, if it is in set-value mode

Parameters

Name Type Description
value T& Value to write

callCallback

Spice
public p CliOption.callCallback(const T& value)

Invoke the option's callback with the given value, if it is in call-callback mode

Parameters

Name Type Description
value const T& Value to pass to the callback

isPositional

Spice
public const f<bool> CliOption.isPositional()

Check whether the option is positional, i.e. neither its name nor any alias starts with a dash

Returns: bool — true if the option is positional, false otherwise

printHelpItem

Spice
public p CliOption.printHelpItem()

Print the option's name, aliases and description as a single help entry