如何为点击中的一个选项创建短期和长期选项?

时间:2017-03-06 01:44:33

标签: python python-click

如何为同一选项指定短选项和长选项? 例如,对于以下内容,我还想将-c用于--count

import click

@click.command()
@click.option('--count', default=1, help='count of something')
def my_command(count):
    click.echo('count=[%s]' % count)

if __name__ == '__main__':
    my_command()

如,

$ python my_command.py --count=2
count=[2]
$ python my_command.py -c 3
count=[3]

参考文献:
click documentation in a single pdf
click sourcecode on github
click website
click PyPI page

1 个答案:

答案 0 :(得分:10)

这没有详细记录,但非常直接:

@click.option('--count', '-c', default=1, help='count of something')

测试代码:

@click.command()
@click.option('--count', '-c', default=1, help='count of something')
def my_command(count):
    click.echo('count=[%s]' % count)

if __name__ == '__main__':
    my_command(['-c', '3'])

<强>结果:

count=[3]