嗨,我第一次尝试发送命令行参数。条件是一个选项需要一个参数,其他参数需要一个参数。(寻找用户友好的)。下面的代码看起来需要一些优化:
import argparse
parser = argparse.ArgumentParser(description='Usage options.')
parser.add_argument('-o','--options',help='Options available: run,rerun,kill, resume, suspend',required=True)
parser.add_argument('-c', '--config',help='config file input',type=file,required=False)
parser.add_argument('-j', '--id',help='id of the job',type=str,required=False)
args = parser.parse_args()
action_arg = list()
action_arg.append(args.options)
action = {'run': start_run,
'rerun': start_rerun,
'kill': kill_job,
'resume': resume_job,
'suspend': suspend_job,
'get_run': get_run,
'get_component': get_component,
'help': print_help}.get('-'.join(action_arg))
if action:
conf_file = str(args.config)
jobid = str(args.jobid)
if args.options == "run":
if conf_file == "None":
print "Job configuration is needed to start a run and job id is not needed"
sys.exit(2)
elif args.options == "rerun":
if conf_file == "None" or jobid == "None":
print "Job configuration and jobid is needed to start a rerun."
sys.exit(2)
else:
if jobid == "None":
print "Jobid is needed to perform %s action on job and configuration is not needed." %args.options
sys.exit(2)
action(conf_file, jobid)
else:
print "Usage Error:"
print_help()
希望您从代码中获得所需选项的所需参数。请让我知道有关要求的详细说明