我希望围绕"arg1“"arg2"的引号被认为是单独的参数,而不是在同一个列表中,"arg1 arg2"作为单独的参数,而是在相同的列表中。
test.py取材于
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--nargs', nargs='*')
for _, value in parser.parse_args()._get_kwargs():
if value is not None:
print(val
我正在编写一个python程序,它主要调用另一个程序,比如watch程序。在watch -n1 date -u中,只有-n1是watch的选项(-u是date的选项,watch是位置参数)。getopt.getopt在python中提供了这样的解析。but解析更容易使用,但它的解析类似于getopt.gnu_getopt。如何在How解析中启用getopt.getopt-like解析?否则,是否有一个常用的参数解析包可供我使用?
我有:
from argparse import ArgumentParser
parser = ArgumentParser(description='Test')
parser.add_argument("command",
help="the command to be executed",
choices=["dump", "delete", "update", "set"],
d
我在Python3中使用argparse。我的要求是支持以下3种用例:
$ python3 foo.py --test <== results e.g. in True
$ python3 foo.py --test=foo <== results in foo
$ python3 foo.py <== results in arg.test is None or False
我找到了store_true和store,但是找不到任何可以实现上面提到的arg列表的配置。它要么想要一个参数,要么不想。两者似乎都不起作用。有什么方法可以让争
现在,我的脚本通过以下方式调用:
python resylter.py -n *newfile* -o *oldfile*
代码看起来像这样:
parser.add_argument('-n', '--newfile', help='Uses only with -o argument. Compares inputed OLD (-o) file with previous run results with NEW(-n) output.xml file with actual run results')
parser.add_argument
对于我所有的python代码,我定义了一个程序参数字典,然后all解析在解析调用序列时使用它来设置命令行参数和参数类型:
import argparse
def get_args(pars):
parser = argparse.ArgumentParser(description='Dynamic arguments')
# add each key of the default dictionary as an argument expecting the same type
for key,val in pars.items():
当前,我创建的--resize标志是布尔值,这意味着我的所有对象都将被调整大小:
parser.add_argument("--resize", action="store_true", help="Do dictionary resize")
# ...
# if resize flag is true I'm re-sizing all objects
if args.resize:
for object in my_obects:
object.do_resize()
是否有一种实现as参数的方法:如果作