cli.flag
The cli.flag function declares one typed flag for a standalone program. Pass the declarations to
cli.command, which parses the command line, applies defaults and
environment bindings, and delivers typed values to your entry function.
Usage
cli.flag(
name,
type = "string",
default = None,
shorthand = "",
description = "",
required = False,
choices = [],
env = "",
)
Arguments
nameRequired. The flag name without dashes, which is also its key in the
flagsdictionary. The user passes it as--name. A name starts with a letter and contains only letters, digits, underscores, and hyphens. The namehelpis reserved, and names are case-insensitive, soStageandstagecannot both be declared.type(Optional) One of
"string"(the default),"int","bool", or"string_list". The value arrives in theflagsdictionary as a string, an integer, a boolean, or a list of strings.default(Optional) The value used when the flag is not supplied. It must match the flag type: a string, an integer that fits a native integer, a boolean, or a list or tuple of strings. Without a default, a string flag is
"", an integer flag is0, a boolean flag isFalse, and a list flag is an empty list.shorthand- (Optional) A single letter, other than
h, that lets the user pass-rinstead of--replicas. description- (Optional) Help text for the flag.
required(Optional) Defaults to
False. A required flag must be supplied on the command line or through itsenvbinding. A required flag cannot declare a default, and boolean flags cannot be required.choices(Optional) A list or tuple of allowed strings. Choices apply to
stringandstring_listflags. A value outside the list stops the program with an error that names the valid values. A declareddefaultmust be one of the choices, otherwisecli.flagfails when the script is loaded. An empty value typed on the command line (--stage=) is checked against the choices like any other value.env(Optional) The name of an environment variable that supplies the value when the flag is not on the command line. The first non-empty value wins. Command-line values override the environment, which overrides the default. Environment values parse exactly like command-line values: a
string_listsplits on commas with CSV quoting (TAGS=a,bis["a", "b"]) and anintaccepts base-10 digits only. Withchoices, every list element is checked.
Returns
A declaration value for the flags list of cli.command.
Command-line forms
- The forms
--replicas=3,--replicas 3, and-r 3set an integer or string flag. - The form
--verbosesets a boolean flag toTrue, and--verbose=falsesets it toFalse. A boolean flag never consumes the next word, so--verbose falseis rejected with a hint to write--verbose=false. - An
intflag reads base-10 digits only.--count 010is ten, and--count 0x10is an error. - When the same flag appears more than once on the command line, the last value wins (
--stage=dev --stage=prodisprod). Only astring_listflag collects every occurrence. - A
string_listflag accepts comma-separated values, repeated flags, or both:--tag=a,b --tag=cproduces["a", "b", "c"].
Examples
#!/usr/bin/env atmos
def main(args, flags):
print(flags["replicas"], flags["environment"], flags["tag"], flags["verbose"])
cli.command(
run = main,
flags = [
cli.flag("replicas", type = "int", shorthand = "r", default = 2,
description = "Number of replicas"),
cli.flag("environment", choices = ["dev", "prod"], default = "dev",
env = "DEPLOY_ENV"),
cli.flag("tag", type = "string_list", shorthand = "t"),
cli.flag("verbose", type = "bool"),
],
)
./deploy.star --replicas=5 --tag=blue,green --verbose
5 dev ["blue", "green"] True
Errors
- An invalid name, an invalid shorthand, an unsupported
type, or a default of the wrong type fails with an argument error whencli.flagis called. - Setting
choiceson anintorboolflag fails, and so does adefaultthat is not one of thechoices. - Duplicate flag names or shorthands in one
cli.commandcall fail, and names that differ only by case count as duplicates. - A missing required flag, an unknown flag, an invalid value, or a value outside
choicesstops the program with a usage error before your functions run. A usage error has no traceback, suggests--help, and exits with status 2.
Help output
--help prints the usage and then ends the script: nothing after the cli.command call runs, and the exit status is
0.
--help annotates each flag: (required) for a required flag, (one of: dev, prod) for choices, and
[env: DEPLOY_ENV] for an env binding, for example
--environment string (one of: dev, prod) [env: DEPLOY_ENV] (default "dev").
Related
cli.commandparses the declared flags.cli.argdeclares positional arguments.- Standalone CLI apps