简体   繁体   English

如何从python调用python click?

[英]How to call python click from from python?

Correct me if I'm wrong, but it seem that click decorators obscure the function default and construction. 如果我错了,请纠正我,但单击装饰器似乎会使功能的默认设置和构造模糊。 Call the wrapped from from the REPL does not immediately work in the same way as the REPL. 从REPL调用包装后的代码不能立即以与REPL相同的方式工作。 For example see this from dask distributed: 例如,从dask分发中查看此内容:

@click.option('--bokeh-prefix', type=str, default=None,
              help="Prefix for the bokeh app")
@click.option('--preload', type=str, multiple=True, is_eager=True,
              help='Module that should be loaded by each worker process '
                   'like "foo.bar" or "/path/to/foo.py"')
@click.argument('preload_argv', nargs=-1,
                type=click.UNPROCESSED, callback=validate_preload_argv)
def main(scheduler, host, worker_port, listen_address, contact_address,
         nanny_port, nthreads, nprocs, nanny, name,
         memory_limit, pid_file, reconnect, resources, bokeh,
         bokeh_port, local_directory, scheduler_file, interface,
         death_timeout, preload, preload_argv, bokeh_prefix, tls_ca_file,
         tls_cert, tls_key): 

Is there some simple pattern to get at the function and all its defaults? 是否有一些简单的模式可以使用该函数及其所有默认值?

As far as I can surmise the only alternatives, from this issue , are to 据我推测,从这个问题出发,唯一的选择是

  1. Call the underlying function like main.callback(*args) . 调用诸如main.callback(*args)类的基础函数。 Then you're missing default arguments from the click interface. 然后,您会从点击界面中丢失默认参数。

  2. To use the click testing interface via something like click.testing.CliRunner().invoke(main, ['--host', '127.0.0.1'], catch_exceptions=False) . 要通过click.testing.CliRunner().invoke(main, ['--host', '127.0.0.1'], catch_exceptions=False)类的东西使用点击测试界面。

I suppose from my point of view, option 1 is the best we can do; 从我的角度来看,我认为选项1是我们能做的最好的事情; default parameters just have to be done manually. 默认参数只需手动完成。 I would be curious too if there were any better alternatives though. 如果有更好的选择,我也很好奇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM