args API reference
A declarative command-line argument parser, at the common surface of Python's argparse plus the features an argparse user misses immediately: typed flags (long + short), defaults, required args, choices, count / append actions, positionals with nargs, subcommands, and --version. It is the structured layer over os.ARGS, replacing the deprecated os.hasFlag / os.flag. Pure Jennifer over strings + convert + lists + maps, so it runs on both binaries.
Build a value-semantic Parser with the copy-returning builder pattern (args.parser then args.flag / args.intFlag / args.positional / ...), then args.parse($p, os.ARGS) -> a Result. Read values back with the typed accessors (args.asString / asInt / asFloat / asBool / asList / args.count). An unknown flag, a missing required arg, a bad-type value, or a choices violation throws a catchable Error{kind: "args"}; -h / --help (and --version) set the result's done flag with helpText to print, rather than exiting the process, so it composes with try / catch.
Import with import "args.j" as args;. See the args guide for prose and examples.
Contents
| Name | What it is |
|---|---|
asBool | The bool value of a flag (true when the stored value is "true"). |
asFloat | The float value of an argument. |
asInt | The int value of an argument (parsed from its stored string). |
asList | The list value of an append flag or variadic positional. |
asString | The string value of an argument (its provided value or default; "" if unset). |
boolFlag | Add a boolean flag (presence sets it true; the argparse store_true action). |
choices | Constrain the most-recently-added argument to a set of allowed values. |
command | Add a subcommand with its own parser (argparse subparsers). |
count | The tally of a count flag. |
countFlag | Add a repeatable counting flag: each occurrence increments a tally (-vvv -> 3), the argparse count action. |
dispatch | Dispatch the parsed subcommand to its handler. |
flag | Add a string-valued optional flag (--long / -short), with a default. |
floatFlag | Add a float-valued optional flag. |
has | Whether an argument was actually supplied on the command line (as opposed to taking its default). |
intFlag | Add an int-valued optional flag. |
listFlag | Add a repeatable value flag: each occurrence appends to a list (the argparse append action). |
parse | Parse argv (the full os.ARGS, whose first element is the program name and is skipped) against the parser. |
parser | Start a parser with a program name and one-line description. |
positional | Add a single required positional argument. |
positionalList | Add a variadic positional collecting zero or more values into a list (nargs "*"). |
positionalList1 | Add a variadic positional requiring one or more values (nargs "+"). |
positionalN | Add a positional taking exactly n values into a list (nargs N). |
positionalOpt | Add an optional positional (nargs "?", 0 or 1) with a default. |
required | Mark the most-recently-added argument as required. |
usage | The generated usage / help text for a parser (what --help prints). |
version | Enable a --version action printing ver. |
Arg | struct: One argument definition (a flag or a positional). |
Command | struct: A subcommand: a name plus its own Parser (its own flags and positionals). |
Parser | struct: A command-line specification: the program name, description, arguments, and any subcommands. |
Result | struct: The outcome of a parse. |
Functions
asBool
args.asBool(r as Result, name as string)The bool value of a flag (true when the stored value is "true").
r- the parse result
name- the flag name
- Returns
bool - the value
asFloat
args.asFloat(r as Result, name as string)The float value of an argument.
r- the parse result
name- the argument name
- Returns
float - the value (0.0 if unset)
asInt
args.asInt(r as Result, name as string)The int value of an argument (parsed from its stored string).
r- the parse result
name- the argument name
- Returns
int - the value (0 if unset)
asList
args.asList(r as Result, name as string)The list value of an append flag or variadic positional.
r- the parse result
name- the argument name
- Returns
list of string - the collected values ([] if none)
asString
args.asString(r as Result, name as string)The string value of an argument (its provided value or default; "" if unset).
r- the parse result
name- the argument name
- Returns
string - the value
boolFlag
args.boolFlag(p as Parser, long as string, short as string, help as string)Add a boolean flag (presence sets it true; the argparse store_true action).
p- the parser
long- the long name
short- the short name ("" for none)
help- the help text
- Returns
Parser - the updated parser
choices
args.choices(p as Parser, allowed as list of string)Constrain the most-recently-added argument to a set of allowed values.
p- the parser
allowed- the permitted values
- Returns
Parser - the updated parser
- Throws
Error - kind "args" if no argument has been added yet
command
args.command(p as Parser, name as string, help as string, sub as Parser)Add a subcommand with its own parser (argparse subparsers).
p- the parent parser
name- the subcommand word
help- the subcommand's one-line help
sub- the subcommand's own parser
- Returns
Parser - the updated parser
count
args.count(r as Result, name as string)The tally of a count flag.
r- the parse result
name- the flag name
- Returns
int - the number of occurrences (0 if none)
countFlag
args.countFlag(p as Parser, long as string, short as string, help as string)Add a repeatable counting flag: each occurrence increments a tally (-vvv -> 3), the argparse count action. Read with args.count.
p- the parser
long- the long name
short- the short name ("" for none)
help- the help text
- Returns
Parser - the updated parser
dispatch
args.dispatch(r as Result, handlers as map of string to func)Dispatch the parsed subcommand to its handler. handlers maps a subcommand name to a func value func(r as Result); the handler for r.command is called with the whole Result (so it reads its own args with args.asString / asInt / ...), and its return value is passed back - so a handler may return an exit code. A Result with done set (a handled --help / --version) dispatches nothing and returns null; a caller usually checks r.done and prints r.helpText before calling this. A missing handler for the selected subcommand, or a selection of none, is a catchable error.
The handlers are ordinary func values, not names - a func value called here runs in the entry program's own context, so it resolves its own imports.
r- the parse result
handlers- subcommand name -> its handler
- Throws
Error - kind "args" when no subcommand was selected or none matches
flag
args.flag(
p as Parser,
long as string,
short as string,
deflt as string,
help as string)Add a string-valued optional flag (--long / -short), with a default.
p- the parser
long- the long name (used without the leading "--")
short- the single-char short name ("" for none)
deflt- the default when the flag is absent
help- the flag's help text
- Returns
Parser - the updated parser
floatFlag
args.floatFlag(
p as Parser,
long as string,
short as string,
deflt as float,
help as string)Add a float-valued optional flag.
p- the parser
long- the long name
short- the short name ("" for none)
deflt- the default value
help- the help text
- Returns
Parser - the updated parser
has
args.has(r as Result, name as string)Whether an argument was actually supplied on the command line (as opposed to taking its default).
r- the parse result
name- the argument name
- Returns
bool - true if supplied
intFlag
args.intFlag(
p as Parser,
long as string,
short as string,
deflt as int,
help as string)Add an int-valued optional flag.
p- the parser
long- the long name
short- the short name ("" for none)
deflt- the default value
help- the help text
- Returns
Parser - the updated parser
listFlag
args.listFlag(p as Parser, long as string, short as string, help as string)Add a repeatable value flag: each occurrence appends to a list (the argparse append action). Read with args.asList.
p- the parser
long- the long name
short- the short name ("" for none)
help- the help text
- Returns
Parser - the updated parser
parse
args.parse(p as Parser, argv as list of string)Parse argv (the full os.ARGS, whose first element is the program name and is skipped) against the parser.
p- the specification
argv- the argument vector (pass
os.ARGS) - Returns
Result - the parsed values; check
.donefor --help / --version - Throws
Error - kind "args" on an unknown flag, missing required arg, bad type, or bad choice
parser
args.parser(prog as string, help as string)Start a parser with a program name and one-line description.
prog- the program name (shown in usage)
help- the one-line description
- Returns
Parser - an empty parser
positional
args.positional(p as Parser, name as string, help as string)Add a single required positional argument.
p- the parser
name- the positional's name (its result key)
help- the help text
- Returns
Parser - the updated parser
positionalList
args.positionalList(p as Parser, name as string, help as string)Add a variadic positional collecting zero or more values into a list (nargs "*"). Read with args.asList.
p- the parser
name- the positional's name
help- the help text
- Returns
Parser - the updated parser
positionalList1
args.positionalList1(p as Parser, name as string, help as string)Add a variadic positional requiring one or more values (nargs "+"). Read with args.asList.
p- the parser
name- the positional's name
help- the help text
- Returns
Parser - the updated parser
positionalN
args.positionalN(p as Parser, name as string, n as int, help as string)Add a positional taking exactly n values into a list (nargs N).
p- the parser
name- the positional's name
n- the exact number of values
help- the help text
- Returns
Parser - the updated parser
positionalOpt
args.positionalOpt(
p as Parser,
name as string,
deflt as string,
help as string)Add an optional positional (nargs "?", 0 or 1) with a default.
p- the parser
name- the positional's name
deflt- the default when absent
help- the help text
- Returns
Parser - the updated parser
required
args.required(p as Parser)Mark the most-recently-added argument as required.
p- the parser
- Returns
Parser - the updated parser
- Throws
Error - kind "args" if no argument has been added yet
usage
args.usage(p as Parser)The generated usage / help text for a parser (what --help prints).
p- the parser
- Returns
string - the multi-line help text
version
args.version(p as Parser, ver as string)Enable a --version action printing ver.
p- the parser
ver- the version string
- Returns
Parser - the updated parser
Structs
Arg
One argument definition (a flag or a positional). Built by the flag / positional family; not usually constructed directly.
def struct Arg {
name as string,
short as string,
kind as string,
typ as string,
action as string,
fallback as string,
hasDefault as bool,
required as bool,
nargs as string,
choices as list of string,
help as string
};name- the canonical key (long flag name, or positional name)
short- the single-character short flag ("" for none)
kind- "flag" or "positional"
typ- the value type: "string" / "int" / "float" / "bool"
action- "store" (default), "count" (repeat -> int), or "append" (repeat -> list)
fallback- the default value in string form (when
hasDefault) hasDefault- whether
fallbackapplies when the arg is absent required- whether the arg must be supplied
nargs- positional arity: "" (one), "?" (0-1), "*" (0+), "+" (1+), or an integer count
choices- the allowed values ([] = any)
help- the per-argument help text
Command
A subcommand: a name plus its own Parser (its own flags and positionals).
def struct Command {
name as string,
help as string,
parser as Parser
};name- the subcommand word (e.g. "add")
help- the subcommand's one-line help
parser- the parser for the subcommand's own arguments
Parser
A command-line specification: the program name, description, arguments, and any subcommands. Value-semantic - every builder returns an updated copy.
def struct Parser {
prog as string,
help as string,
version as string,
args as list of Arg,
commands as list of Command
};prog- the program name shown in usage
help- the one-line description
version- the version string for
--version("" = no --version) args- the declared flags and positionals, in order
commands- the declared subcommands
Result
The outcome of a parse. Read it with the typed accessors rather than poking the maps directly. When done is true the parser handled -h/--help/--version: print helpText and stop.
def struct Result {
command as string,
values as map of string to string,
lists as map of string to list of string,
counts as map of string to int,
present as map of string to bool,
helpText as string,
done as bool
};command- the chosen subcommand name ("" if none)
values- store-action values (string form)
lists- append-action and variadic-positional values
counts- count-action tallies
present- which args were supplied on the command line
helpText- the text to print when
doneis set (--help/--version) done- true when
--help/--versionwas handled (caller should stop)