python - 如何从外部文件中指定 argparse 选项?
问题描述
我正在使用 argparse 指定一些参数,如下所示:
my_parser = argparse.ArgumentParser()
my_parser.add_argument("--script_path", nargs='?', type=str, const='', default='', help="Path of the script to pull.")
my_parser.add_argument("--script", nargs='?', type=str, const='', default='', help="Name of the script to get pulled, without script extension.")
my_parser.add_argument("--project", nargs='?', type=str, const='', default='', help="Project.")
my_args = my_parser.parse_args()
my_script_path = my_args.script_path
my_script = my_args.script
my_project = my_args.project
现在我正在尝试做同样的事情,而是通过我将加载的 .json 文件定义上述参数。我选择 .json 是因为它看起来不错,请随时提出更好的建议。
我尝试过的是有一个这样的 .json 文件:
[
{
"name_or_flags": ["-sp", "--script_path"],
"nargs": "?",
"const": "",
"default": "",
"type": "str",
"help": "The absolute path of the script to run."
},
...
]
加载文件后,我在下面尝试并失败了:
my_parser.add_argument(<combination of all keys, values from .json as a dictionary>)
my_parser.add_argument(<*unnamed_tup, **named_dict>)
#unnamed tuple since name_or_flags isn't supposed to be used
#unnamed tuple is only made from name_or_flags
不管我做什么都行不通。
有没有人做过类似的事情?
我不希望通过外部文件添加值,例如:Using Argparse and Json together
只是为了定义论点。
谢谢!
解决方案
您需要pop("name_or_flags")
,注意始终提供一个列表;此外,您需要排除type
,因为它会引发错误(作为字符串而不是类或函数)。
import argparse
import json
args = json.loads("""
[{
"name_or_flags": "-sp", "--script_path"],
"nargs": "?",
"const": "",
"default": "",
"type": "str",
"help": "The absolute path of the script to run."
}]
""")
parser = argparse.ArgumentParser()
for arg in args:
arg.pop("type", None) # will raise ValueError: 'str' is not callable
parser.add_argument(*arg.pop("name_or_flags"), **arg)
# If the type is important to keep, you can always create a dictionary to map a string to a value, that is a builtin class.
mapping = dict(str=str, bool=bool, int=int) # this will map strings to classes
for arg in args:
thetype_str = arg.pop("type", "str")
arg["type"] = mapping.get(thetype_str, str) # if missing or wrong, will give plain string
parser.add_argument(*arg.pop("name_or_flags"), **arg)
推荐阅读
- azure-active-directory - 通过 AAD SSO 和 MFA 将 Power BI 转换为 Snowflake
- python - 我怎么做,如果乌龟靠近乌龟,它会增加一个变量?
- sql - 使用 sql 的 Databricks Windows 组
- python-3.x - Python3:TypeError:序列项0:预期的str实例,找到列表
- java - 加载我的应用程序时的android生命周期问题
- python - 在 Windows 中以编程方式复制、重命名和粘贴文件的最佳方法
- c# - 如何创建列表
在使用枚举的控制器中 - r - 如何使用 R 语言在基于多个二进制变量的数据框中创建新变量?
- javascript - 切换 React Native Firestore
- c# - LINQ 生成意外 SQL