今天在看:
https://www.bilibili.com/video/av12380578/
这个的时候,开始的那段对命令行参数的解析不知道为社么总是弄不好,之后基于这段代码修改成我所需要的那段代码就可以正常运行了,不明觉厉啊!!!
# naval_fate.py
"""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)
上面的代码来自:
https://wp-lai.gitbooks.io/learn-python/content/0MOOC/docopt.html
修改成我所需要的代码是:
"""Train tickets query from CLI.
Usage:
ticket [-dgktz] <from> <to> <date>
Options:
-h --help Show this screen.
-d 动车
-g 高铁
-k 快车
-t 特快
-z 直达
"""
from docopt import docopt
def cli():
arguments = docopt(__doc__, version='tickets 1.0')
print(arguments)
if __name__ == '__main__':
cli()
然后就能成功运行了,我目测可能是开始注释那块的格式有问题