python - 使用 argparse 的参数名称
问题描述
# import the necessary packages
import argparse
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--name", required=True,
help="name of the user")
args = vars(ap.parse_args())
# display a friendly message to the user
print("Hi there {}, it's nice to meet you!".format(args["name"]))
如果我使用 运行此代码-h
,我会得到:
$ python simple_example.py --help
usage: simple_example.py [-h] -n NAME
optional arguments:
-h, --help show this help message and exit
-n NAME, --name NAME name of the user
是什么NAME
意思,在-n
and之后--name
?代码中没有指定在任何地方使用字符串“NAME”,它是怎么出现在这里的?如果它是“名称”而不是“名称”,那将是有意义的。
解决方案
NAME
表示您需要传递的实际参数。这是一种区分需要类似参数的标志-n
(例如 --n Kyathari
有效,-n
本身无效)和不接受类似参数的标志的方法-h
。
推荐阅读
- yii2 - 在 Yii2 中使用 DBquery 从选择查询中添加值
- sorting - 当比较函数对某些对返回“不知道”时进行排序
- airflow - Airflow SSHOperator 不会 source linux 环境
- reactjs - 如何使用 webpack 分别捆绑供应商和主脚本?
- mysql - 获取 mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'vsearch'@'localhost' (using password: YES) with python
- javascript - JS数组合并求和
- javascript - 有没有一种方便的方法来引用 Svelte 组件中的 DOM 元素?
- javascript - 如何显示用户选择上传的图像
- java - Java 中循环技巧的 Helloworld 示例
- c++ - 从 txt 文件中删除注释