Skip to content

Instantly share code, notes, and snippets.

@xiucz
Last active November 8, 2022 05:54
Show Gist options
  • Save xiucz/a29bfc5912e37367a7ec5a993de81cd5 to your computer and use it in GitHub Desktop.
Save xiucz/a29bfc5912e37367a7ec5a993de81cd5 to your computer and use it in GitHub Desktop.

first

"""
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)


"""
[jiri@localhost ~]$ python ./subcommands.py cool crazy --fool normal --common
usage: PROG [-h] [--foo] {ahoy,booo,cool} ...
PROG: error: unrecognized arguments: normal --common

[jiri@localhost ~]$ python ./subcommands.py cool crazy --fool
Namespace(foo=False, sas=None, fool=True)

[jiri@localhost ~]$ python ./subcommands.py cool normal --common
Namespace(foo=False, sas=None, common=True)
"""

second

import argparse
parsers = argparse.ArgumentParser(prog="myProg") #add_help=False,  to avoid problems with conflicting help argument.

parsers.add_argument("-v", "--verbose", action="store_true", dest="VERBOSE", help="run in verbose mode")

subparsers = parsers.add_subparsers(dest='cmd')

def create(name):
    """
 
    :param name: Name of index
    :return: None
    """
    print("Creating index with name: %s" % name)

def delete(name):
    """
 
    :param name: Name of index
    :return: None
    """
    print("Deleting index with name: %s" % name)

parent_parsers = argparse.ArgumentParser(add_help=False)
parent_parsers.add_argument('-x', help='add db parameter', required=True)

#sub-sub-cmd1
parser_create = subparsers.add_parser ("create", parents=[parent_parsers], help = "create the orbix environment")
parser_create.add_argument ("-p", type = int, help = "set db parameter")
parser_create.set_defaults(function=create)

#sub-sub-cmd2
parser_delete = subparsers.add_parser("delete", parents=[parent_parsers], add_help=False, description="The update parser", usage='''
    $ python indices.py delete [<args>]
''')

parser_delete.add_argument('index', type=str, help='name of index')
parser_delete.set_defaults(function=delete)

parsers.print_help()

#args = parser.parse_args()


##
#https://stackoverflow.com/questions/10448200/how-to-parse-multiple-nested-sub-commands-using-python-argparse

third

import argparse

parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument('--debug', default=False, required=False,
                        action='store_true', dest="debug", help='debug flag')

main_parser = argparse.ArgumentParser()

service_subparsers = main_parser.add_subparsers(title="service",
                    dest="service_command")


service_parser = service_subparsers.add_parser("first", help="first",
                    parents=[parent_parser])
action_subparser = service_parser.add_subparsers(title="action",
                    dest="action_command")
action_parser = action_subparser.add_parser("second", help="second",
                    parents=[parent_parser])

args = main_parser.parse_args()

third.one

import argparse

parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument('--debug', default=False, required=False,
                        action='store_true', dest="debug", help='debug flag')

main_parser = argparse.ArgumentParser()

service_subparsers = main_parser.add_subparsers(title="service",
                    dest="service_command")

#test
service_parser = service_subparsers.add_parser("first", help="first",
                    parents=[parent_parser])
action_subparser = service_parser.add_subparsers(title="action",
                    dest="action_command")
action_parser = action_subparser.add_parser("second", help="second",
                    parents=[parent_parser])

#blast
parsers_blast = service_subparsers.add_parser("blast", help="blast",
                    parents=[parent_parser])
subparsers_hg = parsers_blast.add_subparsers(title="action",
                    dest="action_command")
action_parser = subparsers_hg.add_parser("hg", help="blast->hg",
                    parents=[parent_parser])

def hg():
    print("hg function")

args = main_parser.parse_args()

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