0
我試圖找出如何調用只有一個功能的任何數量的傳遞到Python click
作爲參數如何在蟒蛇點擊子指定參數和只調用一個函數
這樣的命令,我想基本上main_caller
是隻調用一次不要緊多少CMDS我提供
# http://click.pocoo.org/5/commands/
import click
def main_caller(*args, **kwargs):
print('act on arguments', args, kwargs)
@click.group(help='wwwwwwwwww', epilog='wwwwxx', invoke_without_command=True, chain=True)
@click.option('-v', '--verbose', default=False, help='Print Verbose messages')
@click.option('-l', '--logfile', help='Path to logfile to store log messages')
@click.option('-a', '--action', multiple=True, type=click.Choice(['act1', 'act2', 'act3']), default=['act1', 'act2'])
@click.pass_context
def cli(*args, **kwargs):
'''foo bar'''
print('cli', args, kwargs)
main_caller(*args, **kwargs)
@cli.command()
@click.option('--debug/--no-debug', default=False)
@click.pass_context
def cmd1(*args, **kwargs):
print('cmd1', args, kwargs)
main_caller(*args, **kwargs)
@cli.command()
@click.option('-x', '--xxx', default='x')
@click.pass_context
def cmd2(*args, **kwargs):
print('cmd2', args, kwargs)
main_caller(*args, **kwargs)
if __name__ == '__main__':
cli()
Out是
('cli', (<click.core.Context object at 0x7f095a858150>,), {'action': ('act1', 'act2'), 'logfile': None, 'verbose': False})
('act on arguments', (<click.core.Context object at 0x7f095a858150>,), {'action': ('act1', 'act2'), 'logfile': None, 'verbose': False})
('cmd1', (<click.core.Context object at 0x7f095a858190>,), {'debug': False})
('act on arguments', (<click.core.Context object at 0x7f095a858190>,), {'debug': False})
('cmd2', (<click.core.Context object at 0x7f095a8581d0>,), {'xxx': u'x'})
('act on arguments', (<click.core.Context object at 0x7f095a8581d0>,), {'xxx': u'x'})
mmm,請不要在cli中調用main_caller? – Copperfield