Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The cli module

Command-line parsing: declare a program’s options and arguments, then parse them, with the help, version and error messages generated from the declarations.

Bring it in with import cli. The raw arguments stay where they are, in main(args: String[]) and process.arguments().

  • Parser: Parser, and the handles its declarations return: Flag, Argument, ArgumentList; and closest, which picks the name a misspelled one most likely meant
  • Completions: Shell, the shells Parser.completions writes a completion script for
  • Options: Option and Positional, the attributes that shape what Parser.add_options declares from a struct’s fields

Example

import cli;

main(args: String[]): int {
    var parser = cli.Parser("greet", "Greets someone");
    var loud = parser.flag("loud", "l", "shout the greeting");
    var name = parser.positional("name", "who to greet", required = false, default_value = "world");
    if (!parser.parse(args)) {
        return 2;
    }
    var greeting = $"hello, {name.value()}";
    print(loud.is_set ? greeting.upper() : greeting);
    return 0;
}

Source: runtime/cli/module.pep