Python taking inputs from command line

Post date: Mar 7, 2016 7:41:28 PM

I use the package getopt to do the job:

import sys, getopt def get_params_from_command_line(argv): ''' Get the parameters from command line ''' try: opts, args = getopt.getopt(argv,"h" ,["cleanup=" , "resume_jobid=" , "machine_type=" , "node_size=" , "run_date=" , "data_date=" , "data_region_id=" , "trf_date=" , "trf_region_id=" , "abbv_date="] ) except getopt.GetoptError: print 'test_cmdline_params.py --cleanup --resume_jobid --machine_type --node_size --run_date --data_date --data_region_id --trf_date --trf_region_id --abbv_date' sys.exit(2) argv_dict = dict() for opt, arg in opts: if opt == '-h': print 'test_cmdline_params.py --cleanup --resume_jobid --machine_type --node_size --run_date --data_date --data_region_id --trf_date --trf_region_id --abbv_date' sys.exit() elif opt in ("--cleanup"): if arg == 'True': argv_dict['cleanup'] = True else: argv_dict['cleanup'] = False elif opt in ("--resume_jobid"): argv_dict['resume_jobid'] = arg if arg == 'None': argv_dict['resume_jobid'] = None elif opt in ("--machine_type"): argv_dict['machine_type'] = arg elif opt in ("--node_size"): argv_dict["node_size"] = int(arg) elif opt in ("--run_date"): argv_dict["run_date"] = arg elif opt in ("--data_date"): argv_dict["data_date"] = arg elif opt in ("--data_region_id"): argv_dict["data_region_id"] = eval(arg) if isinstance(argv_dict["data_region_id"], list): argv_dict["data_region_id"] = sorted(argv_dict["data_region_id"]) elif opt in ("--trf_date"): argv_dict["trf_date"] = arg elif opt in ("--trf_region_id"): argv_dict["trf_region_id"] = int(arg) elif opt in ("--abbv_date"): argv_dict["abbv_date"] = arg return argv_dict def get_params_from_dict(d): for k, v in d.iteritems(): print "{k}={v} type:{type}".format(k=k, v=v, type=type(v)) argv_dict = get_params_from_command_line(sys.argv[1:]) print argv_dict get_params_from_dict(argv_dict)

The followings are output in command line:

> python test_cmdline_params.py -h
test_cmdline_params.py --cleanup --resume_jobid --machine_type --node_size --run_date --data_date --data_region_id --trf_date --trf_region_id --abbv_date
> python test_cmdline_params.py --cleanup True --resume_jobid 9kajhfbij --node_size 7
{'node_size': 7, 'resume_jobid': '9kajhfbij', 'cleanup': True}
node_size=7 type:<type 'int'>
resume_jobid=9kajhfbij type:<type 'str'>
cleanup=True type:<type 'bool'>
> python test_cmdline_params.py --cleanup 'True' --resume_jobid '9kajhfbij' --node_size 7
{'node_size': 7, 'resume_jobid': '9kajhfbij', 'cleanup': True}
node_size=7 type:<type 'int'>
resume_jobid=9kajhfbij type:<type 'str'>
cleanup=True type:<type 'bool'>

Note that everything will be imported as a string of characters and having single quote embraced or not does not make any difference. And of course, you can escape the single quote using back-slash like the example below:

> python test_cmdline_params.py --cleanup \'True\' --resume_jobid \'9kajhfbij\' --node_size 7
{'node_size': 7, 'resume_jobid': "'9kajhfbij'", 'cleanup': False}
node_size=7 type:<type 'int'>
resume_jobid='9kajhfbij' type:<type 'str'>
cleanup=False type:<type 'bool'>