argparse

How does argparse (and the deprecated optparse) respond to 'tab' keypress after python program name, in bash?

谁都会走 提交于 2019-11-27 13:13:03
问题 I have tested optcomplete working with the optparse module. Its example is a simple file so I could get that working. I also tested it using the argparse module as the prior one is deprecated. But I really do not understand how and by whom the python program gets called on tab presses. I suspect bash together with the shebang line and the argparse (or optparse ) module are involved in some way. I have been trying to figure this out (now gonna read the source code). I have a little more

Conditional command line arguments in Python using argparse

早过忘川 提交于 2019-11-27 12:47:33
问题 I'd like to have a program that takes a --action= flag, where the valid choices are dump and upload , with upload being the default. If (and only if) dump is selected, I'd like there to also be a --dump-format= option. Is there a way to express this using argparse, or do I need to just accept all the arguments and do the logic myself. 回答1: You could use parser.error: import argparse parser = argparse.ArgumentParser() parser.add_argument('--action', choices=['upload', 'dump'], default='dump')

Using the same option multiple times in Python's argparse

不打扰是莪最后的温柔 提交于 2019-11-27 12:38:32
问题 I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this ./my_script.py \ -i input1_url input1_name input1_other_var \ -i input2_url input2_name input2_other_var \ -i input3_url input3_name # notice inputX_other_var is optional But I can't quite figure out how to do this using argparse . It seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option ( nargs='*

Specify format for input arguments argparse python

夙愿已清 提交于 2019-11-27 11:31:07
I have a python script that requires some command line inputs and I am using argparse for parsing them. I found the documentation a bit confusing and couldn't find a way to check for a format in the input parameters. What I mean by checking format is explained with this example script: parser.add_argument('-s', "--startdate", help="The Start Date - format YYYY-MM-DD ", required=True) parser.add_argument('-e', "--enddate", help="The End Date format YYYY-MM-DD (Inclusive)", required=True) parser.add_argument('-a', "--accountid", type=int, help='Account ID for the account for which data is

Python argparse: Make at least one argument required

倖福魔咒の 提交于 2019-11-27 10:24:26
问题 I've been using argparse for a Python program that can -process , -upload or both: parser = argparse.ArgumentParser(description='Log archiver arguments.') parser.add_argument('-process', action='store_true') parser.add_argument('-upload', action='store_true') args = parser.parse_args() The program is meaningless without at least one parameter. How can I configure argparse to force at least one parameter to be chosen? UPDATE: Following the comments: What's the Pythonic way to parametrize a

Python argparse and controlling/overriding the exit status code

痞子三分冷 提交于 2019-11-27 10:17:32
问题 Apart from tinkering with the argparse source, is there any way to control the exit status code should there be a problem when parse_args() is called, for example, a missing required switch? 回答1: I'm not aware of any mechanism to specify an exit code on a per-argument basis. You can catch the SystemExit exception raised on .parse_args() but I'm not sure how you would then ascertain what specifically caused the error. EDIT: For anyone coming to this looking for a practical solution, the

Argparse: how to handle variable number of arguments (nargs='*')

廉价感情. 提交于 2019-11-27 10:15:04
问题 I thought that nargs='*' was enough to handle a variable number of arguments. Apparently it's not, and I don't understand the cause of this error. The code: p = argparse.ArgumentParser() p.add_argument('pos') p.add_argument('foo') p.add_argument('--spam', default=24, type=int, dest='spam') p.add_argument('vars', nargs='*') p.parse_args('1 2 --spam 8 8 9'.split()) I think the resulting namespace should be Namespace(pos='1', foo='2', spam='8', vars=['8', '9']) . Instead, argparse gives this

Argparse - do not catch positional arguments with `nargs`.

霸气de小男生 提交于 2019-11-27 09:15:21
I am trying to write a function wo which you can parse a variable amount of arguments via argparse - I know I can do this via nargs="+" . Sadly, the way argparse help works (and the way people generally write arguments in the CLI) puts the positional arguments last. This leads to my positional argument being caught as part of the optional arguments. #!/usr/bin/python import argparse parser = argparse.ArgumentParser() parser.add_argument("positional", help="my positional arg", type=int) parser.add_argument("-o", "--optional", help="my optional arg", nargs='+', type=float) args = parser.parse

Python argparse command line flags without arguments

﹥>﹥吖頭↗ 提交于 2019-11-27 09:07:25
问题 How do I add an optional flag to my command line args? eg. so I can write python myprog.py or python myprog.py -w I tried parser.add_argument('-w') But I just get an error message saying Usage [-w W] error: argument -w: expected one argument which I take it means that it wants an argument value for the -w option. What's the way of just accepting a flag? I'm finding http://docs.python.org/library/argparse.html rather opaque on this question. 回答1: As you have it, the argument w is expecting a

Argparse: Required arguments listed under “optional arguments”?

情到浓时终转凉″ 提交于 2019-11-27 08:59:39
问题 I use the following simple code to parse some arguments; note that one of them is required. Unfortunately, when the user runs the script without providing the argument, the displayed usage/help text does not indicate that there is a non-optional argument, which I find very confusing. How can I get python to indicate that an argument is not optional? Here is the code: import argparse if __name__ == '__main__': parser = argparse.ArgumentParser( description='Foo') parser.add_argument('-i','-