Python docopt module detailed
Docopt essentially introduces a formal language for command-line arguments in Python, and writes the document in the form of a document that meets the requirements in the beginning of the code using the "" "" ""
Usage:
The use of docopt is very simple, take qingchat as an example, you only need to add at the beginning of the code:
"" "qingchat cliusage:qingchat config ip <ip>qingchat config port <port>qingchat config loginqingchat Group Li Stqingchat Group Choose <group_name>...qingchat Group Cleanqingchat Group Send-t <content>qingchat Group Sen D-i <media>qingchat Group Send-f <file> [<delaytime>]options:-h--help Show this screen.-v--version S How version. "" "
Then add in the execution code:
arguments = docopt (__doc__,version= "Qingchat 0.3.2")
A arguments dictionary is then imported into the program, and the contents of this dictionary are:
{'-F ': false, '-I ': false, '-T ': false, ' <content> ': None, ' <file> ': None, ' <group_name> ': [], ' <ip > ': ' 127.0.0.1 ', ' <media> ': None, ' <port> ': None, ' choose ': false, ' clean ': false, ' config ': True, ' group ': False, ' IP ': True, ' list ': false, ' login ': false, ' port ': false, ' send ': false}
Let's describe in detail how to complete a note document that meets DOCOPT requirements:
Usage
All occurrences of the usage: (case-sensitive) and a blank line between the text will be recognized as a combination of commands, the Usage of the first letter will be recognized as the name of the program, all the command combination of each part (space-delimited) will become a key in the dictionary
Parameters
Text such as <argument> or argument will be recognized as a parameter
The value in the converted dictionary is true or FALSE.
Usage:my_program
Options
Text such as-O or--option will be recognized as an option
Evaluates to TRUE or False in the converted Dictionary
Usage:my_program-f <file>
Tips:
1. Short options can be combined, such as-ABC equivalent to-a-b-C
2. The parameters required for the long option need to be delimited with = or a space,--input=arg equivalent to--input ARG
3. The short option can not need a space, the-f FILE is equivalent to-ffile
Command
Text that does not meet--options or <arguments> will be recognized as a (sub) command
Evaluates to True or False in the converted Dictionary
Options available
Text that is shaped like [optional elements] is optional
Elements includes the above three types: parameters, options, and commands
The same or different parentheses are the same:
Usage:my_program[command--option <argument>]
Equivalent to
Usage:my_program[command][--option][<argument>]
Required Options
Text like (required elements) is a must option
The above three elements are required by default, and () symbols are used in some of the more special cases, such as:
Usage:my_program (--either-this<and-that>|<or-this>)
Select item
Text such as Element|another is the selection, from which you can select a value
Usage:my_program Go (--up|--down|--left|--right)
List items
Shaped like element ... The text is a list item, you can enter multiple parameters
Like what:
Usage:my_program Open <file>
You can then access this list by arguments[' <file> ']
Option
The option section is used to specify certain special cases, such as:
1. Associate a short parameter with a long parameter, such as-I <file>--input <file>
2. An option has one parameter
3. Default values for options, such as--coefficient=k the K coefficient [default:2.95]
Python docopt module detailed