gpt4 book ai didi

python argparse 喜欢 --home-path

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:02 31 4
gpt4 key购买 nike

我有一个问题,我找不到答案。这是我的代码块:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-t', '--test', help='just for testing')
parser.add_argument('--home-path', help='specify the homepath')
args = parser.parser_args()
if args.test:
print('Test')

如何像测试参数一样访问“--home-path”参数?

最佳答案

只需将属性名称中的-替换为_即可:

args = parser.parse_args()
if args.home_path:
# Do stuff

此行为记录在 dest 下在 ArgumentParser.add_argument() :

For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name.

关于python argparse 喜欢 --home-path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46213789/

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