Python command-line parameter analysis tools and application installation docopt

What is docopt?

  1, docopt is to write a Python command-line interactive execution of the script language.

    It is a language!

    It is a language!

    It is a language!

  2, the use of this language can be in your own script, add some rules limit. So that the script must be performed in accordance with this format in the course of implementation, at the same time, you can also easily write some help (in fact, a lot of software to display information when you enter -h, such as python -h, displays all of the python command, and page through the python docopt limiting command format), the following are some simple examples:

docopt installation

  docopt have a lot of the versions, supporting different languages, the easiest is to support python language, docopt.java support java script, docopts is to support shell scripts (the example below the main thing to docopts for example)

  1, the installation docopt 

    method one:      

pip install docopt 

    Method Two:

      You can download the source code (docopt open source projects) on github, and then installed by Python setup.py install

 

  2, the mounting method must be used docopts above two installation docopt to install, use and install python, Download: https: //github.com/docopt/docopts

 

docopt simple analysis

  Have such a property in Python the __doc__, its value is a string, generally designated help information, and docopt is utilized as the attribute information to help replace the parsing command line parameters described in can be parsed :

  for example:

  

"""Naval Fate.
Usage:
 naval_fate.py ship new <name>...
 naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
 naval_fate.py ship shoot <x> <y>
 naval_fate.py mine (set|remove) <x> <y> [--moored | --drifting]
 naval_fate.py (-h | --help)
 naval_fate.py --version
Options:
 -h --help  Show this screen.
 --version  Show version.
 --speed=<kn> Speed in knots [default: 10].
 --moored  Moored (anchored) mine.
 --drifting Drifting mine.
"""
from docopt import docopt
if __name__ == '__main__':
 arguments = docopt(__doc__, version='Naval Fate 2.0')
 print(arguments)

  The above code, a large section of help is our command-line parameters to resolve description, function entry called docopt function parses the returned arguments variable is a variable dictionary, which records whether the option is selected, what is the value of the parameter and other information, when the program runs from the command line, we are to know the options and parameter information entered by the user according to records arguments variable.

  So how to write a command-line parameter parsing instructions is very important, command line parsing information includes two parts, namely, usage patterns and format options description format.

  Use mode format (Usage pattern format)

    Use mode format usage: beginning with a blank line ends, as the code above, it is mainly describes the format of the user to add a command with a parameter, which is the format used, parsing is performed in accordance with this format.

    Each usage pattern contains the following elements:

      * Parameters

        Parameter capital letters or angle brackets <> surrounded

      * Options

        Option starts in the dash or -, is only one letter format -o, --output more than one letter, and also a plurality of single letters can be combined Options, -ov equivalent -o, -v, -i .. Options can 

        Parameters, then do not forget to add a description Option Description

    Next, using some identify patterns used in the meaning of the right to use them to better complete analytical tasks:

      * [] 

        Element represents an optional, non-essential elements of square brackets

      *()

        Some representatives of the necessary elements, the elements must be in parentheses, even when a plurality of selected inside

      *|

        Mutually exclusive elements, vertical lines on both sides of the elements can be only one left

       * ....

          On behalf of the element can be repeated, when a list of final interpretation of the results

       *[options]

          Specify a particular option, complete a specific task.

  Option Description Format (Options description format)

    Option Description likewise essential, especially when the parties have the option parameters, and also requires a default value for timed it.

    Format parameter is added, there are two options:

      -o FILE --output -FILE # comma not using the symbol =

      -i <file>, --input <file> # commas sign = not used

    Add description description as an option, just use two spaces segmentation options and descriptions can be

    Default option is added, it can be later added to the selection described in the following format [defailt: <my-default-value>}  

--coefficient=K The K coefficient [default: 2.95]
--output=FILE Output file [default: test.txt]
--directory=DIR Some directory [default: ./]

    If you can duplicate option, then its value [default: ...] will be a list, if you can repeat, it is a string value.

  

use

  After understanding the usage patterns described format formats and options, in the example it can be given with a better understanding;

  The next step is to get the input information

  Arguments mentioned type parameter is a dictionary that contains information of the user input options and parameters, or end of the code above in the previous example, if we run the command line is input

python test.py ship Guardian move 100 150 --speed=15

   Then print arguments parameters are as follows:

  

{'--drifting': False,
 '--help': False,
 '--moored': False,
 '--speed': '15',
 '--version': False,
 '<name>': ['Guardian'],
 '<x>': '100',
 '<y>': '150',
 ': False,
 'mine'move': True,
 'new': False,
 'remove': False,
 'set': False,
 'ship': True,
 'shoot': False}

  From the print information can go to for options, the boolean input to identify whether this option is used to express specific value.

  This way, you can get the next program operating from the arguments variable, and if the user did not enter any parameters, print Usage instructions prompt content.

 

Guess you like

Origin www.cnblogs.com/jcjc/p/11586618.html
Recommended