Argparse seems repititive - help to refactor this code.

151 views
Skip to first unread message

Kashif Ali

unread,
Apr 10, 2013, 5:49:11 PM4/10/13
to argpars...@googlegroups.com
Hi,

I am using argparse for the first time, and wrote the following, code, but it is very repetitive, can someone help refactor:


    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)


Regards


Kash

Ted Stern

unread,
Apr 15, 2013, 5:52:45 PM4/15/13
to argpars...@googlegroups.com
Hi Kashif,

I recommend installing the argh package, which sets up decorators and
other routines to help you solve exactly this problem.

https://pypi.python.org/pypi/argh

You should be able to install using

pip install argh

if you have pip set up. The documentation is here. Note that the
argument syntax changed a little bit between 0.19 and 0.20, so be sure
you're reading the documentation for the latest version, 0.23.1 or later.

Ted
> --
> You received this message because you are subscribed to the Google Groups
> "argparse-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to argparse-user...@googlegroups.com.
> To post to this group, send email to argpars...@googlegroups.com.
> Visit this group at http://groups.google.com/group/argparse-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>

--
Frango ut patefaciam -- I break so that I may reveal
Reply all
Reply to author
Forward
0 new messages