docopt 使用手册

前言

这个例子很简单,但是对我来说够用,日后可以基于这个例子拓展

版本

docopt: 0.6.2
python: 3.6

安装

pip install docopt==0.6.2

注意

(1) 注意要写在import 前面, 否则报错。
(2) 注意doc前不要添加其他三引号的内容(前面的注释可以用“#”),否则无法识别,报如下错误:

Traceback (most recent call last):
  File "xxxx.py", line 26, in <module>
    args = docopt(__doc__)
  File "C:\Users\xxxx\anaconda3\envs\xxxx\lib\site-packages\docopt.py", line 558, in docopt
    DocoptExit.usage = printable_usage(doc)
  File "C:\Users\xxxx\anaconda3\envs\xxxxx\lib\site-packages\docopt.py", line 468, in printable_usage
    raise DocoptLanguageError('"usage:" (case-insensitive) not found.')
docopt.DocoptLanguageError: "usage:" (case-insensitive) not found.

例子

"""
Usage:
  vocab.py [options] TRAIN_SET_SRC TRAIN_SET_TGT VOCAB_FILE

Options:
    -h --help                  Show this screen.
    --size-src INT             src vocab size [default: 50000]
    --size-tgt INT             tgt vocab size [default: 50000]
    --freq-cutoff INT          frequency cutoff [default: 2]
    --vocab-class STR          the class name of used Vocab class [default: Vocab]
"""

from docopt import docopt

if __name__ == "__main__":
    args = docopt(__doc__)
    print(args)

必选参数:
TRAIN_SET_SRC TRAIN_SET_TGT VOCAB_FILE

可选参数:
-h --help Show this screen.
–size-src INT src vocab size [default: 50000]
–size-tgt INT tgt vocab size [default: 50000]
–freq-cutoff INT frequency cutoff [default: 2]
–vocab-class STR the class name of used Vocab class [default: Vocab]

执行方法:

python ../vocab.py  --size-src 10000  ../samples/train/train.spl.src          ../samples/train/train.txt.tgt          ../samples/dic/python_dic.json

*该命令制定了可选参数(–size-src)的值以及必选参数的值

结果:

{'--freq-cutoff': '2',
 '--help': False,
 '--size-src': '10000',
 '--size-tgt': '50000',
 '--vocab-class': 'Vocab',
 'TRAIN_SET_SRC': '../samples/train/train.spl.src',
 'TRAIN_SET_TGT': '../samples/train/train.txt.tgt',
 'VOCAB_FILE': '../samples/dic/python_dic.json'}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值