每个函数的python click模块输入 [英] python click module input for each function

查看:76
本文介绍了每个函数的python click模块输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是目前在Click模块上工作的python新手.因此,在这里我有一个疑问,即仅为主要cli函数提供输入.但是我想一一提供所有功能的输入.可以点击吗?感谢您的进阶.

  @ click.option('-create',默认='sub',help ='Create')@ click.command()def create(创建):click.echo('创建被叫')os.system('curl http://127.0.0.1:5000/create')@ click.option('-conn',默认='in',help ='连接到服务器')@ click.command()def conn(conn):click.echo(称为conn")os.system('curl http://127.0.0.1:5000/') 

和我的setup.py

来自setuptools导入设置的

 设置(name ="hello",版本='0.1',py_modules = ['hello'],install_requires = ['点击',],entry_points ='''[console_scripts]hello = hello:cli''',) 

我的输出期望

  $ hello --conn输入成功你好-创建子成功 

解决方案

听起来像我想要基于提供给 hello cli的输入的不同命令.因此,Click具有有用的组概念,即可以调用的命令集合.

您可以按以下方式重新组织代码:

 @ click.group()def cli():经过@ cli.command()def create():click.echo('创建被叫')os.system('curl http://127.0.0.1:5000/create')@ cli.command()def conn():click.echo(称为conn")os.system('curl http://127.0.0.1:5000/')def main():值= click.prompt('选择要运行的命令',键入= click.Choice(list(cli.commands.keys())+ ['exit']))而值!='退出':cli.commands [value]()如果__name__ =="__main__":主要的() 

呼叫将是:

  $ hello con$ hello创建 

您似乎不需要使用这些选项,因为您不会根据传入或不传入的选项来更改每个命令的行为.有关更多信息,请参阅命令和组单击文档

I'm a new bee for python currently working on the Click module. So here I have a doubt to providing input for the main cli function only. But I want to provide the input for my all the function one by one. is it possible to click? Thanks for advance.

@click.option('--create', default='sub', help='Create')
@click.command()
def create(create):
    click.echo('create called')
    os.system('curl http://127.0.0.1:5000/create')   
@click.option('--conn', default='in', help='connect to server')
@click.command()
def conn(conn):
    click.echo('conn called')
    os.system('curl http://127.0.0.1:5000/')

and my setup.py

from setuptools import setup
setup(
     name="hello",
     version='0.1',
     py_modules=['hello'],
     install_requires=[
                    'Click',
     ],
     entry_points='''
     [console_scripts]
     hello=hello:cli
''',
)

My output expectation

$ hello --conn in
  success
  hello --create sub
  success

解决方案

Sounds to me like you want different commands based on input provided to your hello cli. For that reason, Click has the useful notion of a group, a collection of commands that can be invoked.

You can reorganize your code as follows:


@click.group()
def cli():
    pass

@cli.command()
def create():
    click.echo('create called')
    os.system('curl http://127.0.0.1:5000/create')

@cli.command()
def conn():
    click.echo('conn called')
    os.system('curl http://127.0.0.1:5000/')

def main():
    value = click.prompt('Select a command to run', type=click.Choice(list(cli.commands.keys()) + ['exit']))
    while value != 'exit':
        cli.commands[value]()

if __name__ == "__main__":
    main()

and the calls would be:

$ hello con
$ hello create

It doesn't seem like you need the options, as you don't change the behaviour of each command based on the option being passed in or not. For more information, please refer to the commands and groups Click documentation

这篇关于每个函数的python click模块输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆