使用此代码,我似乎无法添加“ --height “作为参数,因为python与 -h / --help “默认选项。我试图补充 add_help=False 当创建对象时,我仍然得到错误 main.py: error: the following arguments are required: height
--height
-h / --help
add_help=False
main.py: error: the following arguments are required: height
import argparse parser = argparse.ArgumentParser(description='my description') parser.add_argument('height', type=int, nargs=1) args = parser.parse_args()
您创建了位置参数。argparse的工作方式是,当定义一个没有任何前导的参数时 - 或 -- 它将认为它是位置性的,因此您必须像 python yourscript.py the_height .
-
--
python yourscript.py the_height
如果你想这样称呼它 python myscript.py --height 222 那你必须这么做
python myscript.py --height 222
parser.add_argument("--height", action="store") args_namespace = parser.parse_args() print(args_namespace.height)