Standard Library
std.args
Command-line argument parsing utilities.
Exported functions
Return all command-line arguments as a list of strings.
Examples
# Running: tea script.tea arg1 arg2
const all = args.all() # ["arg1", "arg2"]Return the program name (argv[0] with path stripped).
Examples
# Running: tea script.tea
const name = args.program() # "script"
# Running: ./bin/myapp
const name = args.program() # "myapp"Parse command-line arguments using a declarative command spec.
Parse an explicit argv list using a declarative command spec.
Examples
use args from "std.args"
use json from "std.json"
const spec = json.decode[Dict[String, Dynamic]](`{
"name": "greet",
"options": [
{"name": "count", "aliases": ["--count"], "kind": "option", "type": "int"},
{"name": "verbose", "aliases": ["-v"], "kind": "flag"}
],
"positionals": [{"name": "name", "type": "string"}]
}`)
const parsed = args.parse_with(spec, ["--count", "3", "-v", "Ada"])
@println(parsed.ok) # true
@println(args.option_int_or(parsed, "count", 1)) # 3
@println(args.flag(parsed, "verbose")) # true
@println(args.positional_or(parsed, "name", "world")) # AdaRequire a parsed CLI result to be successful.
Check if a flag is present in the arguments.
Examples
if args.has("--verbose")
@println("Verbose mode enabled")
end
if args.has("-v") || args.has("--verbose")
verbose = true
endGet the value of an option argument.
Examples
# Running: tea script.tea --output result.txt
const output = args.get("--output") # "result.txt"
# Running: tea script.tea --verbose
const output = args.get("--output") # nil
# With default value
const output = args.get("--output") ?? "default.txt"Return positional arguments (arguments that are not flags or option values).
Examples
# Running: tea script.tea --verbose file1.txt file2.txt
const files = args.positional() # ["file1.txt", "file2.txt"]
# Running: tea script.tea -o output.txt input.txt
const files = args.positional() # ["input.txt"]Return an optional parsed string option value by name.
Return an optional parsed integer option value by name.
Return an optional parsed boolean option value by name.
Return true when a parsed command result contains a specific flag option.
Return a string option value or a fallback.
Return an integer option value or a fallback.
Examples
use args from "std.args"
use json from "std.json"
const spec = json.decode[Dict[String, Dynamic]](`{
"name": "greet",
"options": [{"name": "count", "aliases": ["--count"], "kind": "option", "type": "int"}]
}`)
const parsed = args.parse_with(spec, [])
@println(args.option_int_or(parsed, "count", 1)) # 1Return a string option value or throw when it is missing.
Return an integer option value or throw when it is missing.
Return an optional positional value by name.
Return a positional value or a fallback.
Return a positional value or throw when it is missing.
Return the selected subcommand when one was provided.
Examples
use args from "std.args"
use json from "std.json"
const spec = json.decode[Dict[String, Dynamic]](`{"name": "todo", "subcommands": [{"name": "list"}, {"name": "add"}]}`)
const parsed = args.parse_with(spec, ["list"])
@println(args.subcommand(parsed) ?? "help") # list
@println(args.command_is(parsed, "list")) # trueReturn the selected subcommand or throw when no subcommand was chosen.
Return true when the current parsed command matches a given command name.