Skip to main content

Yet another argparse helper: complex sub-command trees made simple

Writing complex subcommand parsers using argparse is certainly possible, but it involves a lot of laborious repetition.

This library provides a small wrapper around argparse that adds a convenience method. That lets you specify a sequence of words that form a command, together with flags - and the definitions of those flags are passed at the same time.

parser = ArgumentParser()
parser.add_command("foo bar baz --json",
                   func=lambda *args: print("foo-bar-baz"),
                   json=dict(default=False, action="store_true"),
                   )
parser.add_command("foo bar quux",
                   func=lambda *args: print("foo-bar-quux"),
                   )

The func parameter will be set on the resulting namespace. Note, there is no particular requirement for the arguments that must be passed to func - this is down to the library user to define.

Additionally, a new argparse.Action subclass is provided, OptionalOverride. This supports having the same flag appear in a global position and be repeated by a subcommand. It works well - in as much as defaults set by the global position won't be re-set in the subordinate position.

parser.add_command("--flag", flag=dict(default="foo"))
parser.add_command("xyzzy --flag", flag=dict(action=OptionalOverride))

Where a flag is repeated in an add_command call, the second and subsequent values receive an OptionalOveride setting automatically.

parser.add_command("--glob xyzzy --glob", glob=dict(action=AtMostOnce))
# the second iteration becomes OptionalOverride

AtMostOnce is supplied as an action type; this causes an explicit error (rather than just overriding the previous value) if a flag is specified more than once.

We also have AppendN(t) as an action type. It works like Append, but the type constructor t is applied to its collected argument(s) before the result is appended to the list.

Metadata

Release files for argparse-helper 0.9.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for argparse-helper 0.9.3
File Interpreter ABI Platform
argparse_helper-0.9.3-py3-none-any.whl Python 3 none any Details

Release files / argparse_helper-0.9.3-py3-none-any.whl

Download URL argparse_helper-0.9.3-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0b621589b2901455129f1789858991e67df8a58679aae910340c8e57b29ff3e
BLAKE2b-256 checksum
How to use checksums
c8b4853195e461994ac38b076e6b2c5b7102c71798c49af57525ecb03563af21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

Security for argparse-helper 0.9.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page