Hi i am trying to send command line arguments for first time. The condition is that one parameter is required for one option and for others other parameter.(looking for user friendly). The below code looks need some optimization:
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()
Hope you get my required params for required options from code. Please let me know for detailed explanation on requirements