looking for best way of giving command line arguments in python, where some params are req for some option and some params are req for other options -
hi trying send command line arguments first time. condition 1 parameter required 1 option , others other parameter.(looking user friendly). below code looks need 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 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 needed start run , job id not needed" sys.exit(2) elif args.options == "rerun": if conf_file == "none" or jobid == "none": print "job configuration , jobid needed start rerun." sys.exit(2) else: if jobid == "none": print "jobid needed perform %s action on job , configuration not needed." %args.options sys.exit(2) action(conf_file, jobid) else: print "usage error:" print_help()
hope required params required options code. please let me know detailed explanation on requirements
this variation on script runs, , cleans several things. uses choices
control options values. omits unnecessary parameters in other add_argument
calls. simplifies post parse_args
logic. help
isn't needed since there -h
option, included choice. falls through end because not in action
dictionary.
import argparse import sys class stub(object): def __init__(self,s): self.s = s def __call__(self,conf_file, jobid): print self.s, conf_file, jobid parser = argparse.argumentparser(description='usage options.') parser.add_argument('-o','--options', choices=('run','rerun','kill', 'resume', 'suspend','help'),required=true) parser.add_argument('-c', '--config',help='config file input') # optionals not required; 'file' not valid type # argparse.filetype open file parser.add_argument('-j', '--jobid',help='id of job') # str default type args = parser.parse_args() # action_arg = [args.options] action = {'run': stub('start_run'), 'rerun': stub('start_rerun'), 'kill': stub('kill_job'), 'resume': stub('resume_job'), 'suspend': stub('suspend_job'), #'get_run': get_run, # not in choices #'get_component': get_component, #'help': print_help, }.get(args.options) # what's '-'.join? if action: if args.options == "run": if args.config none: # proper test none print "job configuration needed start run , job id not needed" sys.exit(2) elif args.options == "rerun": if args.config none or args.jobid none: print "job configuration , jobid needed start rerun." sys.exit(2) else: if args.jobid none: print "jobid needed perform %s action on job , configuration not needed." %args.options sys.exit(2) action(args.config, args.jobid) else: print "usage error:" parser.print_help()
this version replaces options
choices subparsers. config
, jobid
become required arguments appropriate subparsers. use parents
conveniently define required mix , match. argparse
checking.
import argparse class stub(object): def __init__(self,s): self.s = s def __call__(self,conf_file, jobid): print self.s, conf_file, jobid conf_parent = argparse.argumentparser(add_help=false) conf_parent.add_argument('-c', '--config',help='config file input',required=true) job_parent = argparse.argumentparser(add_help=false) job_parent.add_argument('-j', '--jobid',help='id of job',required=true) parser = argparse.argumentparser(description='usage options.') parser.set_defaults(config=none, jobid=none) # put default value in namespace sp = parser.add_subparsers(dest='options') sp.add_parser('run',parents=[conf_parent]) sp.add_parser('rerun',parents=[conf_parent, job_parent]) sp.add_parser('kill',parents=[job_parent]) sp.add_parser('resume',parents=[job_parent]) sp.add_parser('suspend',parents=[job_parent]) args = parser.parse_args() action = {'run': stub('start_run'), 'rerun': stub('start_rerun'), 'kill': stub('kill_job'), 'resume': stub('resume_job'), 'suspend': stub('suspend_job'), }.get(args.options) action(args.config, args.jobid)
Comments
Post a Comment