1

Hi I'm using ArgParse to handle my arguments. I would like the code to work like this

# Main function
$ myApp -i INPUT -o OUTPUT -s STUFF 

# Configure function
$ myApp config -a conf1 -b conf2  

import argparse
from argparse import RawTextHelpFormatter

parser = argparse.ArgumentParser(description='myApp',formatter_class=RawTextHelpFormatter) 
parser.add_argument('-i',help='input',required=True)
parser.add_argument('-o',help='output',required=True)
parser.add_argument('-s',help='stuff',default=None,required=False)
args = parser.parse_args()

subp = parser.add_subparsers()
conf_parser = subp.add_parser('config', help='configure')
conf_parser.add_argument('-a',help='a config file',default=None,required=False)
conf_parser.add_argument('-b',help='b config file',default=None,required=False)
conf_args = conf_arser.parse_args()

Here's the output

python sandbox/test1.py  --help
usage: test1.py [-h] -i I -o O [-s S]

myApp

optional arguments:
  -h, --help  show this help message and exit
  -i I        input
  -o O        output
  -s S        stuff

I'm not getting the config args to show. I'm not sure what I'm doing wrong here.

Thanks!

1 Answer 1

3

Solved it


import argparse
from argparse import RawTextHelpFormatter

parser = argparse.ArgumentParser(description='myApp',formatter_class=RawTextHelpFormatter)
parser.add_argument('-i',help='input',required=True)
parser.add_argument('-o',help='output',required=True)
parser.add_argument('-s',help='stuff',default=None,required=False)

subp = parser.add_subparsers(help='configure')
conf_parser = subp.add_parser('config')
conf_parser.add_argument('-a',help='a config file',default=None,required=False)
conf_parser.add_argument('-b',help='b config file',default=None,required=False)
args = parser.parse_args()

python sandbox/test1.py --help
usage: test1.py [-h] -i I -o O [-s S] {config} ...

myApp

positional arguments:
  {config}    configure

optional arguments:
  -h, --help  show this help message and exit
  -i I        input
  -o O        output
  -s S        stuff

python sandbox/test1.py config --help
usage: test1.py config [-h] [-a A] [-b B]

optional arguments:
  -h, --help  show this help message and exit
  -a A        a config file
  -b B        b config file
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.