在 Python 中使用 argparse 为具有两个值的参数设置单独的选择

发布于 2024-09-09 11:47:09 字数 1906 浏览 3 评论 0原文

我目前有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-09-16 11:47:09

不确定这是否会对以后的任何人有帮助,但我最终按照亚历克斯的建议做了。唯一的问题是我使用了 ~ 而不是 : 因为我必须使用端口处理 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.

忘年祭陌 2024-09-16 11:47:09

老问题,但我自己正在了解这个问题,并认为我应该发布答案。 nargs 参数可以解决这个问题,但显然不能与 choices 结合使用,因为它也适用于电子邮件地址:

import argparse

parser = argparse.ArgumentParser(
    description='Adds a new modem to Iridium account',
    formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('imei', metavar='I', help="the modem's IMEI")
parser.add_argument('-m1', nargs=2, metavar=("type","address"), default=['directip', '75.101.138.217:9097'], help='Call setting')
parser.add_argument('-m2', nargs=2, metavar=("type","address"), help='Call setting')
parser.add_argument('-m3', nargs=2, metavar=("type","address"), help='Call setting')
parser.add_argument('-m4', nargs=2, metavar=("type","address"), help='Call setting')
parser.add_argument('-m5', nargs=2, metavar=("type","address"), help='Call setting')

args = parser.parse_args()

type1 = args.multi1[0]
address1 = args.multi1[1]
if not type1 in ['email', 'directip', 'sbddevice']:
    args.print_help()
    sys.exit(1)
...

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 with choices, since it would apply to the email address as well:

import argparse

parser = argparse.ArgumentParser(
    description='Adds a new modem to Iridium account',
    formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('imei', metavar='I', help="the modem's IMEI")
parser.add_argument('-m1', nargs=2, metavar=("type","address"), default=['directip', '75.101.138.217:9097'], help='Call setting')
parser.add_argument('-m2', nargs=2, metavar=("type","address"), help='Call setting')
parser.add_argument('-m3', nargs=2, metavar=("type","address"), help='Call setting')
parser.add_argument('-m4', nargs=2, metavar=("type","address"), help='Call setting')
parser.add_argument('-m5', nargs=2, metavar=("type","address"), help='Call setting')

args = parser.parse_args()

type1 = args.multi1[0]
address1 = args.multi1[1]
if not type1 in ['email', 'directip', 'sbddevice']:
    args.print_help()
    sys.exit(1)
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文