In Files

Thor::Base::ClassMethods

Public Instance Methods

all_tasks() click to toggle source

Returns the tasks for this Thor class and all subclasses.

Returns

OrderedHash:An ordered hash with tasks names as keys and Thor::Task objects as values.
     # File lib/thor/base.rb, line 264
264:       def all_tasks
265:         @all_tasks ||= from_superclass(:all_tasks, Thor::CoreExt::OrderedHash.new)
266:         @all_tasks.merge(tasks)
267:       end
argument(name, options={}) click to toggle source

Adds an argument to the class and creates an attr_accessor for it.

Arguments are different from options in several aspects. The first one is how they are parsed from the command line, arguments are retrieved from position:

  thor task NAME

Instead of:

  thor task --name=NAME

Besides, arguments are used inside your code as an accessor (self.argument), while options are all kept in a hash (self.options).

Finally, arguments cannot have type :default or :boolean but can be optional (supplying :optional => :true or :required => false), although you cannot have a required argument after a non-required argument. If you try it, an error is raised.

Parameters

name:The name of the argument.
options:Described below.

Options

:desc - Description for the argument. :required - If the argument is required or not. :optional - If the argument is optional or not. :type - The type of the argument, can be :string, :hash, :array, :numeric. :default - Default value for this argument. It cannot be required and have default values. :banner - String to show on usage notes.

Errors

ArgumentError:Raised if you supply a required argument after a non required one.
     # File lib/thor/base.rb, line 130
130:       def argument(name, options={})
131:         is_thor_reserved_word?(name, :argument)
132:         no_tasks { attr_accessor name }
133: 
134:         required = if options.key?(:optional)
135:           !options[:optional]
136:         elsif options.key?(:required)
137:           options[:required]
138:         else
139:           options[:default].nil?
140:         end
141: 
142:         remove_argument name
143: 
144:         arguments.each do |argument|
145:           next if argument.required?
146:           raise ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " <<
147:                                "the non-required argument #{argument.human_name.inspect}."
148:         end if required
149: 
150:         arguments << Thor::Argument.new(name, options[:desc], required, options[:type],
151:                                               options[:default], options[:banner])
152:       end
arguments() click to toggle source

Returns this class arguments, looking up in the ancestors chain.

Returns

Array[Thor::Argument]

     # File lib/thor/base.rb, line 159
159:       def arguments
160:         @arguments ||= from_superclass(:arguments, [])
161:       end
class_option(name, options={}) click to toggle source

Adds an option to the set of class options

Parameters

name:The name of the argument.
options:Described below.

Options

:desc - Description for the argument. :required - If the argument is required or not. :default - Default value for this argument. :group - The group for this options. Use by class options to output options in different levels. :aliases - Aliases for this option. :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean. :banner - String to show on usage notes.

     # File lib/thor/base.rb, line 193
193:       def class_option(name, options={})
194:         build_option(name, options, class_options)
195:       end
class_options(options=nil) click to toggle source

Adds a bunch of options to the set of class options.

  class_options :foo => false, :bar => :required, :baz => :string

If you prefer more detailed declaration, check class_option.

Parameters

Hash[Symbol => Object]

     # File lib/thor/base.rb, line 172
172:       def class_options(options=nil)
173:         @class_options ||= from_superclass(:class_options, {})
174:         build_options(options, @class_options) if options
175:         @class_options
176:       end
group(name=nil) click to toggle source

Defines the group. This is used when thor list is invoked so you can specify that only tasks from a pre-defined group will be shown. Defaults to standard.

Parameters

name

     # File lib/thor/base.rb, line 239
239:       def group(name=nil)
240:         case name
241:           when nil
242:             @group ||= from_superclass(:group, 'standard')
243:           else
244:             @group = name.to_s
245:         end
246:       end
namespace(name=nil) click to toggle source

Sets the namespace for the Thor or Thor::Group class. By default the namespace is retrieved from the class name. If your Thor class is named Scripts::MyScript, the help method, for example, will be called as:

  thor scripts:my_script -h

If you change the namespace:

  namespace :my_scripts

You change how your tasks are invoked:

  thor my_scripts -h

Finally, if you change your namespace to default:

  namespace :default

Your tasks can be invoked with a shortcut. Instead of:

  thor :my_task
     # File lib/thor/base.rb, line 338
338:       def namespace(name=nil)
339:         case name
340:           when nil
341:             @namespace ||= Thor::Util.namespace_from_thor_class(self, false)
342:           else
343:             @namespace = name.to_s
344:         end
345:       end
no_tasks() click to toggle source

All methods defined inside the given block are not added as tasks.

So you can do:

  class MyScript < Thor
    no_tasks do
      def this_is_not_a_task
      end
    end
  end

You can also add the method and remove it from the task list:

  class MyScript < Thor
    def this_is_not_a_task
    end
    remove_task :this_is_not_a_task
  end
     # File lib/thor/base.rb, line 310
310:       def no_tasks
311:         @no_tasks = true
312:         yield
313:         @no_tasks = false
314:       end
remove_argument(*names) click to toggle source

Removes a previous defined argument. If :undefine is given, undefine accessors as well.

Paremeters

names:Arguments to be removed

Examples

  remove_argument :foo
  remove_argument :foo, :bar, :baz, :undefine => true
     # File lib/thor/base.rb, line 208
208:       def remove_argument(*names)
209:         options = names.last.is_a?(Hash) ? names.pop : {}
210: 
211:         names.each do |name|
212:           arguments.delete_if { |a| a.name == name.to_s }
213:           undef_method name, "#{name}=" if options[:undefine]
214:         end
215:       end
remove_class_option(*names) click to toggle source

Removes a previous defined class option.

Paremeters

names:Class options to be removed

Examples

  remove_class_option :foo
  remove_class_option :foo, :bar, :baz
     # File lib/thor/base.rb, line 227
227:       def remove_class_option(*names)
228:         names.each do |name|
229:           class_options.delete(name)
230:         end
231:       end
remove_task(*names) click to toggle source

Removes a given task from this Thor class. This is usually done if you are inheriting from another class and don’t want it to be available anymore.

By default it only remove the mapping to the task. But you can supply :undefine => true to undefine the method from the class as well.

Parameters

name:The name of the task to be removed
options:You can give :undefine => true if you want tasks the method to be undefined from the class as well.
     # File lib/thor/base.rb, line 281
281:       def remove_task(*names)
282:         options = names.last.is_a?(Hash) ? names.pop : {}
283: 
284:         names.each do |name|
285:           tasks.delete(name.to_s)
286:           all_tasks.delete(name.to_s)
287:           undef_method name if options[:undefine]
288:         end
289:       end
start(given_args=ARGV, config={}) click to toggle source

Default way to start generators from the command line.

     # File lib/thor/base.rb, line 349
349:       def start(given_args=ARGV, config={})
350:         config[:shell] ||= Thor::Base.shell.new
351:         yield
352:       rescue Thor::Error => e
353:         if given_args.include?("--debug")
354:           raise e
355:         else
356:           config[:shell].error e.message
357:         end
358:         exit(1) if exit_on_failure?
359:       end
tasks() click to toggle source

Returns the tasks for this Thor class.

Returns

OrderedHash:An ordered hash with tasks names as keys and Thor::Task objects as values.
     # File lib/thor/base.rb, line 254
254:       def tasks
255:         @tasks ||= Thor::CoreExt::OrderedHash.new
256:       end

Protected Instance Methods

exit_on_failure?() click to toggle source

A flag that makes the process exit with status 1 if any error happens.

     # File lib/thor/base.rb, line 497
497:         def exit_on_failure?
498:           false
499:         end
from_superclass(method, default=nil) click to toggle source

Retrieves a value from superclass. If it reaches the baseclass, returns default.

     # File lib/thor/base.rb, line 486
486:         def from_superclass(method, default=nil)
487:           if self == baseclass || !superclass.respond_to?(method, true)
488:             default
489:           else
490:             value = superclass.send(method)
491:             value.dup if value
492:           end
493:         end
inherited(klass) click to toggle source

Everytime someone inherits from a Thor class, register the klass and file into baseclass.

     # File lib/thor/base.rb, line 458
458:         def inherited(klass)
459:           Thor::Base.register_klass_file(klass)
460:         end
method_added(meth) click to toggle source

Fire this callback whenever a method is added. Added methods are tracked as tasks by invoking the create_task method.

     # File lib/thor/base.rb, line 465
465:         def method_added(meth)
466:           meth = meth.to_s
467: 
468:           if meth == "initialize"
469:             initialize_added
470:             return
471:           end
472: 
473:           # Return if it's not a public instance method
474:           return unless public_instance_methods.include?(meth) ||
475:                         public_instance_methods.include?(meth.to_sym)
476: 
477:           return if @no_tasks || !create_task(meth)
478: 
479:           is_thor_reserved_word?(meth, :task)
480:           Thor::Base.register_klass_file(self)
481:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.