Last active
July 13, 2020 10:43
-
-
Save lene/dd54e8f951607e98cabba1e8a53a71dc to your computer and use it in GitHub Desktop.
argparse subparser easy use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def command(args): | |
pass | |
parser = argparse.ArgumentParser() | |
subparser = parser.add_subparsers(title="subcommands") | |
command_parser = subparser.add_parser("command") | |
command_parser.set_defaults(func=command) | |
command_parser.add_argument("--dummy", help="some arg to the subcommand") | |
args = parser.parse_args() | |
if not hasattr(args, "func"): | |
print("you need to specify a subcommand", file=sys.stderr) | |
parser.print_help() | |
sys.exit(1) | |
args.func(args) | |
""" | |
usage: | |
$ python subparser.py command --dummy "something" | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment