Grav v1.14.0

Args

import "std/sys" (included in import "std") · namespace std.sys

A command-line parser. Declare the flags, options and positional arguments your program takes; Args parses the command line, checks it, and generates the --help text.

grav
import "std"

fn main() {
    let cli = new Args("resize", "Resize images to a new width.")
        .flag("verbose", "v", "print each file")
        .option("width", "w", "the new width in pixels", "800")
        .required("out", "o", "the output folder")
        .positional("files", "the images to resize")

    cli.parse(["-v", "--width=640", "-o", "small", "a.png", "b.png"])
    print(cli.has("verbose"))
    print(cli.getInt("width"))
    print(cli.get("out"))
    print(cli.rest())
    print(cli.usage())
}
Output
true
640
small
["a.png", "b.png"]
usage: resize [options] files...

Resize images to a new width.

arguments:
  files                 the images to resize

options:
  -v, --verbose         print each file
  -w, --width <value>   the new width in pixels (default: 800)
  -o, --out <value>     the output folder (required)
  -h, --help            show this help

In a real program, call cli.parseOrExit() instead of parse(...): it reads the program's own arguments, prints the usage and exits on --help, and on a mistake (an unknown option, a missing value, a missing required option) prints the problem and the usage to standard error and exits with status 2.

Accepted forms: --name, --name=value, --name value, -n, -n value, -nvalue, several one-letter flags together (-vn), and -- to end the options (everything after it is positional).

API

class ArgSpec

One option or flag known to an Args parser.

Constructors

  • new ArgSpec(name: string, short: string, help: string, takesValue: bool, fallback: string, required: bool)

Fields

  • readonly name: string
  • readonly short: string
  • readonly help: string
  • readonly takesValue: bool
  • readonly fallback: string
  • readonly required: bool

class Args

A command-line parser: declare the flags, options and positional arguments, then parse.

Constructors

  • new Args(program: string, description: string)

Fields

  • readonly program: string — The program name used in the usage text.
  • readonly description: string — A one-line description shown in the usage text.

Methods

  • fn flag(name: string, short: string, help: string) -> Args — Declares a flag (no value): --name or -short ("" for no short form).
  • fn option(name: string, short: string, help: string, fallback: string) -> Args — Declares an option with a value and a default used when it is not given.
  • fn required(name: string, short: string, help: string) -> Args — Declares an option that must be given.
  • fn positional(name: string, help: string) -> Args — Names a positional argument for the usage text (they are all collected by rest()).
  • fn parse(argv: Array<string>) -> Args — Parses argv (without the program name). ValueError for an unknown option, a missing value, or a missing required option.
  • fn parseCommandLine() -> Args — Parses the program's own command-line arguments.
  • fn parseOrExit() -> Args — Parses the program's arguments; on --help prints the usage and exits with 0, and on a mistake prints it with the usage to standard error and exits with 2.
  • fn has(name: string) -> bool — Whether a flag or option was given.
  • fn get(name: string) -> string — An option's value, or its default when it was not given.
  • fn getInt(name: string) -> int — An option's value as an int; ValueError when it is not a whole number.
  • fn getFloat(name: string) -> float — An option's value as a float; ValueError when it is not a number.
  • fn rest() -> Array<string> — The arguments that are not options, in order.
  • fn usage() -> string — The usage text: synopsis, description, options and positional arguments.

Edit this page on GitHub