libstd/argparse

A command-line parser in the style of Python's argparse: options with values, boolean flags, positionals, subcommands with their own options, automatic --help. Create an ArgParser, register everything, call parse(), then read the values. Every parser is independent and you can create as many as you like.

#include <libstd/argparse>

Includes libstd/string, libstd/type, libstd/list, libstd/os.

Example

#include <libstd/stdio>
#include <libstd/argparse>

p = ArgParser("mygit", "a toy git, to demonstrate argparse")
p.add_flag("--verbose", "-v", "print extra information")

add_id = p.add_subcommand("add", "add a file to the staging area")
p.add_sub_positional(add_id, "file", "path of the file to add")

commit_id = p.add_subcommand("commit", "record a commit")
p.add_sub(commit_id, "--message", "-m", "commit message", 1, "")

p.parse()                                // exits with a message on errors and on --help

cmd = p.subcommand()
if p.get_bool("--verbose"):
    print("[verbose] command: ", cmd, "\n")
if cmd == "add":
    print("adding ", p.get_positional("file"), "\n")
if cmd == "commit":
    print("commit: ", p.get("--message"), "\n")

The command line, as in Python

Several parsers on one command line

parse_known() is Python's parse_known_args. It reads the options of its own parser and puts the others aside instead of failing, so each parser picks its own options from the same command line. Here prog -t 2 --name Mario works:

log = ArgParser("prog", "")
log.add("--times", "-t", "how many times", 0, "1")
log.parse_known()                        // --name Mario are put aside

app = ArgParser("prog", "")
app.add("--name", "-n", "who to greet", 0, "world")
app.parse_known()                        // -t 2 are put aside
i = 0
while i < app.unknown_count:
    print("not mine: ", app.unknown_at(i), "\n")
    i++

Arguments are analysed once

The arguments of a process never change, and what each one means depends only on the command line. The first parser that reads them therefore analyses them once for the whole process: it copies each argument into a TryC string, so its length costs O(1), and records its kind (option, positional, --, help, negative number) and the two halves of --name=value. Every ArgParser, also one created later, reads from there.

The argparse_* functions do the same things on a single parser of the program, for code that does not need instances: argparse_init(prog, description), argparse_add(...), argparse_parse(), argparse_get(...).

Constants

NameValueMeaning
ARGPARSE_NO_OWNER-1subcommand id of options and positionals valid for every subcommand
ARGPARSE_KIND_VALUE0an option that takes a value
ARGPARSE_KIND_FLAG1a boolean flag
ARGPARSE_ARG_POSITIONAL0anche ogni argomento dopo "--" e i numeri negativi
ARGPARSE_ARG_OPTION1inizia con '-'
ARGPARSE_ARG_SEPARATOR2il primo "--"
ARGPARSE_ARG_HELP3--help o -h

Classes

class
ArgParserArg

One argument of the process in the cache: the copied text, its kind (ARGPARSE_ARG_*), and for an option the name before = and the value after.

    text str
    kind int
    name str
    value str
    has_value int
class
ArgParserOption

A registered option: long and short form, help, default, value after the parse, kind, whether it is required, whether it was given, the subcommand it belongs to.

    long_flag str
    short_flag str
    help str
    default_value str
    value str
    kind int
    required int
    provided int
    owner int
class
ArgParserPositional

A registered positional: name, help, value, subcommand.

    name str
    help str
    value str
    owner int
class
ArgParserCommand

A registered subcommand: name and help.

    name str
    help str
class
ArgParser

A parser. Create it with ArgParser(prog, description); the methods are called on the instance: add, add_flag, add_positional, add_subcommand, add_sub, add_sub_flag, add_sub_positional, parse, parse_known, unknown_at, print_help, get, get_int, get_bool, was_provided, get_positional, subcommand. unknown_count is how many arguments parse_known put aside.

    prog str
    description str
    options List
    positionals List
    commands List
    unknown List
    option_count int
    positional_count int
    command_count int
    unknown_count int
    matched_sub int
    add &argparser_add
    add_flag &argparser_add_flag
    add_positional &argparser_add_positional
    add_subcommand &argparser_add_subcommand
    add_sub &argparser_add_sub
    add_sub_flag &argparser_add_sub_flag
    add_sub_positional &argparser_add_sub_positional
    parse &argparser_parse
    parse_known &argparser_parse_known
    unknown_at &argparser_unknown_at
    print_help &argparser_print_help
    get &argparser_get
    get_int &argparser_get_int
    get_bool &argparser_get_bool
    was_provided &argparser_was_provided
    get_positional &argparser_get_positional
    subcommand &argparser_subcommand

Functions

inline def argparser_elaborate_cache() -> int
(no description yet)
def ArgParser(prog: str, description: str) -> ArgParser

Creates an empty parser, like ArgumentParser in Python. prog is the name in help and error messages; with "" it is argv[0] without its directory.

inline def argparser_add_sub(self: ArgParser, sub_id: int, long_flag: str, short_flag: str, help_text: str, required: int, default_value: str) -> int
p.add_sub(sub_id, ...): like add, valid only when that subcommand is invoked.
def argparser_add(self: ArgParser, long_flag: str, short_flag: str, help_text: str, required: int, default_value: str) -> int
p.add(long_flag, short_flag, help, required, default): registers an option that takes a value (--name Mario or --name=Mario). Pass "" for no short form. Returns its index.
def argparser_add_sub_flag(self: ArgParser, sub_id: int, long_flag: str, short_flag: str, help_text: str) -> int
p.add_sub_flag(sub_id, ...): like add_flag, valid only for that subcommand.
def argparser_add_flag(self: ArgParser, long_flag: str, short_flag: str, help_text: str) -> int
p.add_flag(long_flag, short_flag, help): registers a boolean flag (present or absent).
def argparser_add_sub_positional(self: ArgParser, sub_id: int, name: str, help_text: str) -> int
p.add_sub_positional(sub_id, name, help): a positional valid only for that subcommand.
def argparser_add_positional(self: ArgParser, name: str, help_text: str) -> int
p.add_positional(name, help): registers a positional, recognised by position in registration order; it is required.
def argparser_add_subcommand(self: ArgParser, name: str, help_text: str) -> int
p.add_subcommand(name, help): registers a subcommand (the first positional on the command line) and returns its id.
def argparser_print_help(self: ArgParser) -> int
p.print_help(): prints usage, description, subcommands and options, plus the options of the recognised subcommand.
def argparser_parse(self: ArgParser) -> int
p.parse(): parses the command line. On an unknown option, a missing value, a missing required option or positional, an extra positional or an unknown command it prints the error and the help and exits with 1; on --help/-h before -- it prints the help and exits with 0.
def argparser_parse_known(self: ArgParser) -> int
p.parse_known(): like parse, but unknown options and extra positionals are put aside (unknown_count, unknown_at) instead of being errors. An unknown command, a missing value and missing required arguments are still errors.
def argparser_unknown_at(self: ArgParser, idx: int) -> str
p.unknown_at(i): the text of the i-th argument put aside by parse_known.
def argparser_get(self: ArgParser, long_flag: str) -> str
p.get(long_flag): the value of an option after the parse; the default if it was not given, "" if it was never registered.
def argparser_get_int(self: ArgParser, long_flag: str) -> int
p.get_int(long_flag): get converted to an integer (0 if not a number or absent).
def argparser_was_provided(self: ArgParser, long_flag: str) -> int
p.was_provided(long_flag): 1 if the option was given explicitly rather than defaulted.
def argparser_get_bool(self: ArgParser, long_flag: str) -> int
p.get_bool(long_flag): 1 if a flag was on the command line.
def argparser_get_positional(self: ArgParser, name: str) -> str
p.get_positional(name): the value of a positional by its registered name, "" if absent.
def argparser_subcommand(self: ArgParser) -> str
p.subcommand(): the subcommand recognised by the last parse, "" if none.
def argparse_init(prog: str, description: str) -> int

Resets the program's parser with the program name and description. Call it before any argparse_add*.

def argparse_add(long_flag: str, short_flag: str, help_text: str, required: int, default_value: str) -> int
add on the program's parser.
def argparse_add_flag(long_flag: str, short_flag: str, help_text: str) -> int
add_flag on the program's parser.
def argparse_add_positional(name: str, help_text: str) -> int
add_positional on the program's parser.
def argparse_add_subcommand(name: str, help_text: str) -> int
add_subcommand on the program's parser.
def argparse_add_sub(sub_id: int, long_flag: str, short_flag: str, help_text: str, required: int, default_value: str) -> int
add_sub on the program's parser.
def argparse_add_sub_flag(sub_id: int, long_flag: str, short_flag: str, help_text: str) -> int
add_sub_flag on the program's parser.
def argparse_add_sub_positional(sub_id: int, name: str, help_text: str) -> int
add_sub_positional on the program's parser.
def argparse_parse() -> int
parse on the program's parser.
def argparse_print_help() -> int
print_help on the program's parser.
def argparse_get(long_flag: str) -> str
get on the program's parser.
def argparse_get_int(long_flag: str) -> int
get_int on the program's parser.
def argparse_get_bool(long_flag: str) -> int
get_bool on the program's parser.
def argparse_was_provided(long_flag: str) -> int
was_provided on the program's parser.
def argparse_get_positional(name: str) -> str
get_positional on the program's parser.
def argparse_matched_subcommand() -> str
subcommand on the program's parser.

Internal helpers

Used by the functions above; not meant to be called directly.