Git Product home page Git Product logo

thor's Introduction

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 or false
same as :boolean, but fall back to given boolean as default value
: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}

thor's People

Contributors

nex3 avatar wycats avatar jherdman avatar fabien avatar mislav avatar cypher avatar luislavena avatar cldwalker avatar jackdempsey avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.