[英]How can I use a flag as a command with Thor
Given a Ruby program using Thor, how can I implement a method that gets called when an argument that looks like a flag is called. 给定使用Thor的Ruby程序,当调用看起来像标志的参数时,如何实现被调用的方法。
For example, if I run this on the command line: 例如,如果我在命令行上运行此命令:
mycmd --version
I would like to execute the code: 我想执行代码:
desc 'version', 'Print version number'
def version
puts "mycmd version #{Mycmd::VERSION}"
end
You can make a "top level" default task, which examines its arguments and outputs the correct thing: 您可以创建一个“顶级”默认任务,该任务检查其参数并输出正确的内容:
class MyThing < Thor
desc "meta", "Information about the task itself"
argument :name
def meta
if name == "--version"
puts "v 1.1.1"
elsif name == "--author"
puts "meagar"
end
end
default_task :meta
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.