gpt4 book ai didi

ruby - 向 Ruby Thor CLI 添加 --version 选项

转载 作者:数据小太阳 更新时间:2023-10-29 06:40:23 24 4
gpt4 key购买 nike

如何向我的 Ruby 添加 --version 选项 Thor命令行界面应用程序。

例如我希望能够运行

$ thor_app --version
> thor_app version 1.0.0

这个问题与Run a CLI Thor app without arguments or task name有关,但专门添加一个不需要任务的 --version 选项。

注意
这是在 self-answer format 之后编写的.鼓励添加答案和更新

最佳答案

我在这种方法上运气不错:

class CLI < Thor
map %w[--version -v] => :__print_version

desc "--version, -v", "print the version"
def __print_version
puts FooBar::VERSION
end
end

前导下划线确保没有像 yourapp version 这样的命令,并强制执行 yourapp --versionyourapp -vdesc 内容将允许它显示为 -v, --version 而不会暴露 __print_version

关于ruby - 向 Ruby Thor CLI 添加 --version 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22809972/

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