【发布时间】:2018-10-30 17:30:53
【问题描述】:
如何将默认选项设置为 -h 用于 Python 点击?
默认情况下,当duh.py 没有参数时,我的脚本不显示任何内容:
import click
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS)
@click.option('--toduhornot', is_flag=True, help='prints "duh..."')
def duh(toduhornot):
if toduhornot:
click.echo('duh...')
if __name__ == '__main__':
duh()
[出]:
$ python3 test_click.py -h
Usage: test_click.py [OPTIONS]
Options:
--toduhornot prints "duh..."
-h, --help Show this message and exit.
$ python3 test_click.py --toduhornot
duh...
$ python3 test_click.py
问题:
如上图,默认不打印信息python3 test_click.py。
有没有这样的方法,如果没有给出参数,默认选项设置为-h,例如
$ python3 test_click.py
Usage: test_click.py [OPTIONS]
Options:
--toduhornot prints "duh..."
-h, --help Show this message and exit.
【问题讨论】:
-
那么如果没有给出任何选项,那么你想默认帮助吗?你说:如果没有给出参数,但是你的例子只定义了一个选项,它没有定义任何参数。
标签: python command-line-interface argparse python-click