shahakshay94 shahakshay94 - 3 years ago 217
Python Question

Passing Sub-parsers with argparser in python

Basically what I am trying to achieve is this :

python (get|post) [-v] (-h "k:v")* [-d inline-data] [-f file] URL

Now, what I did was something like this :

parser.add_argument('get', help='Get executes a HTTP GET request for a given URL.', default='')

But it's not working. I did some SO and these are the other links which I tried but desired output was not achieved

Python argparse optional sub-arguments

How to have sub-parser arguments in separate namespace with argparse?

Answer Source

This parser produces a similar usage line:

import argparse

parser = argparse.ArgumentParser(prog='')
parser.add_argument("gp", choices=['get','post'])
parser.add_argument('-v', action='version', version='0.0.1')
parser.add_argument('--how', action='append',help='k:v, may repeat')
parser.add_argument('-d', metavar='inline-data')


sample help with usage. Note that the positionals are displayed at the end, but may be interspersed with the optionals:

1356:~/mypy$ python -h
usage: [-h] [-v] [--how HOW] [-d inline-data] [-f FILE]
                      {get,post} url

positional arguments:

optional arguments:
  -h, --help            show this help message and exit
  -v                    show program's version number and exit
  --how HOW             k:v, may repeat
  -d inline-data
  -f FILE, --file FILE

I assume your -v is supposed to be version, though -v is also used for a verbosity flag.

1357:~/mypy$ python -v

example with get/post, several how, and required url

1357:~/mypy$ python get --how 3:3 --how a:b aurl
Namespace(d=None, file=None, gp='get', how=['3:3', 'a:b'], url='aurl')

argparse does not parse k:v strings for you. You get to do that after parsing. I assume the (-h "k:v")* means you want to enter several of the k:v pairs. nargs='*' is alternative to the action='append'.

I defined the simple gp positional with a choices. That restricts the input to these 2 strings. So far in your description I don't see a need for subparsers.

In [210]: args=argparse.Namespace(d=None, file=None, gp='get', how=['3:3', 'a:b'
     ...: ], url='aurl')
In [211]: args
Out[211]: Namespace(d=None, file=None, gp='get', how=['3:3', 'a:b'], url='aurl')
In [212]: vars(args)
Out[212]: {'d': None, 'file': None, 'gp': 'get', 'how': ['3:3', 'a:b'], 'url': 'aurl'}
In [213]: for k in args.__dict__:
     ...:     print(k, args.__dict__[k])
file None
d None
url aurl
gp get
how ['3:3', 'a:b']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download