Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get back the option string using argparse?

parser = argparse.ArgumentParser()
parser.add_argument("-p", "--pattern", help="Pattern file")
args = parser.parse_args()

Now is it possible to get back the string "--pattern" from args? I need the string so that I can construct a cmd list to pass to Popen like Popen(['some_other_program', args.pattern.option_string, args.pattern], ...) without repeating it (and having to maintain it in two places) (Popen(['some_other_prog', '--pattern', args.pattern], ...)).

I need to create a wrapper for another program. Some of the args need to be passed to the wrapped program (via Popen) and some are required by the wrapper.


Is there a better method than the following example?

pass_1 = '--to-be-passed'
parser = argparse.ArgumentParser()
parser.add_argument("-p", pass_1, help="Pass me on")
parser.add_argument("-k", "--arg-for-wrapper")
args = parser.parse_args()

...
process = Popen(['wrapped_program', pass_1, args.pass_1], ...)
... 

This method of keeping the args in variables is not very good as:

  1. Maintaining short options along with long options becomes difficult.
  2. Popen if called in another function requires passing these variables(or a dict of them) to the function. This seems redundant as args passed to it should be sufficient.
like image 610
user80551 Avatar asked Jun 07 '15 04:06

user80551


People also ask

How do I use argparse in Python?

Argparse. To start using the argparse module, we first have to import it. import argparse parser = argparse.ArgumentParser() parser.parse_args() Run the code. Run the code with the –help option (Running the script without any options results in nothing displayed to stdout)

How do I add optional arguments to argparse?

In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line. To make an option required, True can be specified for the required= keyword argument to add_argument ():

How do I get the original option strings from a parser?

There doesn't seem to be an easy way to get the original option strings from the result of a parser.parse_args (), but you can get them from the parser object. You just need to peek into its __dict__, in order to retrieve the parser settings after it's created. In your case you want the _option_string_actions field.

How to display the help text of the ls command using argparse?

The “-l” is knowns as an “optional argument” If you want to display the help text of the ls command, you would type “ls –help” Argparse To start using the argparse module, we first have to import it. import argparse parser = argparse.ArgumentParser() parser.parse_args() Run the code


2 Answers

Add a dest to your add_argument call.

parser.add_argmument("p", "--pattern", dest="pattern", help="your help text")
args = parser.parse_args()
args = vars(args)

The you can reference the pattern with args["pattern"] .

like image 190
Eric Hydrick Avatar answered Oct 24 '22 22:10

Eric Hydrick


There doesn't seem to be an easy way to get the original option strings from the result of a parser.parse_args(), but you can get them from the parser object. You just need to peek into its __dict__, in order to retrieve the parser settings after it's created. In your case you want the _option_string_actions field. Unfortunately this doesn't seem officially supported, as I couldn't find a ArgumentParser method dedicated to this, so YMMV. On Python 3:

Demo:

parser = argparse.ArgumentParser()
parser.add_argument('--foo', '-f', type=int, default=1000, help='intensity of bar')
parser.add_argument('--bar', '-b', type=str, default='bla', help='whatever')
store_action_dict=vars(parser)['_option_string_actions']
print(store_action_dict.keys())    # dict_keys(['--help', '-b', '-f', '-h', '--foo', '--bar'])
like image 26
Yibo Yang Avatar answered Oct 24 '22 20:10

Yibo Yang