Run a CLI Thor app without arguments or task name
It seems the proper Thor-way to do this is using default_task
:
class Commands < Thor
desc "whatever", "The default task to run when no command is given"
def whatever
...
end
default_task :whatever
end
Commands.start
If for whatever reason that isn't what you need, you should be able to do something likeclass Commands < Thor
...
end
if ARGV.empty?
# Perform the default, it doesn't have to be a Thor task
Commands.new.whatever
else
# Start Thor as usual
Commands.start
end
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 have a Thor method that is not an explicit task
I was able to use the no_tasks block for this.
class Update < Thor
desc "do_it", "a simple task"
def do_it
puts i_did_it
end
# no desc here !!!
no_tasks do
def i_did_it
"I did it"
end
end
end
How can I retrieve the current command name in a Thor method?
May be this would work:
def fun
puts __method__
end
fun
#=> :fun
Also look in the caller
method. It returns the current execution stack as a string. Thor task list from within the class?
And of course, right after posting this question, I figure it out.
Foo.tasks would return a hash {task_name => [array of task info]}
ruby-thor calling multiple classes in a gem executable
How do you intend both of the different classes to work together? Do you want all the commands from each to be available? If so, can you just use a single class? Thor isn't designed to work this way. The reason you're getting the error is because the first time you call .start, an error will be thrown if the command isn't found.
If you're worried about having a particularly long class definition, you can separate the definition of each command into separate files but using the same class.
Is there a way to add hooks to a Thor class in order to run code before/after all commands?
Looks like there's no built-in way of doing such callbacks in the Thor gem. (I'd think this would be a popular feature, but maybe I'm looking at things the wrong way.)
An above comment points to a third-party solution, thor-hollaback, but I have not tried it.
Related Topics
Why Slicing The Params Hash Poses a Security Issue on Mass-Assignment
Error Could Not Find I18N-0.7.0 in Any of The Sources
Is The Assignment Operator Really "Just" an Operator
Phusion Passenger with Ruby 1.8 and 1.9
How to Unescape C-Style Escape Sequences from Ruby
Watir Changing Mozilla Firefox Preferences
Rvm - Macports Won't Update Through Proxy
How to Handle Omniauth Callbacks in Multiple Environments
Setting Up Configuration Settings When Writing a Gem
What Is - Gets Is a Directory - Error Message
Sorting a Multidimensional Array in Ruby
Encoding Problems in Rails on Ruby 1.9.1
Alias_Method on Activerecord::Base Results in Nameerror
Current Password Can't Be Blank When Updating Devise Account