I'm new to Thor myself, but I don't think it's set up to work that independently.
Try creating a Thor task internally, and then starting it.
Here's an example I've tried out, and placed in a file called thor_createfile.rb
(I've put in some additional things I'll explain after the code that might be enlightening for you):
#!/usr/bin/env ruby
require 'rubygems'
require 'thor'
class MyThorTasks < Thor
include Thor::Actions
default_task :createInflexibleFile
desc "createFile <fname> <content>", "Creates a file with some content"
def createFile(fname, content)
create_file fname, content
end
INFLEXIBLE_FILENAME = "the_filename.txt"
INFLEXIBLE_CONTENT = "Greetings, Earthlings!"
desc "createInflexibleFile", "Creates a file called '#{INFLEXIBLE_FILENAME}' containing '#{INFLEXIBLE_CONTENT}'"
def createInflexibleFile
puts "Creating a file called '#{INFLEXIBLE_FILENAME}' containing '#{INFLEXIBLE_CONTENT}'"
create_file INFLEXIBLE_FILENAME, INFLEXIBLE_CONTENT
end
end
MyThorTasks.start
You can see it defines a class that extends Thor
, and then calls the start
method on it.
Now you should be able to call it simply like this:
./thor_createfile.rb
and it will use the task designated as default_task
.
But if you need to take some command-line parameters, you can explicitly call tasks by name. So to call the other task, for example:
./thor_createfile.rb createFile fancy_file_name.txt "Text to go inside the file"
Note I've told it to include Thor::Actions
so all the items you were interested in (like create_file
) are available.
Now you can add other tasks inside it (make sure to add the desc
for each one or it will probably complain) and use those too, as needed.
To have it tell you about all of the tasks defined inside it, you can call it this way:
./thor_createfile.rb -?