[英]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
投诉。
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
,而不是针对您的整个代码库。
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
在做什么,但似乎它正在尝试:
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
。
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.