我编写了一个自定义子模块,以便在类似的API项目中重用相同的代码。你知道吗
在我的子模块中,我有以下内容:
# sub.py
from argparse import ArgumentParser
arg_parser = ArgumentParser()
arg_parser.add_argument("-s", "--silent", help="silent mode", action="store_true")
script_args = arg_parser.parse_args()
if not script_args.silent:
# If the silent flag isn't passed then add logging.
#logger.addHandler(console_handler)
在我的主脚本中通过add_argument()
添加额外参数的最佳方法是什么?你知道吗
# main.py
import sub
# This still works:
if sub.script_args.silent:
# Some code
# I tried this, but it doesn't work:
sub.arg_parser.add_argument("-t", "--test", help="test mode", action="store_true")
sub.script_args.parse_args()
# The script doesn't know about -t.
您可以使用解析已知参数函数(partial parsing)。你知道吗
例如:
注意文档中的警告:
相关问题 更多 >
编程相关推荐