Argparse inherit options from parent (or common options)

87 views
Skip to first unread message

Kashif Ali

unread,
Apr 10, 2013, 3:30:39 PM4/10/13
to argpars...@googlegroups.com
Hi,

I am new to argparse and just started using it today. However what I wrote started to become very long and repetitive and I was wondering if there was a better way of doing this:



  parser = argparse.ArgumentParser(description="""Create, Search, Delete

    DNS records or get next free IP address(es)""")


    subparsers = parser.add_subparsers(title='sub-commands',

                                       description='valid sub-commands',

                                       help="""additional help 'sub-command

                                       -h'""")


    parser_create = subparsers.add_parser('create', help="""Create a

    DNS record(s)""")


    parser_create.add_argument('-u', '--username', help="""Required argument:

    Infoblox User name for the authentication.""", dest='username',

    required=True)


    parser_create.add_argument('-p', '--password', help="""Required argument:

    Infoblox User password for the authentication.""", dest='password',

    required=True)


    parser_create.add_argument('-a', '--address', help="""Required argument:

    Infoblox Web API service host address.""", dest='address',

    required=True)


    parser_create.add_argument('-f', '--fqdn', help="""Required argument:

    Fully Qualified Domain Name of Host: Operation with: create,

    delete or search.""", dest='fqdn', required=True)


    parser_create.add_argument('-n', '--network', help="""Required argument:

    Network Subnet in slash format: Example: 10.2.3.4/24""", dest='network',

    required=True)


    parser_create.add_argument('-c', '--count', help="""Number of IP addresses

    required""", dest='count', default=1, type=int)


    parser_delete = subparsers.add_parser('delete', help="""Delete a DNS

    record""")


    parser_delete.add_argument('-u', '--username', help="""Required argument:

    Infoblox User name for the authentication.""", dest='username',

    required=True)


    parser_delete.add_argument('-p', '--password', help="""Required argument:

    Infoblox User password for the authentication.""", dest='password',

    required=True)


    parser_delete.add_argument('-a', '--address', help="""Required argument:

    Infoblox Web API service host address.""", dest='address',

    required=True)


    parser_delete.add_argument('-f', '--fqdn', help="""Required argument:

    Fully Qualified Domain Name of Host: Operation with: create,

    delete or search.""", dest='fqdn', required=True)


    parser_search = subparsers.add_parser('search', help="""Search for a DNS

    record""")


    parser_search.add_argument('-u', '--username', help="""Required argument:

    Infoblox User name for the authentication.""", dest='username',

    required=True)


    parser_search.add_argument('-p', '--password', help="""Required argument:

    Infoblox User password for the authentication.""", dest='password',

    required=True)


    parser_search.add_argument('-a', '--address', help="""Required argument:

    Infoblox Web API service host address.""", dest='address',

    required=True)


    parser_search.add_argument('-f', '--fqdn', help="""Required argument:

    Fully Qualified Domain Name of Host: Operation with: create,

    delete or search.""", dest='fqdn', required=True)


    parser_nextip = subparsers.add_parser('nextip', help="""Next free IP

    address(es)""")


    parser_nextip.add_argument('-u', '--username', help="""Required argument:

    Infoblox User name for the authentication.""", dest='username',

    required=True)


    parser_nextip.add_argument('-p', '--password', help="""Required argument:

    Infoblox User password for the authentication.""", dest='password',

    required=True)


    parser_nextip.add_argument('-a', '--address', help="""Required argument:

    Infoblox Web API service host address.""", dest='address',

    required=True)


    parser_nextip.add_argument('-n', '--network', help="""Required argument:

    Network Subnet in slash format: Example: 10.2.3.4/24""", dest='network',

    required=True)


    parser_nextip.add_argument('-c', '--count', help="""Number of IP addresses

    required""", dest='count', default=1, type=int, required=True)



To explain this more simply, EVERY subcommand needs the options:


optional arguments:


  -u USERNAME, --username USERNAME

                        Required argument: Infoblox User name for the

                        authentication.

  -p PASSWORD, --password PASSWORD

                        Required argument: Infoblox User password for the

                        authentication.

  -a ADDRESS, --address ADDRESS

                        Required argument: Infoblox Web API service host

                        address.

so I was wondering if these could be taken from parent? etc....


Regards


Kash

Reply all
Reply to author
Forward
0 new messages