首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >docopt必需选项和带有参数的选项

docopt必需选项和带有参数的选项
EN

Stack Overflow用户
提问于 2014-06-29 17:03:41
回答 1查看 6K关注 0票数 0

我刚开始学习docopt,很难找到一个很小的例子。我现在有两个小问题,欢迎在这些问题上提供帮助,并就如何改进代码提出更多的一般性意见。第一个问题是让程序需要--required选项。它应该在没有所需的命令行选项的情况下在运行时打印docstring。第二个问题是让程序接受选项(如COMPUTER)的参数(如--computer)。如何在终端中指定这一点,以及应该如何编码?

代码语言:javascript
复制
#!/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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-30 10:38:24

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

代码语言:javascript
复制
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。

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

代码语言:javascript
复制
print("computer name: {computer}".format(computer=options["--computer"]))
print("network name: {network}".format(network=options["--network"]))

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

写-输入ARG (反对-输入= ARG )是模棱两可的,这意味着无法判断ARG是选项的参数还是位置参数。在使用模式中,这将被解释为带有参数的选项,但前提是提供了该选项的选项描述(如下所述)。否则,它将被解释为单独的选项和位置参数。

如果删除这一行,则帮助理解它。

代码语言:javascript
复制
--network=NETWORK       network identification

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

顺便说一句,下面的行似乎有逻辑错误。else只与最后一个if结合。我想这不是你所期望的:

代码语言:javascript
复制
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/

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24478305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档