Command-line flags

Command-line flags

flag gives a program settings that can be changed when it is started, without changing the code:

name: str = flag("name", "guest")
port: int = flag("port", 8080)
print("Hello ${name} on port ${port}")

Run with:

luma run app.luma --name=Ann --port 9000

Output:

Hello Ann on port 9000

A flag that isn’t given keeps its default. Run without flags, the same program prints Hello guest on port 8080.

flag(name, default) takes the flag’s name and its default value. The flags come after the file name with luma run, or after the program’s name when it’s compiled:

luma run app.luma --name=Ann
luma build app.luma
./app --name=Ann

Writing flags on the command line

A value can follow an = or come as the next argument. These two do the same:

./app --name=Ann --port=9000
./app --name Ann --port 9000

If the same flag is given twice, the last one counts. To put spaces in a value, use the quotes of your shell: --name="Ann Lee".

The default decides the type

DefaultTypeOn the command line
"guest"strany text, also an empty one: --name=
8080inta whole number: --port=9000, --offset -5
1.5floata number: --rate=2.5, --rate=3, --rate=1e3
3 in a bytebytea whole number from 0 to 255
falsebool--debug alone means true; --debug=false or --debug=true

Values are read like the input of prompt. A whole-number default becomes a float or byte when the variable has that type: rate: float = flag("rate", 1) has the default 1.0.

A bool flag only takes its value after an =. --debug false doesn’t work: --debug would be true, and false would be an unexpected argument.

verbose: bool = flag("verbose", false)
color: bool = flag("color", true)
if verbose {
    print("verbose output")
}
print(color)

Run with:

luma run app.luma --verbose --color=false

Output:

verbose output
false

Names

A flag name is made of letters, digits, - and _, and starts with a letter: "port", "dry-run", "max_size". It’s written without the dashes in the program and with two dashes on the command line: flag("dry-run", false) is --dry-run. Upper and lower case are different.

The name and the default must be written directly in the call, like flag("port", 8080), not taken from a variable. That way Luma knows every flag of the program before it runs, so it can tell when a flag on the command line doesn’t exist.

The same flag can be read in several places, with the same type and default:

if flag("debug", false) {
    print("debug on")
}
debug: bool = flag("debug", false)
print(debug)

Run with:

luma run app.luma --debug

Output:

debug on
true

Mistakes on the command line

The command line is checked before the program starts. If something is wrong, the program prints a message and stops with exit code 1, without running at all:

Command lineMessage
--prot=9000error: unknown flag --prot (this program has --name, --port)
notes.txterror: unexpected argument "notes.txt" (flags start with --; this program has --name, --port)
--porterror: flag --port needs a value: --port=VALUE or --port VALUE
--port=abcerror: invalid value "abc" for --port: expected an int
port: int = flag("port", 8080)
print(port)

Run with:

luma run app.luma --port=abc

Output:

error: invalid value "abc" for --port: expected an int

Because a typo stops the program, a misspelled flag never goes unnoticed.

A program that doesn’t use flag at all ignores its arguments.

Rules

x: int = flag("port")                  // error: flag expects 2 arguments
n: str = "port"
x: int = flag(n, 8080)                 // error: the name of a flag must be written in quotes
x: int = flag("--port", 8080)          // error: not a valid flag name (write "port")
x: int = flag("port", 80 + 1)          // error: the default must be written directly
x: int = flag("port", "8080")          // error: cannot assign str to x, which has type int
x: int = flag("port", 80)
y: int = flag("port", 81)              // error: flag --port is already used ... with default 80

Not available yet

  • Short flags like -p 9000.
  • Arguments that aren’t flags, like file names (./app notes.txt).
  • Flags that can be given several times to make a list.
  • A --help that lists the flags.

See Limitations.