Class: Thor::Group

### Home Classes Methods

### In Files

- lib/thor/group.rb

### Parent

Object

### Methods

- ::banner
- ::desc
- ::help
- ::invoke
- ::invoke_from_option
- ::printable_commands
- ::printable_tasks
- ::remove_invocation

### Included Modules

- Thor::Base

### Class/Module Index

Quicksearch

- Thor
- Thor::Actions
- Thor::Actions::ClassMethods
- Thor::AmbiguousCommandError
- Thor::Base
- Thor::Base::ClassMethods
- Thor::Command
- Thor::DynamicCommand
- Thor::Error
- Thor::Group
- Thor::HiddenCommand
- Thor::Invocation
- Thor::Invocation::ClassMethods
- Thor::InvocationError
- Thor::LineEditor
- Thor::LineEditor::Basic
- Thor::LineEditor::Readline
- Thor::LineEditor::Readline::PathCompletion
- Thor::MalformattedArgumentError
- Thor::RakeCompat
- Thor::RequiredArgumentMissingError
- Thor::Shell
- Thor::Shell::Basic
- Thor::Shell::Color
- Thor::Shell::HTML
- Thor::UndefinedCommandError
- Thor::UnknownArgumentError
- Thor::Util
- Object

No matching classes.

# Thor::Group

Thor has a special class called Thor::Group. The main difference to Thor class is that it invokes all commands at once. It also include some methods that allows invocations to be done at the class method, which are not available to Thor commands.

### Public Class Methods

desc(description = nil) click to toggle source

The description for this Thor::Group. If none is provided, but a source root exists, tries to find the USAGE one folder above it, otherwise searches in the superclass.

#### Parameters

description<String> | The description for this Thor::Group.

    # File lib/thor/group.rb, line 16
    def desc(description = nil)
      if description
        @desc = description
      else
        @desc ||= from_superclass(:desc, nil)
      end
    end

help(shell) click to toggle source

Prints help information.

#### Options

short | When true, shows only usage.

    # File lib/thor/group.rb, line 29
    def help(shell)
      shell.say "Usage:"
      shell.say "  #{banner}\n"
      shell.say
      class_options_help(shell)
      shell.say desc if desc
    end

invoke(*names, &block) click to toggle source

Invoke the given namespace or class given. It adds an instance method that will invoke the klass and command. You can give a block to configure how it will be invoked.

The namespace/class given will have its options showed on the help usage. Check invoke_from_option for more information.

    # File lib/thor/group.rb, line 56
    def invoke(*names, &block) # rubocop:disable MethodLength
      options = names.last.is_a?(Hash) ? names.pop : {}
      verbose = options.fetch(:verbose, true)

      names.each do |name|
        invocations[name] = false
        invocation_blocks[name] = block if block_given?

        class_eval           def _invoke_#{name.to_s.gsub(/\W/, "_")}            klass, command = self.class.prepare_for_invocation(nil, #{name.inspect})            if klass              say_status :invoke, #{name.inspect}, #{verbose.inspect}              block = self.class.invocation_blocks[#{name.inspect}]              _invoke_for_class_method klass, command, &block            else              say_status :error, %(#{name.inspect} [not found]), :red            end          end, __FILE__, __LINE__
      end
    end

invoke_from_option(*names, &block) click to toggle source

Invoke a thor class based on the value supplied by the user to the given option named "name". A class option must be created before this method is invoked for each name given.

#### Examples

    class GemGenerator < Thor::Group
      class_option :test_framework, :type => :string
      invoke_from_option :test_framework
    end

#### Boolean options

In some cases, you want to invoke a thor class if some option is true or false. This is automatically handled by invoke_from_option. Then the option name is used to invoke the generator.

#### Preparing for invocation

In some cases you want to customize how a specified hook is going to be invoked. You can do that by overwriting the class method prepare_for_invocation. The class method must necessarily return a klass and an optional command.

#### Custom invocations

You can also supply a block to customize how the option is going to be invoked. The block receives two parameters, an instance of the current class and the klass to be invoked.

    # File lib/thor/group.rb, line 110
    def invoke_from_option(*names, &block) # rubocop:disable MethodLength
      options = names.last.is_a?(Hash) ? names.pop : {}
      verbose = options.fetch(:verbose, :white)

      names.each do |name|
        unless class_options.key?(name)
          fail ArgumentError, "You have to define the option #{name.inspect} " <<
                               "before setting invoke_from_option."
        end

        invocations[name] = true
        invocation_blocks[name] = block if block_given?

        class_eval           def _invoke_from_option_#{name.to_s.gsub(/\W/, "_")}            return unless options[#{name.inspect}]            value = options[#{name.inspect}]            value = #{name.inspect} if TrueClass === value            klass, command = self.class.prepare_for_invocation(#{name.inspect}, value)            if klass              say_status :invoke, value, #{verbose.inspect}              block = self.class.invocation_blocks[#{name.inspect}]              _invoke_for_class_method klass, command, &block            else              say_status :error, %(\#{value} [not found]), :red            end          end, __FILE__, __LINE__
      end
    end

printable_commands(*) click to toggle source

Returns commands ready to be printed.

    # File lib/thor/group.rb, line 199
    def printable_commands(*)
      item = []
      item << banner
      item << (desc ? "# #{desc.gsub(/\s+/m, ' ')}" : "")
      [item]
    end

Also aliased as: printable_tasks
printable_tasks(*) click to toggle source
Alias for: printable_commands
remove_invocation(*names) click to toggle source

Remove a previously added invocation.

#### Examples

    remove_invocation :test_framework

    # File lib/thor/group.rb, line 149
    def remove_invocation(*names)
      names.each do |name|
        remove_command(name)
        remove_class_option(name)
        invocations.delete(name)
        invocation_blocks.delete(name)
      end
    end

### Protected Class Methods

banner() click to toggle source

The banner for this class. You can customize it if you are invoking the thor class by another ways which is not the Thor::Runner.

    # File lib/thor/group.rb, line 238
    def banner
      "#{basename} #{self_command.formatted_usage(self, false)}"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.