7

I want to use argeparse module in following way,

from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument('-b', dest='binKey', type=str) args = parser.parse_args() # I will make use of args.binKey option in this space print args.binKey # After that I want to add -d option to the arguments parser.add_argument('-d', dest='dir', type=str) args = parser.parse_args() 

With this example, I can only provide -b option as shown below in the help text.

$$ python test.py -h usage: test.py [-h] [-b BINKEY] optional arguments: -h, --help show this help message and exit -b BINKEY 

What I want is I should be able to provide both options while running this code and those options should also by visible in --help option

NOTE: I know that I can provide both options before executing parse_args() once, but that is not the way I want to use my parser.

6
  • You are not supposed to call parse_args() for each argument. You add all arguments at once, and then call parse_args() once after you defined all of them... Try going over the Argparse Tutorial to better understand how to use it Commented Aug 10, 2020 at 11:20
  • @Tomerikoo I want it in that way actually. I have defined ArgumentParser() object in other module and that object is imported in some other modules(to provide options for conditional statements). I want to make use of that already defined object and add customized extra arguments for the script I am using. Commented Aug 10, 2020 at 11:34
  • This sounds odd but you would have to make sure that parse_args is only called after you added all arguments you need Commented Aug 10, 2020 at 12:22
  • 3
    You can call parse_args multiple times (though a parse_known_args may be safer). But a -h will be caught by the first call (and exit). You could turn off the help when setting up the parser and add the '-h' argument yourself later. Commented Aug 10, 2020 at 16:11
  • @hpaulj, You are right. Could you help newbies like us writing an answer to that? I mean with more details like how to add help option later etc. Commented Aug 10, 2020 at 17:54

2 Answers 2

6

With the help from @hpaulj's comment, here is the resolution:

  • Turn off help option while creating ArgumentParser object.
  • You can call parse_known_args multiple times in between.
  • Add help option at the end.

Below is a snippet of the code

from argparse import ArgumentParser parser = ArgumentParser(add_help=False) parser.add_argument('-b', dest='binKey', type=str) args = parser.parse_known_args()[0] # Here you can make use of args.binKey option in this space print args.binKey # You can more options after this as parser.add_argument('-d', dest='dir', type=str) parser.add_argument('-h', '--help', action='help', default='==SUPPRESS==', help=('show this help message and exit')) args = parser.parse_args() 
Sign up to request clarification or add additional context in comments.

Comments

0

If all you want is to provide both options while running the code you just need to add

parser.add_argument('-d', dest='dir', type=str) 

before you execute parse_args(). If this doesn't solve your problem then you need to more explicitly explain why you need to add the argument afterwards.

1 Comment

I have defined ArgumentParser() object in other module and that object is imported in some other modules(to provide options for conditional statements). I want to make use of that already declared ArgumentParser() object and add customized extra add_argument() for the script I am using. Having said that I have to make sure that I can make use of default options of old arguments before adding newer one. That is why I was executing parse_args() twice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.