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:
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.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)
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 ():
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.
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
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"]
.
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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With