首页 文章

Python docopt有困难需要带参数的选项和选项

提问于
浏览
0

我是docopt的新手,并且在使用一个小例子时遇到了一些困难 . 我刚才遇到两个小问题,欢迎就这些问题提供帮助,并就改进代码提出更多一般性意见 . 第一个问题是让程序需要 --required 选项 . 它应该在没有所需命令行选项的情况下打印文档字符串 . 第二个问题是让程序接受选项的参数(例如 COMPUTER )(例如 --computer ) . 如何在终端中指定它以及如何编码?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
my example program

Usage:
    docopt_example_1.py [--ipaddress=IPADDRESS] [--computer=COMPUTER]
    docopt_example_1.py --network <network>
    docopt_example_1.py (--required)
    docopt_example_1.py --notrequired
    docopt_example_1.py --version

Arguments:
    IPADDRESS               I.P. address
    COMPUTER                computer identification
    <network>               network identification

Options:
    -h, --help              Show this help message.
    --version               Show the version and exit.
    --required              option required for running
    --notrequired           option not required for running
    --ipaddress=IPADDRESS   I.P. address
    --computer=COMPUTER     computer identification
    --network=NETWORK       network identification
"""
from docopt import docopt

def main(options):
    print("----------")
    print("a printout of the command line options as parsed by docopt:")
    print(options)
    print("----------")
    if options["--notrequired"]:
        print("required option selected")
    if options["--computer"]:
        print("computer name: {computer}".format(computer=options["COMPUTER"]))
    if options["--network"]:
        print("computer name: {network}".format(network=options["<network>"]))
    else:
        print("no options")

if __name__ == "__main__":
    options = docopt(__doc__, version='1')
    main(options)

1 回答

  • 4

    关于您的第一个问题,“默认情况下需要所有元素,如果不包含在括号中” . 所有使用线都是并行的,这意味着输入只需匹配任何一条使用线,它将被视为有效 . 因此,您需要在所有使用行中添加“必需参数”:

    Usage:
        docopt_example_1.py --required [--notrequired] [--ipaddress=IPADDRESS] [--computer=COMPUTER]
        docopt_example_1.py --required [--notrequired] --network <network>
        docopt_example_1.py --version
    

    在上面的示例中,如果您的输入不是“docopt_example_1.py --version”且不包含“--required”,则会在运行时打印docstring .


    关于第二个问题,使用选项名称读取选项的参数:

    print("computer name: {computer}".format(computer=options["--computer"]))
    print("network name: {network}".format(network=options["--network"]))
    

    此外,您可能需要处理来自官方网站的说明:

    写入 - 输入ARG(与--input = ARG相对)是不明确的,这意味着无法判断ARG是选项的参数还是位置参数 . 在使用模式中,只有在提供该选项的选项说明(如下所述)时,才会将此参数解释为带参数的选项 . 否则,它将被解释为单独的选项和位置参数 .

    如果您删除此行,帮助会理解它

    --network=NETWORK       network identification
    

    输入是 docopt_example_1.py --required [--notrequired] --network network_name ,那么你将无法从 options['--network'] 读取"network_name",而是需要使用 options['<network>'] . 因为在这种情况下"network_name"被认为是一个单独的位置参数 .


    顺便说一句,以下几行似乎有逻辑错误 . else 仅与最后一个 if 结合 . 我认为这不是你的预期:

    if options["--notrequired"]:
        print("required option selected")
    if options["--computer"]:
        print("computer name: {computer}".format(computer=options["COMPUTER"]))
    if options["--network"]:
        print("computer name: {network}".format(network=options["<network>"]))
    else:
        print("no options")
    

    参考:http://docopt.org/

相关问题