gpt4 book ai didi

python - 添加一个以 "h"开头的参数

转载 作者:太空宇宙 更新时间:2023-11-03 13:57:18 25 4
gpt4 key购买 nike

使用这段代码,我似乎无法添加“--height”作为参数,因为 Python 与默认的“-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 myscript.py --height 222那样调用它,那么你必须这样做

parser.add_argument("--height", action="store")
args_namespace = parser.parse_args()
print(args_namespace.height)

关于python - 添加一个以 "h"开头的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53803168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com