【问题标题】:How to set only specific values for a parameter in docopt python?如何在docopt python中只为参数设置特定值?
【发布时间】:2023-03-11 17:18:01
【问题描述】:

我正在尝试将 docopt 用于 python 代码。我实际上只需要为参数设置特定值。我的用法如下:

"""
Usage:
test.py --list=(all|available)

Options:
    list    Choice to list devices (all / available)
"""

我尝试将其运行为:python test.py --list=all,但它不接受该值,只显示文档字符串。

我希望列表参数的值为“全部”或“可用”。有什么办法可以实现吗?

【问题讨论】:

标签: python docopt


【解决方案1】:

这是实现您想要的示例:

test.py:

"""
Usage:
  test.py list (all|available)

Options:
  -h --help     Show this screen.
  --version     Show version.

  list          Choice to list devices (all / available)
"""
from docopt import docopt

def list_devices(all_devices=True):
    if all_devices:
        print("Listing all devices...")
    else:
        print("Listing available devices...")


if __name__ == '__main__':
    arguments = docopt(__doc__, version='test 1.0')

    if arguments["list"]:
        list_devices(arguments["all"])

使用此脚本,您可以运行如下语句:

python test.py list all

或:

python test.py list available 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多