Last active
February 17, 2024 14:18
-
-
Save jirihnidek/3f5d36636198e852280f619847d22d9e to your computer and use it in GitHub Desktop.
Python example of using argparse sub-parser, sub-commands and sub-sub-commands
This file contains 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
""" | |
Example of using sub-parser, sub-commands and sub-sub-commands :-) | |
""" | |
import argparse | |
def main(args): | |
""" | |
Just do something | |
""" | |
print(args) | |
if __name__ == '__main__': | |
# create the top-level parser | |
parser = argparse.ArgumentParser(prog='PROG') | |
parser.add_argument('--foo', action='store_true', help='foo is great option') | |
# create sub-parser | |
sub_parsers = parser.add_subparsers(help='sub-command help') | |
# create the parser for the "ahoy" sub-command | |
parser_ahoy = sub_parsers.add_parser('ahoy', help='ahoy is cool sub-command') | |
parser_ahoy.add_argument('--bar', type=int, help='bar is useful option') | |
# create the parser for the "booo" sub-command | |
parser_booo = sub_parsers.add_parser('booo', help='booo is also cool sub-command') | |
parser_booo.add_argument('--baz', choices='XYZ', help='baz is another option') | |
parser_booo.add_argument('--zaz', action='store_const', const=True, help='zaz is French singer') | |
# create the parse for the "cool" sub-command | |
parser_cool = sub_parsers.add_parser('cool', help='cools is sub-command with sub-commands') | |
parser_cool.add_argument('--sas', type=str, help='sas are bad asses') | |
# create sub-parser for sub-command cool | |
cool_sub_parsers = parser_cool.add_subparsers(help='sub-sub-command help') | |
# create sub-command "crazy" for sub-command cool | |
parser_crazy = cool_sub_parsers.add_parser('crazy', help='it is crazy sub-sub-command') | |
parser_crazy.add_argument('--fool', action='store_const', const=True, help='it is foolish option') | |
parser_normal = cool_sub_parsers.add_parser('normal', help='it is normal sub-sub-command') | |
parser_normal.add_argument('--common', action='store_const', const=True, help='it is common option') | |
args = parser.parse_args() | |
main(args) |
But it seems that it has solution: https://stackoverflow.com/questions/10448200/how-to-parse-multiple-nested-sub-commands-using-python-argparse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is possible to use only one sub-command. Example