2017-02-24 5 views
0

하나 개의 함수를 호출, 기본적으로 내가 원하는 한 번만 내가 제공 한 얼마나 많은 CMDS 중요하지라고어떻게 파이썬 클릭으로 하위 인수를 가지고로 <code>main_caller</code> 만 내가 인수로 <code>click</code> 그래서</p> <p>를 파이썬에 전달 된 명령의 수에 대해 하나의 함수를 호출하는 방법을 알아 내려고 노력하고 있어요

# 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() 

아웃

('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'}) 
+1

mmm, cli에 main_caller를 호출하지 않습니까? – Copperfield

답변

0

답을 찾을 수있다. 그것은 사용했다 resultcallback()

# 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']) 
def cli(*args, **kwargs): 
    '''foo bar''' 
    pass 

@cli.command() 
@click.option('--debug/--no-debug', default=False) 
def cmd1(*args, **kwargs): 
    print('cmd1', args, kwargs) 
    return 'cmd11111' 

@cli.command() 
@click.option('-x', '--xxx', default='x') 
def cmd2(*args, **kwargs): 
    print('cmd2', args, kwargs) 
    return 'cmd22222' 

@cli.resultcallback() 
def process_pipeline(*args, **kwargs): 
    print('process', args, kwargs) 
#  iterator = (x.rstrip('\r\n') for x in input1) 
#  for processor in processors: 
#   iterator = processor(iterator) 
#  for item in iterator: 
#   click.echo(item) 


if __name__ == '__main__': 
    cli()