- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试向 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/
将 .off 与 .click 结合使用有何用途或区别? $('#link').off('click').on('click',function(){}); 对比 $('#link').on('cli
我正在学习 javascript,并且我见过不止一种注册点击事件的方法: $(DOMelement).click(function() {}); $(DOMelement).on('click',fu
我习惯使用.click()和delegate('click') ,所以当我读到这两个在 jQuery 的最新版本中都被弃用时,我想我应该读一下它,但我有点摸不着头脑。 文档 here似乎表明这是 .l
我已经使用它们很长一段时间了,但大多数时候,我更喜欢较短的,但是,我只是想真正深入了解本质细节。我可能一直在创建有错误的代码,并且我不想在网络上贡献和传播懒惰完成的代码。 所以,告诉我: What a
我想实现类似在 Android 上点击 Instagram 中的帖子的功能(我认为在 iOS 上应该是相同的功能)。在 Instagram 中,如果有人点击照片,它会打开,双击,它会被喜欢,然后单击并
我的点击事件有问题。它运行 1 次。示例: 我有 3 页(1 2 3)。当我点击 2 时它起作用了。然后在第 3 页,它又可以工作了。但我再次点击 2 不起作用。该事件未触发。 $("div .top
我正在尝试这样做。在 jsfiddle 中:http://jsfiddle.net/QGGhW/2/ 但是在我点击 not now 之后,下一个文本输入就不再可点击了。 我该怎么做?是否有类似 .on
我想知道是否有任何情况下使用 .click(function {...}); 而不是 .live('click', function { ...});? 据我所知,实时选项似乎是一个更好的选择,因此我
我正在尝试在 Accordion 内部创建一个 Accordion ......并且我有点挣扎。 本质上,我有一个 div .applicant,单击它会添加一个 .expand 类,它将高度设置为
我可以使用一些帮助来调试这个: var factBody = jQuery(".fact__body"); jQuery(".fact").on("click", function() { j
如果我有一个用于 clicked 类的监听器,以及另一个用于 click-option-1、click-option-2 类的监听器,以及click-option-3,如何在我的 clicked 事件
下面的代码有什么区别吗? $('#whatever').on('click', function() { /* your code here */ }); 和 $('#whatever').
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我不擅长 UI 设计,所以我使用 bootstrap 和 jquery 来帮助我一点。我正在尝试将 jQuery 事件绑定(bind)到导航栏,因此当用户单击导航栏上的 p 标签时,它应该触发单击事件
问题是,当我单击 delaccbut 按钮时,单击功能起作用并显示消息,但是当我单击 confdel 或 redel 按钮来自 click 函数,它不...为什么? HTML: Delete my
我想要完成的是“类似”的东西 $("button .toggleExcerpt) .toggle( function FIRST() { do first function..
function show1(){ $("#btn").click(show2()); window.alert(
我不明白为什么这不起作用。当我单击具有该类名的复选框时,clickId 被赋予值access-toggle-all,所以这没有意义(至少对我来说).. $(document).ready(functi
我有一个关于 click() 的复杂问题。我有 4 个按钮,分别命名为功能 1、功能 2、方法 1 和方法 2。 Function 1 Function 2 Method 1 Method 2 当单
最后,我认为这不是我特别需要解决的问题,但我不明白为什么会这样,这让我很困扰。 基本上,我有一些复选框,我只希望用户能够选择其中的一定数量。我正在使用下面的代码来实现该效果。 $j( function
我是一名优秀的程序员,十分优秀!