在 Python 中使用 argparse 为具有两个值的参数设置单独的选择
我目前有以下代码:
import argparse
parser = argparse.ArgumentParser(description='Adds a new modem to Iridium account')
parser.add_argument('imei', metavar='I', nargs=1, help='the modems IMEI')
parser.add_argument('-t1', '--type1', metavar='t1', nargs=1, choices=('email', 'directip', 'sbddevice'), default='directip', help='Call type (default: directip)')
parser.add_argument('-a1', '--address1', metavar='a1', nargs=1, default='75.101.138.217:9097', help='Call address (default: 75.101.138.217:9097)')
parser.add_argument('-t2', '--type2', metavar='t2', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a2', '--address2', metavar='a2', nargs=1, help='Call address')
parser.add_argument('-t3', '--type3', metavar='t3', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a3', '--address3', metavar='a3', nargs=1, help='Call address')
parser.add_argument('-t4', '--type4', metavar='t4', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a4', '--address4', metavar='a4', nargs=1, help='Call address')
parser.add_argument('-t5', '--type5', metavar='t5', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a5', '--address5', metavar='a5', nargs=1, help='Call address')
args = parser.parse_args()
有没有办法可以将所有 -t 和 -a 组合成 -m1、-m2、-m3、-m4、-m5,其中 t 是相同选择的参数的第一个值下面,-a 是第二个参数,但不限于与第一个值相同的选择?看起来你应该能够做到这一点,否则你必须稍后进行一系列检查以查看是否存在 t1 和 a1,因为如果用户提供一个,他们需要提供另一个。
因此,不要执行 -t1 email -a1 [email protected]
你可以这样做 -m1 email [email protected]
I currently have the following code:
import argparse
parser = argparse.ArgumentParser(description='Adds a new modem to Iridium account')
parser.add_argument('imei', metavar='I', nargs=1, help='the modems IMEI')
parser.add_argument('-t1', '--type1', metavar='t1', nargs=1, choices=('email', 'directip', 'sbddevice'), default='directip', help='Call type (default: directip)')
parser.add_argument('-a1', '--address1', metavar='a1', nargs=1, default='75.101.138.217:9097', help='Call address (default: 75.101.138.217:9097)')
parser.add_argument('-t2', '--type2', metavar='t2', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a2', '--address2', metavar='a2', nargs=1, help='Call address')
parser.add_argument('-t3', '--type3', metavar='t3', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a3', '--address3', metavar='a3', nargs=1, help='Call address')
parser.add_argument('-t4', '--type4', metavar='t4', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a4', '--address4', metavar='a4', nargs=1, help='Call address')
parser.add_argument('-t5', '--type5', metavar='t5', nargs=1, choices=('email', 'directip', 'sbddevice'), help='Call type')
parser.add_argument('-a5', '--address5', metavar='a5', nargs=1, help='Call address')
args = parser.parse_args()
Is there a way I can combine all the -t and -a into say -m1, -m2, -m3, -m4, -m5 where t is the first value of the argument which the same choices below and -a is the second argument but not restricted to the same choices as the first value? It seems like you should be able to do this otherwise you have to do a bunch of checking later on to see if there is a t1 and a1 since if the user provides one they need to provide the other.
So instead of doing -t1 email -a1 [email protected]
you could just do -m1 email [email protected]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否会对以后的任何人有帮助,但我最终按照亚历克斯的建议做了。唯一的问题是我使用了
~
而不是:
因为我必须使用端口处理 IP 地址,所以使用:
会把事情搞砸。Not sure if this will help anyone down the road, but I ended up doing what Alex suggested. Only thing is I used
~
instead of:
because I had to handle IP addresses with ports so using:
would screw things up.老问题,但我自己正在了解这个问题,并认为我应该发布答案。
nargs
参数可以解决这个问题,但显然不能与choices
结合使用,因为它也适用于电子邮件地址:Old question, but I'm learning about this myself, and thought I'd post an answer. The
nargs
parameter can take care of this, but obviously cannot be combined withchoices
, since it would apply to the email address as well: