Ruby Thor-方法中未识别命令行选项
我必须使用此命令来运行我的Ruby Thor-方法中未识别命令行选项,ruby,thor,Ruby,Thor,我必须使用此命令来运行我的ruby程序: $ruby filename.rb NAME --from="People" --yell 我有这样的剧本: require 'thor' class CLI < Thor desc "hello NAME", "say hello to NAME" method_option :from, :required => true method_option :yell, :type => :boolean def s
ruby
程序:
$ruby filename.rb NAME --from="People" --yell
我有这样的剧本:
require 'thor'
class CLI < Thor
desc "hello NAME", "say hello to NAME"
method_option :from, :required => true
method_option :yell, :type => :boolean
def self.hello(name)
output = []
output << "from: #{options[:from]}" if options[:from]
output << "Hello #{name}"
output = output.join("\n")
puts options[:yell] ? output.upcase : output
end
end
CLI.hello(ARGV)
require'thor'
类CLItrue
方法_选项:yell,:type=>:boolean
def self.hello(姓名)
输出=[]
output ruby testing.rb Jay--from=“Ray”--yell
发件人:#
你好[“杰”,“--FROM=RAY”,“--YELL”]
它看起来像是:无论我是否指定,yell
始终有效,选项
在hello
方法中都被解读为名称
输入
require 'thor'
class CLI < Thor
desc "hello NAME", "say hello to NAME"
method_option :from, :required => true
method_option :yell, :type => :boolean
def hello(name)
#do something
end
end
CLI.start ARGV
我从在线教程中找到并尝试了许多方法,但问题没有解决。请告诉我出了什么问题。谢谢大家! 问题是由我在脚本中调用
CLI.hello ARGV
引起的。当程序运行时,它将调用hello
方法,并将所有命令行输入识别为hello
的参数,该参数是一个数组
解决此问题的方法之一是通过删除通过start
方法调用脚本的self
来公开hello
require 'thor'
class CLI < Thor
desc "hello NAME", "say hello to NAME"
method_option :from, :required => true
method_option :yell, :type => :boolean
def hello(name)
#do something
end
end
CLI.start ARGV
require'thor'
类CLItrue
方法_选项:yell,:type=>:boolean
def你好(姓名)
#做点什么
结束
结束
CLI.start ARGV