-
-
Save mivade/384c2c41c3a29c637cb6c603d4197f9f to your computer and use it in GitHub Desktop.
"""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) |
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.
I just came here to say, thanks for sharing this approach, I really liked it and found it very clean.
Thanks!
could the sudcommand() be expanded to support aliases?
Adding support for aliases can be accomplished by adding another argument to subcommand
. Here's an example:
def subcommand(args=[], parent=subparsers, aliases=[]):
def decorator(func):
parser = parent.add_parser(func.__name__, description=func.__doc__, aliases=aliases)
...
@subcommand([argument("-d", help="Debug mode", action="store_true")], aliases=['t'])
def test(args):
...
Why doesn't this work with @functools.wraps
? See https://stackoverflow.com/a/41377289/8652920 for reference. When I do this to the subcommand
decorator, everything breaks.
Thanks for sharing!
A modified version for those who prefer dedicated decorator for each argument (more readable IMHO). Perhaps somewhat awkward by creating new attribute __arguments__
but hopefully someone may share a cleaner solution.
import argparse
cli = argparse.ArgumentParser()
subparsers = cli.add_subparsers(dest="command")
def command(*args, parent=subparsers, **kwargs):
def decorator(func):
parser = parent.add_parser(*args, **kwargs)
parser.set_defaults(func=func)
for arg in getattr(func, "__arguments__", []):
parser.add_argument(*arg[0], **arg[1])
return func
return decorator
def argument(*name_or_flags, **kwargs):
def decorator(func):
if not hasattr(func, "__arguments__"):
func.__arguments__ = []
func.__arguments__.append((name_or_flags, kwargs))
return func
return decorator
@command(
"nothing",
help="help for nothing command",
)
def nothing_handler(_):
print("Nothing special!")
@command(
"the_command",
help="help for the command",
)
@argument(
"arg",
type=str,
help="random arg to provide",
)
@argument(
"-f",
"--flag",
action="store_true",
help="random flag to set",
)
def the_command_handler(args):
print(f"running 'the_command' with arg: {args.arg}, flag: {args.flag},")
def main():
args = cli.parse_args()
if args.command is None:
cli.print_help()
else:
args.func(args)
if __name__ == "__main__":
main()
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