This is my Ruby code:
require 'thor'
require 'thor/group'
module CLI
class Greet < Thor
desc 'hi', 'Say hi!'
method_option :name, :type => :string, :description => 'Name to greet', :default => 'there'
def hi
puts "Hi, #{options[:name]}!"
end
desc 'bye', 'say bye!'
def bye
puts "Bye!"
end
end
class Root < Thor
register CLI::Greet, 'greet', 'greet [COMMAND]', 'Greet with a command'
end
end
CLI::Root.start
This is the output:
C:\temp>ruby greet.rb help greet
Usage:
greet.rb greet [COMMAND]
Greet with a command
How do I get that to look something like this?
C:\temp>ruby greet.rb help greet
Usage:
greet.rb greet [COMMAND]
--name Name to greet
Greet with a command
You’ve got two things going on here. First, you’ve assigned –name to a method, not to the entire
CLI::Greetclass. So if you use the command:you get
Which, yes, is wrong– it doesn’t have the subcommand in the help. There’s a bug filed for this in Thor. It, however, is showing the method option properly.
What it seems like you’re looking for, however, is a class method. This is a method defined for the entire
CLI::Greetclass, not just for the#himethod.You’d do this as such:
With this,
ruby greet.rb help greetreturnsNote there is still a hack needed here: the
tasks["greet"].options = CLI::Greet.class_optionsline inCLI::Root. There’s a bug filed for this in Thor, too.