Skip to content

Instantly share code, notes, and snippets.

@mivade
Last active January 18, 2024 07:51
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mivade/384c2c41c3a29c637cb6c603d4197f9f to your computer and use it in GitHub Desktop.
Save mivade/384c2c41c3a29c637cb6c603d4197f9f to your computer and use it in GitHub Desktop.
Using a decorator to simplify subcommand creation with argparse
"""This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
"""
from argparse import ArgumentParser
cli = ArgumentParser()
subparsers = cli.add_subparsers(dest="subcommand")
def argument(*name_or_flags, **kwargs):
"""Convenience function to properly format arguments to pass to the
subcommand decorator.
"""
return (list(name_or_flags), kwargs)
def subcommand(args=[], parent=subparsers):
"""Decorator to define a new subcommand in a sanity-preserving way.
The function will be stored in the ``func`` variable when the parser
parses arguments so that it can be called directly like so::
args = cli.parse_args()
args.func(args)
Usage example::
@subcommand([argument("-d", help="Enable debug mode", action="store_true")])
def subcommand(args):
print(args)
Then on the command line::
$ python cli.py subcommand -d
"""
def decorator(func):
parser = parent.add_parser(func.__name__, description=func.__doc__)
for arg in args:
parser.add_argument(*arg[0], **arg[1])
parser.set_defaults(func=func)
return decorator
@subcommand()
def nothing(args):
print("Nothing special!")
@subcommand([argument("-d", help="Debug mode", action="store_true")])
def test(args):
print(args)
@subcommand([argument("-f", "--filename", help="A thing with a filename")])
def filename(args):
print(args.filename)
@subcommand([argument("name", help="Name")])
def name(args):
print(args.name)
if __name__ == "__main__":
args = cli.parse_args()
if args.subcommand is None:
cli.print_help()
else:
args.func(args)
@evdcush
Copy link

evdcush commented Mar 15, 2019

This is excellent! Thank you for your post on subparsers; keep finding there is so much more functionality in argparse than what can be gleaned from the docs. It'd be great to see this recipe like this in the docs.

Minor edit from my own usage: subcommand receives argument as-is, and a little simpler unpacking.

def argument(*names_or_flags, **kwargs):
    return names_or_flags, kwargs

def subcommand(*subparser_args, parent=subparsers):
    def decorator(func):
        parser = parent.add_parser(func.__name__, description=func.__doc__)
        for args, kwargs in subparser_args:
            parser.add_argument(*args, **kwargs)
        parser.set_defaults(func=func)
    return decorator

@subcommand(argument('-f', '--filename', help="A thing with a filename"))
def filename(args):
    print(args.filename)

@rforman9
Copy link

Thank you for posting your argparse tutorial . It is helping me to write better command line interfaces with argparse.
One thing I haven't figured out yet with your technique is how to add multiple arguments to the sub-commands.

for instance let's say I want to have an add_contacts subcommand and I want to have arguments for name, address and phone. How would that look?

I am able to add a decorator for the name argument, but if try to add multiple decorators I get the error:

Traceback (most recent call last): File "cli.py", line 50, in <module> @subcommand([argument("-p", "--phone", action="store", type=str, help="Contact phone number", required=True)]) File "cli.py", line 30, in decorator parser = parent.add_parser(func.__name__, description=func.__doc__) AttributeError: 'NoneType' object has no attribute '__name__'

any help would be greatly appreciated!
Thanks!

@rforman9
Copy link

Thank you for posting your argparse tutorial . It is helping me to write better command line interfaces with argparse.
One thing I haven't figured out yet with your technique is how to add multiple arguments to the sub-commands.

for instance let's say I want to have an add_contacts subcommand and I want to have arguments for name, address and phone. How would that look?

I am able to add a decorator for the name argument, but if try to add multiple decorators I get the error:

Traceback (most recent call last): File "cli.py", line 50, in <module> @subcommand([argument("-p", "--phone", action="store", type=str, help="Contact phone number", required=True)]) File "cli.py", line 30, in decorator parser = parent.add_parser(func.__name__, description=func.__doc__) AttributeError: 'NoneType' object has no attribute '__name__'

any help would be greatly appreciated!
Thanks!

nvm, I figured it out, I wasn't paying close enough attention. I wasn't passing in all of the arguments in one list. It's working for me now.
This technique is the pretty awesome!

@NSBum
Copy link

NSBum commented Dec 31, 2020

It's a sensible approach.

What's not clear is how to deal with flags that pertain not to a single subcommand but to the entire app. In the example, you've attached the -d debug flag to the subcommand test; but what if one wanted to make the d flag applicable across all subcommands. For example:

cli.add_argument('-d', '--debug', action='store_true', help='Show debug information')

does not work (error: unrecognized arguments: -d)

@mivade
Copy link
Author

mivade commented Jan 2, 2021

As written there's currently no way to add an application-wide argument like that, but it shouldn't be too hard to modify things to allow for that. I would think in that case it would be best to define an Application class (or whatever name makes sense) which can be given app-wide options. Then the decorators would be applied with @app.subcommand.

@jamesyip22
Copy link

@mivade
could you post the class with the decorators? i'm still learning python and this would be a great example to understand. thank you!

@hornetmadness
Copy link

could the sudcommand() be expanded to support aliases?

@MestreLion
Copy link

For global arguments, as requested by @NSBum and @mivade , there are 2 approaches:

  • If they're truly global, just add them directly to the main parser (in this example, cli). Note that when using the CLI such args must be specified before the subcommand (./cli.py --global arg subcommand subcmdargs...)
  • If they're a useful set of args shared among many subcommands, create each set as a distinct ArgumentParser() instance and pass them as parents when creating the submmand. There are examples in the official docs. Something like this:
niceargs = ArgumentParser(add_help=False)
niceargs.add_argument(...)
niceargs.add_argument(...)
...
someargs = ArgumentParser(add_help=False)
someargsadd.add_argument(...)
...

# Adjust the subcommand decorator to take in parents
# None: in real world you should NEVER use `[]` (or any mutable) as default argument!
def subcommand(args=[], parent=subparsers, parents=[]):
    ...
    def decorator(func):
        parser = parent.add_parser(func.__name__, description=func.__doc__, parents=parents)
        ...

# Now each subcommand can specify their additional args:
@subcommand([argument("name", help="Name")], parents=[niceargs])
def name(args):
    print(args.name)

@subcommand([argument("extra", help="Complete")], parents=[niceargs, someargs])
def full(args):
    print(args.name)

@guludo
Copy link

guludo commented Nov 24, 2021

I recently developed a related python library for creating subcommands with decorator. Though it would be nice to share here: https://github.com/guludo/python-argparse-subdec

@innot
Copy link

innot commented Jun 19, 2022

Here is yet another python library for creating subcommands with decorators:

https://github.com/innot/argparseDecorator

While I started with this little gist the project grew quite more than intentioned to make it as simple as possible to use.

With this library all arguments are taken from the function signature (no add-argument() calls/decorators required) with some optional data in the docstring. Example:

from argparsedecorator import *
cli = ArgParseDecorator()

@cli.command
def add(values: OneOrMore[float], squared: Option = False) -> None:
    """
    Add up a list of numbers.
    :param values: one or more numbers
    :param squared: when present square each number first
    :alias squared: -sq
    """
    if squared:
        values = [x*x for x in values]
    print sum(values)

parser.execute("add --squared 1 2 3 4")    # output 30
parser.execute("help add")    # outputs help info for the add command

I have just released the first version, feel free to check it out and give feedback.

@Karosuo
Copy link

Karosuo commented Jan 21, 2023

I just came here to say, thanks for sharing this approach, I really liked it and found it very clean.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment