开发者

Setting separate choices for argument with two values using argparse in Python

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 e开发者_如何学Pythonmail -a1 test@email.com you could just do -m1 email test@gmail.com


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.


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)
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜