我有一个 python 脚本来解析来自用户的输入参数。我的代码是这样的
def get_arg(argv):
result = {}
input_file=stag_db=main_tb=stag_table=main_table = "";
debug="N"
msg = ''' Syntax: dt transaction date
-i input_file (E.g. input_file.tar.gz)
-ds staging_database
-dm main_database
-ts staging_table
-tm main_table
-db debug (Y/N)'''
try:
opts, args = getopt.getopt(argv,"hd:i:ds:dm:db:ts:tm:",["ifile=","ofile="])
print args
print opts
except getopt.GetoptError:
f_end_process(msg)
for opt, arg in opts:
if opt == '-h':
f_end_process(msg)
elif opt == "-i":
input_file = arg
elif opt == "-ds":
stag_db = arg
elif opt == "-dm":
main_tb = arg
elif opt == "-ts":
stag_table = arg
elif opt == "-tm":
main_table = arg
elif opt == "-db":
debug = arg
result = {'input_file':input_file,'stag_db':stag_db,'main_tb':main_tb,'stag_table':stag_table,'main_table':main_table}
print result
if '' in result.values():
exit_status=-1
f_end_process(msg)
result['debug']= debug
return result
def main():
global input_arg
input_arg = get_arg(sys.argv[1:])
print "process started at " +strftime("%Y-%m-%d %H:%M:%S")
print input_arg
我正在运行这样的代码
python main.py -i ok.txt -ds ds_val -dm dm_val -ts tas_val -tm tm_val
我想将所有输入参数解析为一个列表。我将所有必需的模块导入到我的脚本中
现在我只能解析 -i 输入。如何解析 -tm、-ts、-dm、-ds 输入?