如何将子部分用于可选的位置参数?
我刚刚开始使用 argprase
,并且具有以下示例 main.py
,
import os
import numpy
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-C','--Chk',type=str, help='Choose arg')
args = vars(parser.parse_args())
if args['Chk'] == 'compo1':
print('This is test1')
elif args['Chk'] == 'compo2':
print('This is test2')
if __name__=='__main__':
main()
如果我使用 python3 main.py.py -c compo1 我得到所需的结果,即,
这是test1
。现在,如果调用 -c compo1
,我想添加附加参数。例如 python main.py -c compa1 -d
其中, -d
执行一个任务,例如。
This is test1 #output for -C compo1
This is sub task from test1 #2nd output when -d is called
另外,我希望 -d
也是 compa2
时的标志,但随后输出应不同并且指定。
谁能建议如何在ArgParse库中获得可选位置论点的其他论点?并且子部分可以用于可选参数和相同的命令线标志吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,不幸的是,我能告诉我的答案是否定的。 ArgParse模块无法区分您想区分的订单。但是,我确实在
中,
我为您的设置编码了以下实现方案:
由于ArgParse帮助不会显示您要鼓励的格式,我建议编写详细的
Description> Description> Description
消息以指示-d
标志在哪里。希望这有帮助!So the answer from what I can tell is unfortunately no. The argparse module can't distinguish between the orderings which you would like to distinguish between. I did however find some decent workarounds to this issue at
Find the order of arguments in argparse python3
I coded the following implementation scheme for your setup:
Since the argparse help won't display the formatting that you're trying to encourage, I'd recommend writing a detailed
description
message to indicate where the-d
flags go. Hope this helps!