Skip to content

Cli Subcommand

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

CliSubcommand struct

A subcommand of a CLI parser. It groups options, flags and nested subcommands under a name and is responsible for parsing the slice of arguments that belongs to it. The root of a CliParser is itself a CliSubcommand.

Constructors

ctor

Spice
public p CliSubcommand.ctor(CliSubcommand* parent, const String& versionString, const String& name, const String& description = String(""))

Construct a subcommand

Parameters

Name Type Description
parent CliSubcommand* Parent subcommand, or nil for the root subcommand
versionString const String& Version string inherited from the parent
name const String& Name of the subcommand
description const String& Description of the subcommand shown in the help output (default: String(""))

Methods

parse

Spice
public f<int> CliSubcommand.parse(unsigned int argc, string[] argv, int layer = 1)

Parse the arguments belonging to this subcommand, dispatching to nested subcommands as needed

Parameters

Name Type Description
argc unsigned int Number of arguments
argv string[] Array of argument strings
layer int Index of the first argument that belongs to this subcommand (default: 1)

Returns: int — Exit code of the parse run

getName

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

Retrieve the name of the subcommand

Returns: const String& — Name of the subcommand

getDescription

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

Retrieve the description of the subcommand

Returns: const String& — Description of the subcommand

setVersion

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

Set the version string reported by the subcommand

Parameters

Name Type Description
versionString const String& Version string to report

setFooter

Spice
public p CliSubcommand.setFooter(const String& footerString)

Set the footer text shown at the bottom of the subcommand's help output

Parameters

Name Type Description
footerString const String& Footer text

setCallback

Spice
public p CliSubcommand.setCallback(p() callback)

Set the callback invoked when this subcommand is selected without a nested subcommand

Parameters

Name Type Description
callback p() Callback to invoke

allowUnknownOptions

Spice
public p CliSubcommand.allowUnknownOptions()

Allow unknown options on this subcommand and all of its nested subcommands

addAlias

Spice
public p CliSubcommand.addAlias(string subCommand)

Add an alternative name (alias) for the subcommand

Parameters

Name Type Description
subCommand string Alias to add

addAlias

Spice
public p CliSubcommand.addAlias(const String& subCommand)

Add an alternative name (alias) for the subcommand

Parameters

Name Type Description
subCommand const String& Alias to add

addSubcommand

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

Register a nested subcommand

Parameters

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

Returns: CliSubcommand& — Reference to the newly created subcommand

addSubcommand

Spice
public f<CliSubcommand&> CliSubcommand.addSubcommand(const String& name, const String& description)

Register a nested subcommand

Parameters

Name Type Description
name const String& Name of the subcommand
description const String& Description of the subcommand

Returns: CliSubcommand& — Reference to the newly created subcommand

addOption

Spice
public f<CliOption<string>&> CliSubcommand.addOption<StrOrConstStrObjRef, TargetVarOrCallbackString>(StrOrConstStrObjRef name, TargetVarOrCallbackString output, StrOrConstStrObjRef description)

Register a string option on the subcommand. If the name does not start with a dash, the option is treated as a positional argument.

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the option
output TargetVarOrCallbackString Target variable or callback receiving the parsed value
description StrOrConstStrObjRef Description of the option

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

addOption

Spice
public f<CliOption<String>&> CliSubcommand.addOption<StrOrConstStrObjRef, TargetVarOrCallbackStringObj>(StrOrConstStrObjRef name, TargetVarOrCallbackStringObj output, StrOrConstStrObjRef description)

Register a String option on the subcommand. If the name does not start with a dash, the option is treated as a positional argument.

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the option
output TargetVarOrCallbackStringObj Target variable or callback receiving the parsed value
description StrOrConstStrObjRef Description of the option

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

addOption

Spice
public f<CliOption<int>&> CliSubcommand.addOption<StrOrConstStrObjRef, TargetVarOrCallbackInt>(StrOrConstStrObjRef name, TargetVarOrCallbackInt output, StrOrConstStrObjRef description)

Register an int option on the subcommand. If the name does not start with a dash, the option is treated as a positional argument.

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the option
output TargetVarOrCallbackInt Target variable or callback receiving the parsed value
description StrOrConstStrObjRef Description of the option

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

addOption

Spice
public f<CliOption<FilePath>&> CliSubcommand.addOption<StrOrConstStrObjRef, TargetVarOrCallbackFilePath>(StrOrConstStrObjRef name, TargetVarOrCallbackFilePath output, StrOrConstStrObjRef description)

Register a FilePath option on the subcommand. If the name does not start with a dash, the option is treated as a positional argument.

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the option
output TargetVarOrCallbackFilePath Target variable or callback receiving the parsed value
description StrOrConstStrObjRef Description of the option

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

addFlag

Spice
public f<CliOption<bool>&> CliSubcommand.addFlag<StrOrConstStrObjRef, TargetVarOrCallbackBool>(StrOrConstStrObjRef name, TargetVarOrCallbackBool output, StrOrConstStrObjRef description)

Register a boolean flag on the subcommand

Parameters

Name Type Description
name StrOrConstStrObjRef Name of the flag
output TargetVarOrCallbackBool Target variable set to true, or callback invoked, when the flag is present
description StrOrConstStrObjRef Description of the flag

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

printHelpItem

Spice
public p CliSubcommand.printHelpItem()

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