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

Python docopt必需选项和带有参数的选项有困难,python,arguments,options,docopt,Python,Arguments,Options,Docopt,我是docopt的新手,要让一个小示例工作起来有点困难。我刚才遇到了两个小问题,希望您能对这些问题提供帮助,并对改进代码提出更一般性的意见。第一个问题是让程序需要--required选项。它应该在运行时打印docstring,而不使用所需的命令行选项。第二个问题是让程序接受选项(例如--COMPUTER)的参数(例如-COMPUTER)。如何在终端中指定,以及如何对其进行编码 #!/usr/bin/env python # -*- coding: utf-8 -*- """ my example

我是docopt的新手,要让一个小示例工作起来有点困难。我刚才遇到了两个小问题,希望您能对这些问题提供帮助,并对改进代码提出更一般性的意见。第一个问题是让程序需要
--required
选项。它应该在运行时打印docstring,而不使用所需的命令行选项。第二个问题是让程序接受选项(例如
--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)
#/usr/bin/env python
#-*-编码:utf-8-*-
"""
我的示例程序
用法:
docopt_示例_1.py[--ipaddress=ipaddress][--computer=computer]
docopt_示例_1.py--网络
docopt_示例_1.py(--必需)
docopt_示例_1.py--不需要
docopt_示例_1.py--版本
论据:
IP地址IP地址
计算机识别
网络识别
选项:
-h、 --帮助显示此帮助消息。
--版本显示版本并退出。
--运行所需的必需选项
--notrequired选项运行时不需要
--IP地址=IP地址IP地址
--计算机识别
--网络=网络标识
"""
从docopt导入docopt
def干管(选件):
打印(------------)
print(“由docopt解析的命令行选项的打印输出:”)
打印(选项)
打印(------------)
如果选项[“--notrequired”]:
打印(“所选必需选项”)
如果选项[“--计算机”]:
打印(“计算机名:{computer}”。格式(computer=options[“computer”]))
如果选项[“--network”]:
打印(“计算机名:{network}”。格式(network=options[“”]))
其他:
打印(“无选项”)
如果名称=“\uuuuu main\uuuuuuuu”:
options=docopt(\uuuu doc\uuuuu,version='1')
主要(选项)

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

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

此外,您可能需要注意官方网站上的注释:

写入--input ARG(与--input=ARG相对)是不明确的,这意味着 无法判断ARG是选项的参数还是位置参数 论点在使用模式中,这将被解释为带有 仅当选项的描述(包含在下面)用于该选项时,才使用参数 提供。否则将被解释为单独的选项和 位置参数

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

--network=NETWORK       network identification
输入是
docopt\u example\u 1.py--required[--notrequired]--network network\u name
,那么您将无法从
options['--network']
中读取“network\u name”,而需要使用
options['']
。因为在本例中,“网络名称”被视为一个单独的位置参数


顺便说一句,以下几行似乎有逻辑错误。
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")
如果选项[“--notrequired”]:
打印(“所选必需选项”)
如果选项[“--计算机”]:
打印(“计算机名:{computer}”。格式(computer=options[“computer”]))
如果选项[“--network”]:
打印(“计算机名:{network}”。格式(network=options[“”]))
其他:
打印(“无选项”)

参考资料:

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

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

此外,您可能需要注意官方网站上的注释:

写入--input ARG(与--input=ARG相对)是不明确的,这意味着 无法判断ARG是选项的参数还是位置参数 论点在使用模式中,这将被解释为带有 仅当选项的描述(包含在下面)用于该选项时,才使用参数 提供。否则将被解释为单独的选项和 位置参数

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

--network=NETWORK       network identification
输入是
docopt\u example\u 1.py--required[--notrequired]--network network\u name
,那么您将无法从
options['--network']
中读取“network\u name”,而需要使用
options['']
。因为在本例中,“网络名称”被视为一个单独的位置参数


顺便说一句,以下几行似乎有逻辑错误。
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")
如果选项[“--notrequired”]:
打印(“所选必需选项”)
如果选项[“--计算机”]:
打印(“计算机名:{computer}”。格式(computer=options[“computer”]))
如果选项[“--network”]:
打印(“计算机名:{network}”。格式(network=options[“”]))
其他:
打印(“无选项”)

参考资料:

Ah,非常好。非常感谢你的帮助。我还没有看到使用线是并行考虑的。非常感谢你的其他建议。啊,太好了。非常感谢你的帮助。我还没有看到使用线是并行考虑的。非常感谢您的其他建议。