This question started out here. But changed significantly as I learned more about Thor.
I’m trying to make a Thor::Group subcommand that takes an argument. Oddly, it works if there are no arguments.
Can I use a Thor::Group as a sub-command?
This works when I type: foo counter
foo/bin/foo
module Foo
class CLI < Thor
register(Counter, 'counter', 'counter', 'Count up from the 1.')
end
class Counter < Thor::Group
desc "Prints 1 2"
def one
puts 1
end
def two
puts 2
end
end
end
Foo::CLI.start
But this doesn’t work when I type: foo counter 5
module Foo
class CLI < Thor
register(Counter, 'counter', 'counter <number>', 'Count up from the input.')
end
class Counter < Thor::Group
argument :number, :type => :numeric, :desc => "The number to start counting"
desc "Prints 2 numbers based on input"
def one
puts number + 0
end
def two
puts number + 1
end
end
end
Foo::CLI.start
It replies: counter was called incorrectly. Call as foo counter number
I have a solution. Instead of using Thor::Group I’m using Invocations
bin/foo looks like this:
lib/cli.rb – registers ‘generate’ as a subtask of of the base command, foo:
lib/generate.rb looks like this:
Now I can type:
foo generate project fredand it will output:
Notice the banner override. It means that typing:
foo generate projectwith invalid or missing args will give the correct help message:as opposed to