一种实现目标的方法是从click.Option继承,并自定义解析器。
自订类别:使用自定义类:import clickclass OptionEatAll(click.Option): def __init__(self, *args, **kwargs): self.save_other_options = kwargs.pop('save_other_options', True) nargs = kwargs.pop('nargs', -1) assert nargs == -1, 'nargs, if set, must be -1 not {}'.format(nargs) super(OptionEatAll, self).__init__(*args, **kwargs) self._previous_parser_process = None self._eat_all_parser = None def add_to_parser(self, parser, ctx): def parser_process(value, state): # method to hook to the parser.process done = False value = [value] if self.save_other_options: # grab everything up to the next option while state.rargs and not done: for prefix in self._eat_all_parser.prefixes: if state.rargs[0].startswith(prefix): done = True if not done: value.append(state.rargs.pop(0)) else: # grab everything remaining value += state.rargs state.rargs[:] = [] value = tuple(value) # call the actual process self._previous_parser_process(value, state) retval = super(OptionEatAll, self).add_to_parser(parser, ctx) for name in self.opts: our_parser = parser._long_opt.get(name) or parser._short_opt.get(name) if our_parser: self._eat_all_parser = our_parser self._previous_parser_process = our_parser.process our_parser.process = parser_process break return retval
要使用自定义类,请将
cls参数传递给
@click.option()decorator,如下所示:
@click.option("--an_option", cls=OptionEatAll)
或者如果希望该选项将占用整个命令行的其余部分,而不尊重其他选项:
这是如何运作的?@click.option("--an_option", cls=OptionEatAll, save_other_options=False)
之所以可行,是因为click是一个设计良好的OO框架。该
@click.option()装饰通常实例化一个
click.Option对象,但允许这种行为与CLS参数超过缠身。因此,
click.Option在我们自己的类中继承并超越所需的方法是相对容易的事情。
在这种情况下,我们骑车
click.Option.add_to_parser()而猴子则对解析器进行修补,以便在需要时可以吃多个令牌。测试代码:
试验结果:@click.command()@click.option('-g', 'greedy', cls=OptionEatAll, save_other_options=False)@click.option('--polite', cls=OptionEatAll)@click.option('--other')def foo(polite, greedy, other): click.echo('greedy: {}'.format(greedy)) click.echo('polite: {}'.format(polite)) click.echo('other: {}'.format(other))if __name__ == "__main__": commands = ( '-g a b --polite x', '-g a --polite x y --other o', '--polite x y --other o', '--polite x -g a b c --other o', '--polite x --other o -g a b c', '-g a b c', '-g a', '-g', 'extra', '--help', ) import sys, time time.sleep(1) print('Click Version: {}'.format(click.__version__)) print('Python Version: {}'.format(sys.version)) for cmd in commands: try: time.sleep(0.1) print('-----------') print('> ' + cmd) time.sleep(0.1) foo(cmd.split()) except baseException as exc: if str(exc) != '0' and not isinstance(exc, (click.ClickException, SystemExit)): raise
Click Version: 6.7Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]-----------> -g a b --polite xgreedy: ('a', 'b', '--polite', 'x')polite: Noneother: None-----------> -g a --polite x y --other ogreedy: ('a', '--polite', 'x', 'y', '--other', 'o')polite: Noneother: None-----------> --polite x y --other ogreedy: Nonepolite: ('x', 'y')other: o-----------> --polite x -g a b c --other ogreedy: ('a', 'b', 'c', '--other', 'o')polite: ('x',)other: None-----------> --polite x --other o -g a b cgreedy: ('a', 'b', 'c')polite: ('x',)other: o-----------> -g a b cgreedy: ('a', 'b', 'c')polite: Noneother: None-----------> -g agreedy: ('a',)polite: Noneother: None-----------> -gError: -g option requires an argument-----------> extraUsage: test.py [OPTIONS]Error: Got unexpected extra argument (extra)-----------> --helpUsage: test.py [OPTIONS]Options: -g TEXT --polite TEXT --other TEXT --help Show this message and exit.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)