Brief introduction
Compare Optparse and argparse,docopt to save programmers ' scripting parameter hints and parameter entry time, but are more casual and non-canonical, and code maintainability is worse
Use case:
# coding=utf-8 "" "1 #这儿写标题, H will be printed when the usage: #没有出现在Usage中的不规范用法, the usage section will be printed, and SY S.exit (1) 1.py d_ip init_instance [--configsvr] [--replname=<replsetname>] [-P PORT |--port=port] [--disk=data{}] 1.py Init_mongos [-P PORT |--port=port] [--instance=<ip>:<port>] 1.py-h | --helparguments: #可以直接输入参数 instead of entering the parameter name. Its location is defined by the usage above d_ip destination ipoptions: #对上面Usage中出现的参数的说明,-P port self port. #当一个参数项同时拥有-X--xx Two forms of expression when--disk=data_number Install in/data{}. #结果字典中返回的参数名为--xx--replname=<replsetname> replset ' s Name. # # The value of a parameter entry has two expressions--X YY--xx=yy or-x <yy>,--xx <yy& Gt # The previous parameter value hint should be fullUppercase, followed by a comma-delimited, hint should be used <> package "" "from docopt import docoptdef concat_parameters (dict_a): Opt_str = ' For i in DICT_A.K Eys (): # If the option is not false (has value, or true) and is--parameters, append to the parameter string if dict_a[i] and i[0:2] = = '--': Print (' Key: ', i) Opt_str = opt_str+ ' {}={} '. Format (I,dict_a[i]) print (OPT_STR) if __name__ = = ' __main__ ': Argum Ents = docopt (__doc__, version= ' 0.1.1RC ') #借用了脚本的__doc__属性存放参数说明 the # version option specifies the versioning information for the script # print (arguments) if arguments[' init_instance ': d_ip = arguments[' d_ip '] concat_parameters (arguments) elif arguments[' init_m Ongos ']: Pass
How to use:
python 1.py init_instance --port=3304
Reference connection
GitHub Project: Docopt
To generate a python script parameter item using DOCOPT