cromo La guida di Cromo
Scarica PDF EN

std.cmdline

The module std.cmdline: the options of a program, read from the arguments of main, and the text of the help. A CmdLine declares the options, each with a long name, a short name of one letter and a description; then parse reads the arguments, and getInt, getFloat, getString, exists and rest give what they hold.

cromo
import std.cmdline

i32 main(list<string> args) {
    cmdline.CmdLine cmd
    cmd.flag("verbose", 'v', "print more")
    cmd.addInt("port", 'p', "port to listen on", false, 8080)
    cmd.parse(args)
    i64 port = cmd.getInt("port")
    ret 0
}

A wrong use of the module, as two options with the same name, is a panic: it is an error of the program. A wrong command line is an error of who runs it: parse prints the error and the help, and ends the program with the exit code 2.

The guide: Files, processes and time.

Generata dalla documentazione nei sorgenti della libreria, che è in inglese.

#Tipi

#CmdLine

cromo
struct CmdLine

The options of a program, the arguments that are not options, and the text of the help. The options are added first, then parse reads the arguments of main, and the methods that start with get give their values. The options can be written --port 9000, --port=9000, -p 9000 or -p9000, and the flags together, as -vq; an option given twice keeps its last value.

cromo
cmdline.CmdLine cmd
cmd.flag("verbose", 'v', "print more")
cmd.addString("format", 'f', "output format", false, "text")
cmd.choices("format", ["text", "json"])
cmd.footer("files...")
cmd.parse(args)
list<string> files = cmd.rest()

#CmdLine.flag

cromo
void flag(string name, char letter, string description)

Adds an option without a value, as --verbose and -v: name is the long name, without --, letter the short one, or a space, , when there is none, and description the text of the help. exists says whether it was given. A name that is empty, starts with - or is "help", the letters - and h, and a name or a letter used twice, are a panic.

#CmdLine.addInt

cromo
void addInt(string name, char letter, string description, bool required, i64 value)

Adds an option with an i64, as --port 8080 or -p 8080: name, letter and description as in flag. value is the default, which getInt gives when the option is not given; a required option must be given, and its default is not used. The value is read as i64("...") reads it.

#CmdLine.addFloat

cromo
void addFloat(string name, char letter, string description, bool required, f64 value)

Adds an option with an f64, as --ratio 0.5: value is the default, which getFloat gives when the option is not given; the rest as in addInt. The value is read as f64("...") reads it.

#CmdLine.addString

cromo
void addString(string name, char letter, string description, bool required, string value)

Adds an option with a string, as --format json: value is the default, which getString gives when the option is not given; the rest as in addInt.

#CmdLine.choices

cromo
void choices(string name, list<string> values)

Sets the values allowed for the option name, as texts: ["text", "json"]. A number is compared as format("{}", n) writes it, so 2.0 is "2". A value given outside the choices is an error of the command line. An option that was not added, a flag, and the default of an option that is not required when it is not among the values, are a panic.

#CmdLine.footer

cromo
void footer(string text)

Sets what the line of usage of the help shows after the options, as "files...", for the arguments that are not options.

#CmdLine.parse

cromo
void parse(list<string> args)

Reads the options from the arguments of main, where args[0] is the program: its name, without the folder and .exe, goes in the help. The arguments that are not options go to rest(); after -- every argument goes there, also when it starts with -, and so does - alone.

With -h or --help it prints the help on the standard output and ends the program with the exit code 0. A wrong command line, as an unknown option, a missing value, a number that is not valid, a value outside the choices or a required option that is missing, prints the error and the help on the standard error, and ends the program with the exit code 2.

#CmdLine.exists

cromo
bool exists(string name) const

Whether the option name, a flag or one with a value, was on the command line. A name that was not added is a panic.

#CmdLine.getInt

cromo
i64 getInt(string name) const

The value of the option name, added with addInt: the one given, or the default. A name that was not added, or an option of another type, is a panic.

#CmdLine.getFloat

cromo
f64 getFloat(string name) const

The value of the option name, added with addFloat: the one given, or the default. A name that was not added, or an option of another type, is a panic.

#CmdLine.getString

cromo
string getString(string name) const

The value of the option name, added with addString: the one given, or the default. A name that was not added, or an option of another type, is a panic.

#CmdLine.rest

cromo
list<string> rest() const

The arguments that are not options, in their order, without args[0].

#CmdLine.help

cromo
string help() const

The text of the help, which -h and --help print: the line of usage, as "usage: server [options] files...", with the required options and the footer, then a line for each option, with its short and long names, its type, its description, its choices and its default. Before parse the name of the program is "program".