docopt

  • 功能还是很强大的:http://hao.jobbole.com/docopt/
[root@shuffle-dev py_test]$ vim doc.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Naval Fate.

 Usage:
   doc.py ship new ...
   doc.py ship  move   [--speed=]
   doc.py ship shoot  
   doc.py mine (set|remove)   [--moored | --drifting]
   doc.py (-h | --help)
   doc.py --version
   doc.py [--name NAME] [-h] [--version]

Options:
   --name NAME   Your name                                                                                                                    
   -h --help     Show this screen.
   --version     Show version.
   --speed=  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)
[root@shuffle-dev py_test]$ python doc.py --name shuffle
{'--drifting': False,
 '--help': False,
 '--moored': False,
 '--name': 'shuffle',
 '--speed': '10',
 '--version': False,
 '': [],
 '': None,
 '': None,
 'mine': False,
 'move': False,
 'new': False,
 'remove': False,
 'set': False,
 'ship': False,
 'shoot': False}
[root@shuffle-dev py_test]$ python doc.py ship new shuffle
{'--drifting': False,
 '--help': False,
 '--moored': False,
 '--name': None,
 '--speed': '10',
 '--version': False,
 '': ['shuffle'],
 '': None,
 '': None,
 'mine': False,
 'move': False,
 'new': True,
 'remove': False,
 'set': False,
 'ship': True,
 'shoot': False}

你可能感兴趣的:(docopt)