【问题标题】:Why does docopt exits the script after parsing the parameters?为什么docopt解析完参数后退出脚本?
【发布时间】:2016-12-24 20:36:03
【问题描述】:

我现在使用docopt 有一段时间了,在一个新脚本上我无法通过参数解析:

# coding=utf-8
"""
API server for the infoscreen frontends

Usage:
    python3 webserver.py [options]

Options:
    --bind ADDRESS  address to bind to  [default: 0.0.0.0]
    --galarmclock URL  URL for the galarmclock API [default: http://10.100.10.202:8082]
    --loglevel LOG  logging level   [default: logging.DEBUG]
    --console   log to console [default: False]
    --syslog    log to syslog [default: False]

"""

import docopt

# process arguments
args = docopt.docopt(__doc__)
print(args)

所有参数(参数)都是可选的并且具有默认值,那么为什么脚本会停止?

C:\Python3\python.exe C:/tst.py
Usage:
    python3 webserver.py [options]

Process finished with exit code 1

【问题讨论】:

    标签: python docopt


    【解决方案1】:

    问题出在用法部分:

    Usage:
        python3 webserver.py [options]
    

    Docopt 期望使用部分的第一个字符串是您的程序,而不是 python。所以 docopt 会将此解释为 python3 是您的程序,并且它总是需要一个名为 webserver.py 的命令。如果您删除 python3 部分,它应该可以正常工作,如下所示:

    Usage:
        webserver.py [options]
    

    来自docopt's documentation 我们有:

    出现在关键字使用:(不区分大小写)和明显空行之间的文本被解释为使用模式列表。使用后的第一个词:被解释为程序的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多