【问题标题】:Is it possible to tell whether the user set an option using docopt?是否可以判断用户是否使用 docopt 设置了选项?
【发布时间】:2017-01-27 06:02:11
【问题描述】:

考虑以下简单程序。

from docopt import docopt

doc = """
Usage: ./opt.py [options]

    -h,--help         show this
    -a <option>       generic option [default: Foo]
"""

options = docopt(doc)
print options['-a']
print options

是否可以在我的程序中区分以下两个用户调用?

python Opt.py -a Foo
python Opt.py

【问题讨论】:

    标签: python docopt


    【解决方案1】:

    如果定义了默认值,则无法区分。但是您可以从文档字符串中删除默认值并以编程方式设置它。

    from docopt import docopt
    
    doc = """
    Usage: ./opt.py [options]
    
        -h,--help         show this
        -a <option>       generic option
    """
    
    options = docopt(doc)
    if options['-a']:
        # option was defined
        ...
    else:
        # option was not defined
        options['-a'] = "Foo"
        ...
    

    但无论如何,对于哪个用例来说,这根本是必要的?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多