我正在使用 parser.add_option 到我的 optionparser。最初有 5 个选项,现在有 13 个参数。所以我的代码有 13 行 passrser.add_option; 它还有一行很长的代码来检查每个选项是否为 None 并在任何必填字段为空时抛出 parser.error。
反正有没有使这个简洁?
谢谢 ps1:我使用的是 python 2.6
ps2:示例代码:
usage = "usage: -b <bname> -a <aname> -c <cname> -d <dname> -i <iname> -t <tname> -r <rname> -k <kname> -s <sname> -e <ename> -f <fname> -g <gname> -u <uname>"
parser=OptionParser(usage)
parser.add_option("-b", "--bname", dest="bname",help="Enter the bname")
parser.add_option("-a", "--aname", dest="aname",help="Is aname required? True/False")
parser.add_option("-c", "--cname", dest="cname", help="Is cname? True/False")
parser.add_option("-d", "--dname", dest="dname", help="Is dname required? True/False")
parser.add_option("-i", "--iname", dest="iname", help="Is iname required? True/False")
parser.add_option("-t", "--tname", dest="tname", help="Is tname required? True/False")
parser.add_option("-r", "--rname", dest="rname", help="Is rname required? True/False")
parser.add_option("-k", "--kname", dest="kname", help="Is kname required? True/False")
parser.add_option("-s", "--sname", dest="sname", help="Is sname required? True/False")
parser.add_option("-e", "--ename", dest="ename", help="Is ename required? True/False")
parser.add_option("-f", "--fname", dest="fname", help="Is fname required? True/False")
parser.add_option("-g", "--gname", dest="gname", help="Is gname required? True/False")
parser.add_option("-u", "--uname", dest="uname", help="Enter your user name")
(options, args) = parser.parse_args()
if(options.bname is None or options.aname is None or options.cname is None or options.dname is None or options.iname is None or options.tname is None or options.rname is None or options.kname is None or options.sname is None or options.ename is None or options.uname is None or options.gname is None or options.fname is None):
parser.error("-b -v -a -c -d -i -t -r -k -s -e -u -f -g are all required parameters")