当我运行此代码时,我得到了
AttributeError: 'ArgumentParser' object has no attribute 'max_seed'
Run Code Online (Sandbox Code Playgroud)
这是代码
import argparse
import ConfigParser
CFG_FILE='/my.cfg'
# Get command line arguments
args = argparse.ArgumentParser()
args.add_argument('verb', choices=['new'])
args.add_argument('--max_seed', type=int, default=1000)
args.add_argument('--cmdline')
args.parse_args()
if args.max_seed:
pass
if args.cmdline:
pass
Run Code Online (Sandbox Code Playgroud)
我的源文件名为"fuzz.py"
我有以下代码:
parser.add_argument('file', help='file to test')
parser.add_argument('-revs', help='range of versions', nargs='+', default=False)
Run Code Online (Sandbox Code Playgroud)
有没有办法-revs在使用它时不使用标志,如下所示:
./somescript.py settings.json 1 2 3 4
Run Code Online (Sandbox Code Playgroud) 有没有办法让 python 的 argparse.ArgumentParser 像 python 函数处理参数一样处理命令行参数?那么参数可以不用名字就传递吗?
我有一些 Pythonargparse命令行处理代码,最初看起来像这样:
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("--x", help = "Set `x`.", action = "store_true", default = False)
ap.add_argument("--y", help = "Set `y`.", action = "store_true", default = False)
ap.add_argument(
"--all", help = "Equivalent to `--x --y`.",
action = "store_true", default = False
)
args = ap.parse_args()
if args.all:
args.x = True
args.y = True
print "args.x", args.x
print "args.y", args.y
Run Code Online (Sandbox Code Playgroud)
基本思想:我有一些布尔标志可以在特定设置(--x、--y等)上切换,并且我想添加一个方便的选项来切换多个设置 - 例如--all相当于--x --y.
我想避免任何未包含在 中ArgumentParser并在 中完成的命令行处理逻辑 …
我想使用 argparse 的 {choices} 参数,但允许用户从选择中输入任意数量的项目。例如,如果选择是 [1,2,3],我希望以下内容有效:
--arg 1
--arg 1,2
--arg 1,3
Run Code Online (Sandbox Code Playgroud)
等等。
但是,使用 nargs="+" 时,选择似乎不接受逗号分隔的输入。有没有办法解决?我仍然想强制传入的选项在我定义的选项集中,以便对奇怪的输入进行错误检查。
我正在尝试将参数从 pytest 测试用例传递给正在测试的模块。例如,使用Python 样板中的 main.py ,我可以从命令行运行它:
$ python3 main.py
usage: main.py [-h] [-f] [-n NAME] [-v] [--version] arg
main.py: error: the following arguments are required: arg
$ python3 main.py xx
hello world
Namespace(arg='xx', flag=False, name=None, verbose=0)
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试对 pytest 做同样的事情,使用以下 test_sample.py
(注意: main.py 需要命令行参数。但是这些参数需要在特定测试中进行硬编码,它们不应该是 pytest 的命令行参数。pytest 测试用例只需要将这些值作为命令行参数发送给 main。主要的()。)
import main
def test_case01():
main.main()
# I dont know how to pass 'xx' to main.py,
# so for now I just have one test with no arguments
Run Code Online (Sandbox Code Playgroud)
并按以下方式运行测试:
pytest -vs …Run Code Online (Sandbox Code Playgroud) 我正在使用库解析程序中的 CLI 参数argparse。我想解析一个可以重复的参数,具有以下行为:
到目前为止我有以下代码:
import argparse
ap = argparse.ArgumentParser(description="Change channel colours.")
ap.add_argument('-c', '--channel', action='append', default=['avx', 'fbx'])
print(ap.parse_known_args(['-c', 'iasdf', '-c', 'fdas']))
print(ap.parse_known_args())
Run Code Online (Sandbox Code Playgroud)
这适当地设置了一个默认列表,但是当参数出现时它不会以空列表开头。换句话说,第二个print语句打印正确的值(默认列表),但第一个语句打印
['avx', 'fbx', 'iasdf', 'fdas']
Run Code Online (Sandbox Code Playgroud)
代替
['iasdf', 'fdas']
Run Code Online (Sandbox Code Playgroud)
有没有办法argparse做我想做的事而不做类似的事情
if len(args.channel) > 2:
args.channel = args.channel[2:]
Run Code Online (Sandbox Code Playgroud)
事后?
我正在尝试制作这样的构建脚本:
import glob
import os
import subprocess
import re
import argparse
import shutil
def create_parser():
parser = argparse.ArgumentParser(description='Build project')
parser.add_argument('--clean_logs', type=bool, default=True,
help='If true, old debug logs will be deleted.')
parser.add_argument('--run', type=bool, default=True,
help="If true, executable will run after compilation.")
parser.add_argument('--clean_build', type=bool, default=False,
help="If true, all generated files will be deleted and the"
" directory will be reset to a pristine condition.")
return parser.parse_args()
def main():
parser = create_parser()
print(parser)
Run Code Online (Sandbox Code Playgroud)
然而,无论我如何尝试传递参数,我都只能得到默认值。我总是得到Namespace(clean_build=False, clean_logs=True, run=True)。
我努力了:
python3 build.py --run False …Run Code Online (Sandbox Code Playgroud) 我希望将用户输入的参数从命令行传递到 python 脚本的入口点。到目前为止,我尝试使用 argparse 将参数从命令行传递到 test.py 脚本。当我尝试传递参数时,它们无法被识别,并且收到以下错误。
load_entry_point('thesaurus==0.1', 'console_scripts', 'thesaurus')()
TypeError: find_synonym() 缺少 1 个必需的位置参数:'argv'
我在这里查看了其他示例,但无法使任何解决方案发挥作用。
def main(argv):
if argv is None:
argv = sys.argv
parser = argparse.ArgumentParser(description='Enter string')
parser.add_argument('string', type=str, help='Enter word or words', nargs='*')
args = parser.parse_args(argv[1:])
print(args)
if __name__ == "__main__":
sys.exit(main(sys.argv))
Run Code Online (Sandbox Code Playgroud)
我的 setup.py 脚本入口点如下所示
setup(entry_points={'console_scripts': ['test=test_folder.main:main']})
Run Code Online (Sandbox Code Playgroud)
我期望发生的情况与我运行 python main.py main foo 时类似。当它传递给函数时,它将成功打印出 hello。
如何使用 argparse 从一组选择中提供参数列表。例如,假设我想要类似的东西:
python sample.py --p1 ['a','b'] --p2 ['x','y']
Run Code Online (Sandbox Code Playgroud)
其中p1只能是列表中的任何或全部'a', 'b', 'c'并且p2只能是列表中的任何或全部'x', 'y', 'z'
argparse ×10
python ×9
python-3.x ×3
arguments ×2
command-line ×1
pytest ×1
python-2.7 ×1
setup.py ×1
sys ×1