argparse

Cross-argument validation in argparse

穿精又带淫゛_ 提交于 2019-12-01 03:31:59
问题 I'm looking for a Pythonic way to validate arguments when their validation logically depends on the value(s) parsed from other argument(s). Here's a simple example: parser.add_argument( '--animal', choices=['raccoon', 'giraffe', 'snake'], default='raccoon', ) parser.add_argument( '--with-shoes', action='store_true', ) In this case, parsing this command should cause an error: my_script.py --animal snake --with-shoes Adding a mutually exclusive group doesn't seem to help here, as the other

Reorder Python argparse argument groups

喜欢而已 提交于 2019-12-01 03:30:29
I'm using argparse and I have a custom argument group required arguments . Is there any way to change the order of the argument groups in the help message? I think it is more logical to have the required arguments before optional arguments, but haven't found any documentation or questions to help. For example, changing this: usage: foo.py [-h] -i INPUT [-o OUTPUT] Foo optional arguments: -h, --help show this help message and exit -o OUTPUT, --output OUTPUT Output file name required arguments: -i INPUT, --input INPUT Input file name to this: usage: foo.py [-h] -i INPUT [-o OUTPUT] Foo required

Passing integer lists to python

一世执手 提交于 2019-12-01 03:03:55
I want to pass 2 lists of integers as input to a python program. For e.g, (from command line) python test.py --a 1 2 3 4 5 -b 1 2 The integers in this list can range from 1-50, List 2 is subset of List1. Any help/suggestions ? Is argparse the right module ? Any concerns in using that ? I have tried : import argparse if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--a', help='Enter list 1 ') parser.add_argument('--b', help='Enter list 2 ') args = parser.parse_args() print (args.a) You can pass them as strings than convert to lists. You can use argparse or

Disable unique prefix matches for argparse and optparse

你。 提交于 2019-12-01 02:56:25
When I use Python's argparse or optparse command line argument parser, any unique prefix of an argument is considered valid, e.g. $ ./buildall.py --help usage: buildall.py [-h] [-f] Build all repositories optional arguments: -h, --help show this help message and exit -f, --force Build dirty repositories works with --help , --hel , --he for the help option as well as --forc and --fo for the force option. Can this behavior be turned off somehow? I want to get an error message for incomplete arguments. The ability to disable abbreviated long options was only added in Python 3.5. From the argparse

ArgumentParser: Optional argument with optional value

我的梦境 提交于 2019-12-01 02:42:39
问题 If I have an optional argument with optional argument value, is there a way to validate if the argument is set when the value is not given? For instance: parser = argparse.ArgumentParser() parser.add_argument('--abc', nargs='?') args = parser.parse_args() Would correctly give me: optional arguments: --abc [ABC] How do I distinguish between 1 and 2 below? '' => args.abc is None '--abc' => args.abc is still None '--abc something' => args.abc is something ... Update: Found a trick to solve this

How to use python argparse with args other than sys.argv?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 02:28:28
I've been all over the documentation and it seems like there's no way to do it, but: Is there a way to use argparse with any list of strings, instead of only with sys.argv? Here's my problem: I have a program which looks something like this: # This file is program1.py import argparse def main(argv): parser = argparse.ArgumentParser() # Do some argument parsing if __name__ == '__main__': main(sys.argv) This works fine when this program is called straight from the command line. However, I have another python script which runs batch versions of this script with different commandline arguments,

Disable/Remove argument in argparse

冷暖自知 提交于 2019-12-01 02:25:18
Is it possible to remove or disable an argument in argparse, such that it does not show in the help? How? It is easy to add new arguments: parser = argparse.ArgumentParser() parser.add_argument('--arg1', help='Argument 1') parser.add_argument('--arg2', help='A second one') And I know you can override arguments with a new definition by specifying the "resolve" conflict handler: #In one script that should stand-alone and include arg1: parser = argparse.ArgumentParser(conflict_handler='resolve') parser.add_argument('--arg1', help='Argument 1') parser.add_argument('--arg2', help='A second one')

python unittest for argparse

醉酒当歌 提交于 2019-12-01 01:01:54
I have a function inside a module that creates an argparse : def get_options(prog_version='1.0', prog_usage='', misc_opts=None): options = [] if misc_opts is None else misc_opts parser = ArgumentParser(usage=prog_usage) if prog_usage else ArgumentParser() parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(prog_version)) parser.add_argument('-c', '--config', dest='config', required=True, help='the path to the configuration file') for option in options: if 'option' in option and 'destination' in option: parser.add_argument(option['option'], dest=option.get(

Is it possible to only parse one argument group's parameters with argparse?

ⅰ亾dé卋堺 提交于 2019-12-01 00:38:11
I'm looking to do something like this: parser = argparse.ArgumentParser() group1 = parser.add_argument_group('group1') group1.add_argument('--test1', help="test1") group2 = parser.add_argument_group('group2') group2.add_argument('--test2', help="test2") group1_args = group1.parse_args() group2_args = group2.parse_args() However, I'm getting the following error: Traceback (most recent call last): File "test.py", line 19, in <module> group1_args = group1.parse_args() AttributeError: '_ArgumentGroup' object has no attribute 'parse_args' Is there a way to only get the arguments for one argument

Reorder Python argparse argument groups

我怕爱的太早我们不能终老 提交于 2019-12-01 00:24:38
问题 I'm using argparse and I have a custom argument group required arguments . Is there any way to change the order of the argument groups in the help message? I think it is more logical to have the required arguments before optional arguments, but haven't found any documentation or questions to help. For example, changing this: usage: foo.py [-h] -i INPUT [-o OUTPUT] Foo optional arguments: -h, --help show this help message and exit -o OUTPUT, --output OUTPUT Output file name required arguments: