Module: Thunder::ClassMethods

Defined in:
lib/thunder.rb

Overview

This module provides methods for any class that includes Thunder

Instance Method Summary collapse

Instance Method Details

#default_command(command) ⇒ Object

Set the default command to be executed when no suitable command is found.

Parameters:

  • command (Symbol)

    the default command



175
176
177
# File 'lib/thunder.rb', line 175

def default_command(command)
  thunder[:default_command] = command
end

#desc(usage, description = "") ⇒ Object

Describe the next method (or subcommand). A longer description can be given using the #longdesc command

Parameters:

  • usage (String)

    the perscribed usage of the command

  • description (String) (defaults to: "")

    a short description of what the command does



184
185
186
# File 'lib/thunder.rb', line 184

def desc(usage, description="")
  thunder[:usage], thunder[:description] = usage, description
end

#get_help_formatterObject



142
143
144
145
146
147
148
# File 'lib/thunder.rb', line 142

def get_help_formatter
  unless thunder[:help_formatter]
    require File.expand_path("../thunder/help/default", __FILE__)
    thunder[:help_formatter] = Thunder::DefaultHelp
  end
  thunder[:help_formatter]
end

#help_formatter(formatter) ⇒ Object

Set the help formatter.

Parameters:

  • formatter (#help_list, #help_command)


168
169
170
# File 'lib/thunder.rb', line 168

def help_formatter(formatter)
  thunder[:help_formatter] = formatter
end

#longdesc(description) ⇒ Object

Provide a long description for the next method (or subcommand).

Parameters:

  • description (String)

    a long description of what the command does



191
192
193
# File 'lib/thunder.rb', line 191

def longdesc(description)
  thunder[:long_description] = description
end

#method_added(method) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers a method as a thunder task



152
153
154
155
156
# File 'lib/thunder.rb', line 152

def method_added(method)
  add_command(method.to_sym) do |command|
    command[:params] = instance_method(method).parameters
  end
end

#option(name, options = {}) ⇒ Object

Define an option for the next method (or subcommand)

Examples:

option :output_file, type: String
option "verbose", desc: "print extra information"

Parameters:

  • name (Symbol, String)

    the long name of this option

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :short (String)

    the short version of the option [the first letter of the option name]

  • :type (Class)

    the datatype of this option [Boolean]

  • :desc (String)

    the long description of this option [“”]

  • :default (*)

    the default value



208
209
210
211
212
213
214
215
216
# File 'lib/thunder.rb', line 208

def option(name, options={})
  name = name.to_sym
  options[:name] = name
  options[:short] ||= name[0]
  options[:type] ||= Boolean
  options[:desc] ||= ""
  thunder[:options] ||= {}
  thunder[:options][name] = options
end

#options_processor(processor) ⇒ Object

Set the options processor.

Parameters:

  • processor (#process_options)


161
162
163
# File 'lib/thunder.rb', line 161

def options_processor(processor)
  thunder[:options_processor] = processor
end

#subcommand(command, handler) ⇒ Object

Define a subcommand

Parameters:

  • command (Symbol, String)

    the command that transfers processing to the provided handler

  • handler (Thunder)

    the handler that processes the request



222
223
224
225
226
# File 'lib/thunder.rb', line 222

def subcommand(command, handler)
  add_command(command.to_sym) do |subcommand|
    subcommand[:subcommand] = handler
  end
end

#thunderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the thunder configuration



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/thunder.rb', line 126

def thunder
  @thunder ||= {
    default_command: :help,
    commands: {
      help: {
        name: :help,
        usage: "help [COMMAND]",
        description: "list available commands or describe a specific command",
        long_description: nil,
        options: nil,
        default_help: true
      },
    }
  }
end