2017-03-06 70 views
1

如何爲同一選項指定短期權和長期權? 例如,對於下面,我也想用-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

回答

3

這不是有據可查的,但也是相當直截了當:

@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] 
+0

完美!謝謝@Stephen_Rauch! –

相關問題