Skip to main content

argdispatch 🐈 Drop-in replacement for argparse dispatching subcommand calls to functions, modules or binaries

If your parser has a few subcommands, you can parse them with argparse. If you have more, you still can, but you will get a huge, unreadable code. This module makes this easier by dispatching subcommand calls to functions, modules or binaries.

Examples

Example 1 : Manual definition of subcommands

For instance, consider the following code for mycommand.py:

import sys
from argdispatch import ArgumentParser

def foo(args):
    """A function associated to subcommand `foo`."""
    print("Doing interesting stuff")
    sys.exit(1)

if __name__ == "__main__":
    parser = ArgumentParser()
    subparser = parser.add_subparsers()

    subparser.add_function(foo)
    subparser.add_module("bar")
    subparser.add_executable("baz")

     parser.parse_args()

With this simple code:

  • mycommand.py foo -v --arg=2 is equivalent to the python code foo(['-v', '--arg=2']);

  • mycommand.py bar -v --arg=2 is equivalent to python -m bar -v --arg=2;

  • mycommand.py baz -v --arg=2 is equivalent to baz -v --arg=2.

Then, each function, module or binary does whatever it wants with the arguments.

Example 2 : Automatic definition of subcommands

With programs like git, if a git-foo binary exists, then calling git foo --some=arguments is equivalent to git-foo --some=arguments. The following code, in myprogram.py copies this behaviour:

import sys
from argdispatch import ArgumentParser

if __name__ == "__main__":
    parser = ArgumentParser()
    subparser = parser.add_subparsers()

    subparser.add_submodules("myprogram")
    subparser.add_prefix_executables("myprogram-")

    parser.parse_args()

With this program, given that binary myprogram-foo and python module myprogram.bar.__main__.py exist:

  • myprogram foo -v --arg=2 is equivalent to myprogram-foo -v --arg=2;

  • myprogram bar -v --arg=2 is equivalent to python -m myprogram.bar -v --arg=2.

Example 3 : Defining subcommands with entry points

Now that your program is popular, people start writing plugins. Great! You want to allow them to add subcommands to your program. To do so, simply use this code:

import sys
from argdispatch import ArgumentParser

if __name__ == "__main__":
    parser = ArgumentParser()
    subparser = parser.add_subparsers()

    # You probably should only have one of those.
    subparser.add_entrypoints_functions("myprogram.subcommand.function")
    subparser.add_entrypoints_modules("myprogram.subcommand.module")

    parser.parse_args()

With this code, plugin writers can add lines like those in their setup.cfg:

[options.entry_points]
myprogram.subcommand.function =
    foo = mypluginfoo:myfunction
myprogram.subcommand.module =
    bar = mypluginbar

Then, given than function myfunction() exists in module mypluginfoo, and than module mypluginbar exists:

  • myprogram foo -v --arg=2 is equivalent to the python code myfunction(['-v', '--arg=2']);

  • myprogram bar -v --arg=2 is equivalent to python -m mypluginbar -v --arg=2.

Documentation

The complete documentation is available on readthedocs.

To compile it from source, download and run:

cd doc && make html

What’s new?

See changelog.

Download and install

  • From sources:

  • From pip:

    pip install argdispatch
  • Quick and dirty Debian (and Ubuntu?) package

    This requires stdeb to be installed:

    python3 setup.py --command-packages=stdeb.command bdist_deb
    sudo dpkg -i deb_dist/argdispatch-<VERSION>_all.deb

Metadata

Release files for argdispatch 1.6.0

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

Source distribution (sdist)

Source distribution for argdispatch 1.6.0
File Size Uploaded Provenance Actions
argdispatch-1.6.0.tar.gz 43.1 kB Without provenance Details for argdispatch-1.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for argdispatch 1.6.0
File Interpreter ABI Platform Actions
argdispatch-1.6.0-py3-none-any.whl Python 3 none any Details for argdispatch-1.6.0-py3-none-any.whl

Total release size: 65.3 kB

Release files / argdispatch-1.6.0.tar.gz

Download URL argdispatch-1.6.0.tar.gz
Size 43.1 kB
Tags Source
SHA-256 checksum
How to use checksums
01b86724b6843ea4122ed02f323802f943962100c4e9b7ad1e50b35e5e7f5bb0
BLAKE2b-256 checksum
How to use checksums
9861da41da243bd6f14a5938a1b0d3aa69bfb14cfaf71fd3e942c52fd13e3439
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / argdispatch-1.6.0-py3-none-any.whl

Download URL argdispatch-1.6.0-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3bc55cf5ecb88ce3fc16b106c5991f7cd110db949443aa2f2853aa46ab945291
BLAKE2b-256 checksum
How to use checksums
2dc8d8d8efb4210a2f8a75faee22cae8a1f11fc7ccc4ac6155cc110513017d7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for argdispatch 1.6.0

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