Namespacing thor commands in a standalone ruby executable
Another way of doing this is to use register:
class CLI < Thor
register(SubTask, 'sub', 'sub <command>', 'Description.')
end
class SubTask < Thor
desc "bar", "..."
def bar()
# ...
end
end
CLI.start
Now - assuming your executable is called foo - you can call:
$ foo sub bar
In the current thor version (0.15.0.rc2) there is a bug though, which causes the help texts to skip the namespace of sub commands:
$ foo sub
Tasks:
foo help [COMMAND] # Describe subcommands or one specific subcommand
foo bar #
You can fix that by overriding self.banner and explicitly setting the namespace.
class SubTask < Thor
namespace :sub
def bar ...
def self.banner(task, namespace = true, subcommand = false)
"#{basename} #{task.formatted_usage(self, true, subcommand)}"
end
end
The second parameter of formatted_usage is the only difference to the original implemtation of banner. You can also do this once and have other sub command thor classes inherit from SubTask. Now you get:
$ foo sub
Tasks:
foo sub help [COMMAND] # Describe subcommands or one specific subcommand
foo sub bar #
Hope that helps.
Is it possible to invoke Thor commands from my Web App?
You could try this:
MyClass.start(args, config)
args is an array of strings that represent the options you would pass on the command line, config is a hash.
MyClass.start(["-f", "blah"], type: :yo)
You should be able to access the config options within your Thor class like this:
config[:type] # => :yo
Possible to call executable Thor-powered script without calling thor?
Make the shebang line
#!/usr/bin/env ruby
and then at the end of your script add
App.start
Related Topics
Ruby Strftime: Month Without Leading Zero
Ruby Net::Http::Get and JSON Responses
Rails, Ruby, How to Sort an Array
How to Write Specs for Code That Depends on Environment Variables
What's the Best Way to Parse a Tab-Delimited File in Ruby
Ruby on Rails, Including a Module with Arguments
How to Attach a Message to Rspec Check
How to Delete Special Characters
Converting an Empty String into Nil in Ruby
Ruby on Rails, Could Not Find a Valid Gem 'Rails'
Reading the First Line of a File in Ruby
Ruby: How to Count the Number of Times a String Appears in Another String
Collect Values from an Array of Hashes
How to Get Request.Uri in Model in Rails