我正在使用 Python 的argparse模块来解析命令行参数。考虑以下简化示例,# File test.pyimport argparseparser = argparse.ArgumentParser()parser.add_argument('-s', action='store')parser.add_argument('-a', action='append')args = parser.parse_args()print(args)可以成功调用python test.py -s foo -a bar -a baz-s在 each之后和之后需要一个参数-a,如果我们使用引号,它可能包含空格。但是,如果参数以破折号 ( -)开头并且不包含任何空格,则代码会崩溃:python test.py -s -begins-with-dash -a bar -a baz错误:参数 -s:应为一个参数我知道它解释-begins-with-dash为新选项的开始,这是非法的,因为-s尚未收到所需的参数。尽管没有-begins-with-dash定义带有名称的选项,但它也很清楚,因此首先不应将其解释为选项。如何argparse使用一个或多个前导破折号接受参数?
添加回答
举报
0/150
提交
取消