ArgParse中的可选参数(如何使可选论点与特定字段一起使用)
我有以下要求使用argparse。 我的代码:
def main(argv=None):
parser = argparse.ArgumentParser(description="")
parser.add_argument("--svc_only", nargs="?", const=True, metavar="", help="")
group = parser.add_mutually_exclusive_group()
group.add_argument("--a", nargs="?", const=True, default=False, choices=["--svc_only"], help="")
group.add_argument("--b", nargs="?", const=True, default=False, choices=["--svc_only"],help="")
group.add_argument("--c", action='store_true', help="")
group.add_argument("--cmd", nargs='+', help="")
parser.add_argument("--loc", help="")
parser.set_defaults(func=handle_op)
args = parser.parse_args(argv)
print("args",args)
return args.func(args)
问题:我正在使用mutally_exclusive_group
,因此它可以正常工作,我可以使用该组中的任何一个命令,但对于 - svc_only 我希望它仅适用于 - 可选论点。
也就是说
python3 abc.py --a --svc_only
,它可以正常工作,但是可以将此-svc_only传递给组中注册的任何命令,例如python3 abc.py -b - -svc_only
。 (如何限制将其与特定的位置参数一起使用)。
我已经检查了子解析器,但似乎它不使用-
(可选参数
)
#!/usr/bin/env python3
import argparse
def main(argv=None):
# create the top-level parser
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest="command")
pcyc = subparser.add_parser("--a", help="Restrat SPUS")
pcyc.add_argument("--svc_only",nargs="?", const=True, metavar="", help="Scale down stateful set")
stop = subparser.add_parser("--b", help="Restrat SPUS")
stop.add_argument("--svc_only", nargs="?", const=True, metavar="", help="Scale down stateful set")
args = parser.parse_args(argv)
print("args",args)
if __name__ == '__main__':
main()
python .\testCase1.py --a
usage: testCase1.py [-h] {--a,b} ...
testCase1.py: error: unrecognized arguments: --a
I am having the below requirement using Argparse.
My code:
def main(argv=None):
parser = argparse.ArgumentParser(description="")
parser.add_argument("--svc_only", nargs="?", const=True, metavar="", help="")
group = parser.add_mutually_exclusive_group()
group.add_argument("--a", nargs="?", const=True, default=False, choices=["--svc_only"], help="")
group.add_argument("--b", nargs="?", const=True, default=False, choices=["--svc_only"],help="")
group.add_argument("--c", action='store_true', help="")
group.add_argument("--cmd", nargs='+', help="")
parser.add_argument("--loc", help="")
parser.set_defaults(func=handle_op)
args = parser.parse_args(argv)
print("args",args)
return args.func(args)
Problem: I am using mutually_exclusive_group
so It is working fine and I can use any one command from the group but for --svc_only I want It to work only with --a optional argument.
that is
python3 abc.py --a --svc_only
//It is working fine but this --svc_only can be passed to any command registered in the group for example python3 abc.py --b --svc_only
. (How to restrict to use it with specific positional argument).
I have checked about the sub parser but seems like It was not working with --
(optional argument)
for example
#!/usr/bin/env python3
import argparse
def main(argv=None):
# create the top-level parser
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest="command")
pcyc = subparser.add_parser("--a", help="Restrat SPUS")
pcyc.add_argument("--svc_only",nargs="?", const=True, metavar="", help="Scale down stateful set")
stop = subparser.add_parser("--b", help="Restrat SPUS")
stop.add_argument("--svc_only", nargs="?", const=True, metavar="", help="Scale down stateful set")
args = parser.parse_args(argv)
print("args",args)
if __name__ == '__main__':
main()
python .\testCase1.py --a
usage: testCase1.py [-h] {--a,b} ...
testCase1.py: error: unrecognized arguments: --a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论