Skip to content

Making An Executable

chikamichi edited this page Aug 2, 2011 · 4 revisions

You may want to make a script as an executable command. To do this with Thor:

  • include the ruby shebang line.
  • require "thor" in your script.
  • define your Thor class.
  • add #{YourThorClassname}.start to the bottom of your script.

Example: mythorcommand.rb

#!/usr/bin/env ruby
require "rubygems" # ruby1.9 doesn't "require" it though
require "thor"

class MyThorCommand < Thor
  desc "foo", "Prints foo"
  def foo
    puts "foo"
  end
end

MyThorCommand.start

Make the script executable:

chmod a+x mythorcommand.rb

Now you can type:

./mythorcommand.rb foo
Clone this wiki locally