Skip to content
Jennifer Programming Language

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

NameWhat it is
asBoolThe bool value of a flag (true when the stored value is "true").
asFloatThe float value of an argument.
asIntThe int value of an argument (parsed from its stored string).
asListThe list value of an append flag or variadic positional.
asStringThe string value of an argument (its provided value or default; "" if unset).
boolFlagAdd a boolean flag (presence sets it true; the argparse store_true action).
choicesConstrain the most-recently-added argument to a set of allowed values.
commandAdd a subcommand with its own parser (argparse subparsers).
countThe tally of a count flag.
countFlagAdd a repeatable counting flag: each occurrence increments a tally (-vvv -> 3), the argparse count action.
dispatchDispatch the parsed subcommand to its handler.
flagAdd a string-valued optional flag (--long / -short), with a default.
floatFlagAdd a float-valued optional flag.
hasWhether an argument was actually supplied on the command line (as opposed to taking its default).
intFlagAdd an int-valued optional flag.
listFlagAdd a repeatable value flag: each occurrence appends to a list (the argparse append action).
parseParse argv (the full os.ARGS, whose first element is the program name and is skipped) against the parser.
parserStart a parser with a program name and one-line description.
positionalAdd a single required positional argument.
positionalListAdd a variadic positional collecting zero or more values into a list (nargs "*").
positionalList1Add a variadic positional requiring one or more values (nargs "+").
positionalNAdd a positional taking exactly n values into a list (nargs N).
positionalOptAdd an optional positional (nargs "?", 0 or 1) with a default.
requiredMark the most-recently-added argument as required.
usageThe generated usage / help text for a parser (what --help prints).
versionEnable a --version action printing ver.
Argstruct: One argument definition (a flag or a positional).
Commandstruct: A subcommand: a name plus its own Parser (its own flags and positionals).
Parserstruct: A command-line specification: the program name, description, arguments, and any subcommands.
Resultstruct: The outcome of a parse.

Functions

asBool

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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 .done for --help / --version
Throws Error
kind "args" on an unknown flag, missing required arg, bad type, or bad choice

parser

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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.

jennifer
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 fallback applies 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).

jennifer
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.

jennifer
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.

jennifer
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 done is set (--help / --version)
done
true when --help / --version was handled (caller should stop)