shahakshay94
shahakshay94

Reputation: 145

Passing Sub-parsers with argparser in python

Basically what I am trying to achieve is this :

python http_client.py (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='http://httpbin.org/get')

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?

Upvotes: 0

Views: 342

Answers (1)

hpaulj
hpaulj

Reputation: 231375

This parser produces a similar usage line:

import argparse

parser = argparse.ArgumentParser(prog='http_client.py')
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')
parser.add_argument('-f','--file')
parser.add_argument('url')

print(parser.parse_args())

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

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

positional arguments:
  {get,post}
  url

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 stack46357414.py -v
0.0.1

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

1357:~/mypy$ python stack46357414.py 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']

Upvotes: 1

Related Questions