在Python中如何获取命令行参数
1个回答
展开全部
import sys
import getopt
def usage():
print ("sys.argv[0]: '-a aa -b bb -c cc'")
print ("sys.argv[0]: ' -h'")
def db_get_args():
try:
opts,args = getopt.getopt(sys.argv[1:], "ha:b:c:")
except getopt.GetoptError,err:
print str(err)
usage()
sys.exit(1)
if len(opts) == 3:
for op,value in opts:
if(op == "-h"):
usage()
sys.exit(1)
elif(op == "-a"):
aa = value
elif(op == "-b"):
bb = value
elif(op == "-c"):
cc = value
else:
usage()
sys.exit(1)
else:
usage()
sys.exit(1)
return aa,bb,cc
aa,bb,cc = db_get_args()
print aa,bb,cc
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询