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

时间:2014-06-29 17:03:42

标签: python arguments options docopt

我是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 个答案:

答案 0 :(得分:4)

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

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

在上面的示例中,如果您的输入不是&#34; docopt_example_1.py --version&#34;并且不包含&#34; - required&#34;,它将在运行时打印文档字符串。


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

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,那么您将无法阅读&#34; network_name&#34;来自options['--network'],您需要使用options['<network>']。因为在这种情况下&#34; network_name&#34;被认为是一个单独的位置论证。


顺便说一句,以下几行似乎有逻辑错误。 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/

相关问题