0

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?

shahakshay94
  • 145
  • 1
  • 2
  • 13
  • Usually `argparse` questions need the full `parser` definition, not just one line. Also show the commandline(s) that you are testing, and the resulting error(s). Your `add_argument` so far just defines a positional with a `dest='get'`. – hpaulj Sep 22 '17 at 05:33
  • While `subparsers` are explained in the docs (forget that link - it's too advanced), I don't know what you mean by `sub-arguments`. I don't see how the `sub-argument` link helps. It's not asking for the same thing. – hpaulj Sep 22 '17 at 05:36
  • 1
    Watch out for that `-h`. By default a parser already has a `-h` help definition. – hpaulj Sep 22 '17 at 05:39

1 Answers1

1

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']
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • somwhow I am getting trouble accessing the values of it. This is what I am trying to do ----->>> for k in args.__dict__: print (k, args.__dict__[k]) – shahakshay94 Oct 01 '17 at 16:02