gpt4 book ai didi

python - 如何将 OPTION 添加到 Click 类实现?

转载 作者:行者123 更新时间:2023-12-05 05:30:08 24 4
gpt4 key购买 nike

尝试向 click 的类实现添加“选项”。诚然,Python 不是我擅长的领域,但它是必须要做的。已经有一堆使用此类方法实现的“参数”。有人知道如何获得在这里工作的选项吗?

测试.py

import click

class OptionGroup(click.Option):
"""Customizing the default click option"""

def list_options(self, ctx: click.Context):
"""Sorts options in the specified order"""
# By default, click alphabetically sorts options
# This method will override that feature
return self.opts.keys()

@click.option(cls=OptionGroup)
def cli_opt():
"""Command Line Interface to configure options"""
pass

@cli_opt.command()
@click.option('-d', '--dest', 'dst-ip', type=str)
def dest_ip(dest_ip):
"""Specifies the destination controller IP address"""
print(f"Dest IP: ", dest_ip)
click.echo(dest_ip)

if __name__ == "__main__":
cli_opt()

这是我运行脚本时的输出...

$ python test.py --help
Traceback (most recent call last):
File "C:\Users\jfell\repos\test.py", line 13, in <module>
@click.option(cls=OptionGroup)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\decorators.py", line 308, in decorator
_param_memo(f, OptionClass(param_decls, **option_attrs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 2495, in __init__
super().__init__(param_decls, type=type, multiple=multiple, **attrs)
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 2072, in __init__
self.name, self.opts, self.secondary_opts = self._parse_decls(
^^^^^^^^^^^^^^^^^^
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 2640, in _parse_decls
raise TypeError("Could not determine name for option")
TypeError: Could not determine name for option

仅实现参数的类似代码工作正常......

import click

class CommandGroup(click.Group):
"""Customizing the default click group"""

def list_commands(self, ctx: click.Context):
"""Sorts commands in the specified order"""
# By default, click alphabetically sorts commands
# This method will override that feature
return self.commands.keys()


@click.group(cls=CommandGroup)
def cli():
"""Command Line Interface to send commands"""
pass


@cli.command("goto-mode")
@click.argument("mode", type=str)
def goto_mode(mode: str):
"""Directs Application Mode Change"""
click.echo(mode)


if __name__ == "__main__":
cli()

仅用于参数的输出...

$ python test.py goto-mode Success!
Success!

添加了选项的脚本...

import click

class OptionGroup(click.Option):
"""Customizing the default click option"""

def list_options(self, ctx: click.Context):
"""Sorts options in the specified order"""
# By default, click alphabetically sorts options
# This method will override that feature
return self.opts.keys()


@click.option(cls=OptionGroup)
def cli_opt():
"""Command Line Interface to configure options"""
pass


@cli_opt.command()
@click.option('--dest', '-d', 'dst-ip', type=str)
def dest_ip(dest_ip):
"""Specifies the destination controller IP address"""
print(f"Dest IP: ", dest_ip)
click.echo(dest_ip)


class CommandGroup(click.Group):
"""Customizing the default click group"""

def list_commands(self, ctx: click.Context):
"""Sorts commands in the specified order"""
# By default, click alphabetically sorts commands
# This method will override that feature
return self.commands.keys()


@click.group(cls=CommandGroup)
def cli():
"""Command Line Interface to send commands"""
pass


@cli.command("goto-mode")
@click.argument("mode", type=str)
def goto_mode(mode: str):
"""Directs Application Mode Change"""
click.echo(mode)


if __name__ == "__main__":
cli_opt()
cli()

...产生同样的错误...

$ python test.py --help
Traceback (most recent call last):
File "C:\Users\jfell\repos\test.py", line 13, in <module>
@click.option(cls=OptionGroup)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\decorators.py", line 308, in decorator
_param_memo(f, OptionClass(param_decls, **option_attrs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 2495, in __init__
super().__init__(param_decls, type=type, multiple=multiple, **attrs)
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 2072, in __init__
self.name, self.opts, self.secondary_opts = self._parse_decls(
^^^^^^^^^^^^^^^^^^
File "C:\Users\jfell\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 2640, in _parse_decls
raise TypeError("Could not determine name for option")
TypeError: Could not determine name for option

最佳答案

我很想看到更好的方法来做到这一点,但到目前为止,我能想到的最好的方法是为所有点击命令添加所需的选项。

例如……

@cli.command("goto-mode")
@click.argument("mode", type=str)
@click.option('--dest', '-d', 'dst-ip', type=str, default="192.168.8.8")
def goto_mode(mode: str, dest_ip: str):
"""Directs Application Mode Change"""
click.echo(mode)
click.echo(dest_ip)

虽然它有效,但它有点笨拙,因为必须将选项添加到每个命令,这也造成了维护上的麻烦。

关于python - 如何将 OPTION 添加到 Click 类实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74773394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com