public
Rubygem
Description: A scripting framework that replaces rake and sake
Homepage: http://www.yehudakatz.com
Clone URL: git://github.com/wycats/thor.git
thor /
name age message
file .autotest Wed May 07 19:28:50 -0700 2008 First pass of Thor::Runner [wycats]
file .gitignore Thu May 15 18:22:28 -0700 2008 Add coverage to gitignore. [nex3]
file CHANGELOG.rdoc Wed Aug 27 13:13:13 -0700 2008 fixed a typo in runner.rb; added TODO; release ... [mislav]
file LICENSE Tue May 06 19:21:22 -0700 2008 Initial checkin of Hermes [wycats]
file README.markdown Wed Aug 27 07:28:58 -0700 2008 update (incorrect) README and task.thor sample ... [mislav]
file Rakefile Fri Aug 15 06:56:57 -0700 2008 Quote path so it works on Windows [jherdman]
file Thorfile Fri Sep 12 15:47:09 -0700 2008 0.9.6 [wycats]
directory bin/ Sun May 18 02:46:46 -0700 2008 Get rid of the ObjectSpace monkeypatch in the t... [nex3]
directory lib/ Sun Oct 12 04:36:35 -0700 2008 Fixed issue where default method options weren'... [fabien]
directory script/ Tue May 06 19:21:22 -0700 2008 Initial checkin of Hermes [wycats]
directory spec/ Tue Oct 07 05:11:26 -0700 2008 Tiny fixup to initialize method signature [fabien]
file task.thor Wed Aug 27 07:28:58 -0700 2008 update (incorrect) README and task.thor sample ... [mislav]
file thor.gemspec Thu Oct 09 10:22:24 -0700 2008 Bumped up version to 0.9.6 in preparation of up... [fabien]
README.markdown

thor

Map options to a class. Simply create a class with the appropriate annotations, and have options automatically map to functions and parameters.

Example:

class MyApp < Thor                                                # [1]
  map "-L" => :list                                               # [2]

  desc "install APP_NAME", "install one of the available apps"    # [3]
  method_options :force => :boolean, :alias => :optional          # [4]
  def install(name)
    user_alias = options[:alias]
    if options.force?
      # do something
    end
    # ... other code ...
  end

  desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
  def list(search = "")
    # list everything
  end
end

Thor automatically maps commands as such:

app install myname --force

That gets converted to:

MyApp.new.install("myname")
# with {'force' => true} as options hash
  1. Inherit from Thor to turn a class into an option mapper
  2. Map additional non-valid identifiers to specific methods. In this case, convert -L to :list
  3. Describe the method immediately below. The first parameter is the usage information, and the second parameter is the description.
  4. Provide any additional options. These will be marshaled from -- and - params. In this case, a --force and a -f option is added.

Types for method_options

:boolean
true if the option is passed
true
same as :boolean
:required
the value for this option MUST be provided
:optional
the value for this option MAY be provided
:numeric
the value MAY be provided, but MUST be in numeric form
a String or Numeric
same as :optional, but fall back to the given object as default value

In case of unsatisfied requirements, Thor::Options::Error is raised.

Examples of option parsing:

# let's say this is how we defined options for a method:
method_options(:force => :boolean, :retries => :numeric)

# here is how the following command-line invocations would be parsed:

command -f --retries 5    # => {'force' => true, 'retries' => 5}
command --force -r=5      # => {'force' => true, 'retries' => 5}
command -fr 5             # => {'force' => true, 'retries' => 5}
command --retries=5       # => {'retries' => 5}
command -r5               # => {'retries' => 5}