To generate a python script parameter item using DOCOPT

Source: Internet
Author: User
Tags python script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.