简体   繁体   English

使用 Python 点击,如何添加超过 5 个选项,即超过 5 个?

[英]Using Python Click, how do I add more than 5 options i.e. more than 5?

Current code:当前代码:

@click.command()
@click.option('--logs', type=click.Choice(['all', 'errors', 'no-errors', 'archive',
                                           'archive-with-errors']))
@click.option('--process', type=click.Choice(['all', 'fix', 'spool', 'status', 'import']))
@click.option('--quiet', is_flag=True)
@click.option('--check', is_flag=True)
@click.option('--scan', nargs=1)
def main(logs, process, quiet, check, scan):

If I add a 6th option to the main function, pylint complains about "Too many arguments (6/5) (52:0) [too-many-arguments]"如果我向主 function 添加第 6 个选项, pylint抱怨“arguments (6/5) (52:0) [too-many-arguments] 太多”

I do not want to add an exception to pylint config file.我不想向 pylint 配置文件添加异常。

I want to know how to pass more options to python @click.我想知道如何将更多选项传递给 python @click。 Or do I need to restructure my file, so each option has its own @click.command and its own python function?还是我需要重组我的文件,所以每个选项都有自己的@click.command和自己的 python function?

Sorry if this is duplicated, I could not find it since click is a very general term.对不起,如果这是重复的,我找不到它,因为点击是一个非常笼统的术语。

Not quite sure what are you trying to do with click, including usage of 'click' in main would be nice.不太确定你想用点击做什么,包括在 main 中使用“点击”会很好。

After testing module click, it just sends keyword arguments to decorated function 'main'.测试模块点击后,它只是将关键字 arguments 发送到修饰的 function 'main'。

Python gets keyword arguments as dictionary, so you normally use **kwargs to pass unlimited number of keyword arguments. Python 获取关键字 arguments 作为字典,因此您通常使用 **kwargs 传递无限数量的关键字 arguments。


Example:例子:

@click.command()
@click.option('--logs', type=click.Choice(['all', 'errors', 'no-errors', 'archive', 'archive-with-errors']))
@click.option('--process', type=click.Choice(['all', 'fix', 'spool', 'status', 'import']))
@click.option('--quiet', is_flag=True)
@click.option('--check', is_flag=True)
@click.option('--scan', nargs=1)
def main(*args, **kwargs):
    click.echo(kwargs)

Result:结果:

{'logs': None, 'process': None, 'quiet': False, 'check': False, 'scan': None} {'logs':无,'process':无,'quiet':False,'check':False,'scan':无}

Process finished with exit code 0进程以退出代码 0 结束


Your Code:您的代码:

@click.command()
@click.option('--logs', type=click.Choice(['all', 'errors', 'no-errors', 'archive', 'archive-with-errors']))
@click.option('--process', type=click.Choice(['all', 'fix', 'spool', 'status', 'import']))
@click.option('--quiet', is_flag=True)
@click.option('--check', is_flag=True)
@click.option('--scan', nargs=1)
def main(logs, process, quiet, check, scan):
    click.echo(locals())

Result:结果:

{'logs': None, 'process': None, 'quiet': False, 'check': False, 'scan': None} {'logs':无,'process':无,'quiet':False,'check':False,'scan':无}

Process finished with exit code 0进程以退出代码 0 结束


As you see, using **kwargs yields same results.如您所见,使用 **kwargs 会产生相同的结果。 I don't know if this will affect module's behavior, but at least arguments are passed correctly.我不知道这是否会影响模块的行为,但至少 arguments 被正确传递。 Try this out.试试这个。

I see 3 options to handle this pylint complaint.我看到 3 个选项来处理这个pylint投诉。

Option 1: Inline Suppression选项 1:内联抑制

This option does not require that you add an exception to your pylint config file.此选项不需要您向pylint配置文件添加例外。
However, it does require that you add this extra inline comment: # pylint: disable=too-many-arguments to your code as follows:但是,它确实需要您在代码中添加这个额外的内联注释: # pylint: disable=too-many-arguments ,如下所示:

@click.command()
@click.option('--logs', type=click.Choice(['all', 'errors', 'no-errors', 'archive',
                                           'archive-with-errors']))
@click.option('--process', type=click.Choice(['all', 'fix', 'spool', 'status', 'import']))
@click.option('--quiet', is_flag=True)
@click.option('--check', is_flag=True)
@click.option('--scan', nargs=1)
def main(logs, process, quiet, check, scan):
    # pylint: disable=too-many-arguments

This will disable too-many-arguments just for this function, not for your entire code base.这将仅针对此 function 禁用too-many-arguments ,而不是针对您的整个代码库。

Option 2: Refactor Your Code选项 2:重构代码

This option is a good choice whenever you have a complex command, which is doing many things, that also can be performed by multiple simple commands.当您有一个复杂的命令时,此选项是一个不错的选择,该命令执行许多操作,也可以由多个简单命令执行。

I'm not sure what your main @click.command is doing, but it seems as if it is trying to:我不确定您的main @click.command在做什么,但似乎它正在尝试:

  1. show logs显示日志
  2. process some things处理一些事情
  3. check some status检查一些状态
  4. scan something扫描东西

If that is the case, then could refactor this:如果是这样的话,那么可以重构这个:

@click.command()
@click.option('--logs', type=click.Choice(['all', 'errors', 'no-errors', 'archive',
                                           'archive-with-errors']))
@click.option('--process', type=click.Choice(['all', 'fix', 'spool', 'status', 'import']))
@click.option('--check', is_flag=True)
@click.option('--scan', nargs=1)
def main(logs, process, quiet, check, scan):

to:至:

# @click.command has become Wclick.group.
@click.group()
def main(): pass

# We split off the functionality from `main` into sub-commands.
@main.command()
@click.argument("logtype", 
                type=click.Choice(['all', 'errors', 'no-errors', 'archive',  
                                   'archive-with-errors']))
def logs(logtype): pass


@main.command()
@click.argument("process_category", 
                type=click.Choice(['all', 'fix', 'spool', 'status', 'import']))
def process(process_category): pass


@main.command()
def check(): pass


@main.command()
@click.argument("arg")
def scan(arg): pass

This would solve the too-many-arguments complain, without suppressing pylint .这将解决too-many-arguments抱怨,而不会抑制pylint
Now, with the refactored code, instead of invoking main --logs=archive you would invoke main logs archive .现在,使用重构的代码,而不是调用main --logs=archive您将调用main logs archive

Option 3:选项 3:

Use the *args **kwargs trick:使用*args **kwargs技巧:

@click.command()
@click.option('--logs', 
              type=click.Choice(['all', 'errors', 'no-errors', 'archive',
                                 'archive-with-errors']))
@click.option('--process', 
              type=click.Choice(['all', 'fix', 'spool', 'status', 'import']))
@click.option('--quiet', is_flag=True)
@click.option('--check', is_flag=True)
@click.option('--scan', nargs=1)
def main(*args, **kwargs):

as explained by @jupiterbjy in this answer正如@jupiterbjy 在这个答案中所解释的

This option would be suitable if you intend to pass the *args and **kwargs unchanged anyway.如果您打算以不变的方式传递*args**kwargs ,则此选项将是合适的。
If you would first pack *args and **kwargs in the function signature, and then unpack them manually, just to satisfy pylint , then you would be better off with either option 1 or 2.如果您首先将*args**kwargs打包到 function 签名中,然后手动解压缩它们以满足pylint ,那么您最好使用选项 1 或 2。

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

相关问题 如何在pygame的动画中添加两个以上的精灵? - How do I add more than 2 sprites into an animation in pygame? Python:如何迭代两个以上的词典? - Python: How do I iterate over more than 2 dictionaries? 如何替换字符串中的多个字符 Python - How do I replace more than one character in a string Python 如何在Python中将1个以上的输入加在一起 - How do I add inputs together from more than 1 input in Python "Python - 如何使用 slice 方法、find 和 rfind 在 python 上删除多个字符" - Python - How do i delete more than one character on python using slice method, find and rfind 如何在不使用python中的多个if / else语句的情况下多次生成随机列表? - How do I generate a random list more than once without using multiple if/else statements in python? 如何使用 python 在折线图中显示 label 超过 50% 的准确度? - how do i display the label that more than 50% accuracy in line chart using python? 在python中,如何使用for循环查找在句子中出现多次的单词的位置? - In python, how do I find the positions of a word that occurs more than once in a sentence using a for loop? 如何使用Python在CSV文件中跳过多行 - How do I skip more than 1 row in a CSV file using Python Python不会打印超过12个有效数字。 如何更轻松地打印? - Python won't print more than 12 significant figures. How do I print more easily?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM