这个问题正在跟进链接
#!/usr/bin/env python
import argparse
import sys
import itertools as IT
from snemail import *
parser = argparse.ArgumentParser(
prog='snemail',
usage='%(prog)s [-h] [--usage] action target [input]',
epilog="refer to '%(prog)s usage' for examples of how to run snemail"
)
parser.add_argument('action', metavar='action', nargs='?', type=str, choices=('list', 'add', 'remove'), help="list | remove | add")
parser.add_argument('target', metavar='target', nargs='?', type=str, choices=('domain', 'forwarding', 'transport', 'user', 'all'), help="domain | forwarding | transport | user")
parser.add_argument('input', nargs='?', type=str, default=None, help="required input to go with the 'remove' and 'add' flags. 'list' requires no input field")
parser.add_argument('--usage', action='store_true', help="show usage examples for snemail")
args = parser.parse_args()
if args.usage:
usage()
sys.exit(0)
if args.action is None or args.target is None:
parser.print_help()
sys.exit(0)
if args.target == 'all':
all_list()
sys.exit(0)
if len(sys.argv) >= 3:
input = args.input
input = input.split(',')
else:
input = None
def usage():
print 'usage info here'
targets = 'domain forwarding transport user'.split()
actions = 'list add remove'.split()
dispatch = {key:globals()['%s_%s' % key] for key in IT.product(targets, actions)}
if input is not None:
dispatch.get((args.target, args.action), usage)(input)
elif args.action is not None and args.target is not None:
parser.print_help()
sys.exit(0)
这段代码可能会让专业 python 编码器的皮肤爬行,但它适用于我,在 Python 2.7(我用于我的开发机器)上,但是当我尝试在将要执行的机器上执行这段代码时在生产中运行此代码,它没有执行代码,因为
dispatch = {key:globals()['%s_%s' % key] for key in IT.product(targets, actions)}
在 2.7 中不起作用。
亲爱的互联网专家,如何在 Python 2.6 上进行这项工作?(因为升级到 2.7 不是一个选项,但更重要的是我想编写合适的 Python)