argparse

Can argparse in python 2.7 be told to require a minimum of TWO arguments?

荒凉一梦 提交于 2019-12-30 09:46:27
问题 My application is a specialized file comparison utility and obviously it does not make sense to compare only one file, so nargs='+' is not quite appropriate. nargs=N only excepts a maximum of N arguments, but I need to accept an infinite number of arguments as long as there are at least two of them. 回答1: Short answer is you can't do that because nargs doesn't support something like '2+'. Long answer is you can workaround that using something like this: parser = argparse.ArgumentParser(usage='

Disable unique prefix matches for argparse and optparse

老子叫甜甜 提交于 2019-12-30 08:04:19
问题 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

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

早过忘川 提交于 2019-12-30 07:26:07
问题 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: '

Catching ArgumentTypeError exception from custom action

人走茶凉 提交于 2019-12-30 06:16:08
问题 What is the best practice to throw an ArgumentTypeError exception from my own custom action and let the argparse to catch it for me? It seems that argparse's try/except block does not handle this exception for my custom actions. Though it does that just fine for its built-in actions. class unique_server_endpoints(argparse.Action): """This class avoids having two duplicate OuterIPs in the client argument list""" def __call__(self, parser, namespace, values, option_string=None): ips = set()

Python argparse: command-line argument that can be either named or positional

本秂侑毒 提交于 2019-12-30 06:01:39
问题 I am trying to make a Python program that uses the argparse module to parse command-line options. I want to make an optional argument that can either be named or positional. For example, I want myScript --username=batman to do the same thing as myScript batman . I also want myScript without a username to be valid. Is this possible? If so, how can it be done? I tried various things similar to the code below without any success. parser = argparse.ArgumentParser() group = parser.add_mutually

argparse argument dependency

雨燕双飞 提交于 2019-12-30 05:50:08
问题 If I call the script below with these options: --user u1 --password p1 --foo f1 --user u2 --user u3 --password p3 Then it will print: Namespace(foo=['bar', 'f1'], password=['p1', 'p3'], user=['u1', 'u2', 'u3']) Question: Is there any way for me to set up a dependency between user and password, so it throws an error, because password for user u2 is not specified? Less relevant question: How do I specify a default foo value for all users? With the given input I would like foo to equal ['f1',

argparse with required subcommands

这一生的挚爱 提交于 2019-12-30 05:32:08
问题 With python's argparse, how do I make a subcommand a required argument? I want to do this because I want argparse to error out if a subcommand is not specified. I override the error method to print help instead. I have 3-deep nested subcommands, so it's not a matter of simply handling zero arguments at the top level. In the following example, if this is called like so, I get: $./simple.py $ What I want it to do instead is for argparse to complain that the required subcommand was not specified

argparse help without duplicate ALLCAPS

a 夏天 提交于 2019-12-30 03:49:47
问题 I'd like to display argparse help for my options the same way the default -h , --help and -v , --version are, without the ALLCAPS text after the option, or at least without the duplicated CAPS. import argparse p = argparse.ArgumentParser("a foo bar dustup") p.add_argument('-i', '--ini', help="use alternate ini file") print '\n', p.parse_args() This is what I currently get with python foobar.py -h : usage: a foo bar dustup [-h] [-i INI] optional arguments: -h, --help show this help message and

Handle spaces in argparse input

為{幸葍}努か 提交于 2019-12-30 01:36:12
问题 Using python and argparse, the user could input a file name with -d as the flag. parser.add_argument("-d", "--dmp", default=None) However, this failed when the path included spaces. E.g. -d C:\SMTHNG\Name with spaces\MORE\file.csv NOTE: the spaces would cause an error (flag only takes in 'C:SMTHNG\Name' as input). error: unrecognized arguments: with spaces\MORE\file.csv Took me longer than it should have to find the solution to this problem... (did not find a Q&A for it so I'm making my own

python 参数定义库argparse

血红的双手。 提交于 2019-12-29 19:58:05
python 参数定义库argparse 这一块的官方文档在 这里 注意到这个库是因为argparse在IDE中和在ipython notebook中使用是有差异的,习惯了再IDE里面用,转到ipython中会报错,究其原因,还是对库的本质不够理解。 打开argparse.py,里面有很多class,但是,实际笨妞貌似只用过ArgumentParser。ArgumentParser是用来创建argparse类的。 一般的应用过程是这样的: import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() print(args