module Thunder::ClassMethods

This module provides methods for any class that includes Thunder

Public Instance Methods

default_command(command) click to toggle source

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

@param command [Symbol] the default command

# File lib/thunder.rb, line 180
def default_command(command)
  thunder[:default_command] = command
end
desc(usage, description="") click to toggle source

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

@param usage [String] the perscribed usage of the command @param description [String] a short description of what the command does

# File lib/thunder.rb, line 189
def desc(usage, description="")
  thunder[:usage], thunder[:description] = usage, description
end
help_formatter(formatter) click to toggle source

Set the help formatter.

@param formatter [#help_list,#help_command]

# File lib/thunder.rb, line 173
def help_formatter(formatter)
  thunder[:help_formatter] = formatter
end
longdesc(description) click to toggle source

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

@param description [String] a long description of what the command does

# File lib/thunder.rb, line 196
def longdesc(description)
  thunder[:long_description] = description
end
method_added(method) click to toggle source

@api private Registers a method as a thunder task

# File lib/thunder.rb, line 157
def method_added(method)
  add_command(method.to_sym) do |command|
    command[:params] = instance_method(method).parameters
  end
end
option(name, options={}) click to toggle source

Define an option for the next method (or subcommand)

@param name [Symbol,String] the long name of this option @option options :short [String] the short version of the option [the first letter of the option name] @option options :type [Class] the datatype of this option [Boolean] @option options :desc [String] the long description of this option [“”] @option options :default [*] the default value

@example

option :output_file, type: String

@example

option "verbose", desc: "print extra information"
# File lib/thunder.rb, line 213
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) click to toggle source

Set the options processor.

@param processor [#process_options]

# File lib/thunder.rb, line 166
def options_processor(processor)
  thunder[:options_processor] = processor
end
subcommand(command, handler) click to toggle source

Define a subcommand

@param command [Symbol,String] the command that transfers processing to the provided handler @param handler [Thunder] the handler that processes the request

# File lib/thunder.rb, line 227
def subcommand(command, handler)
  add_command(command.to_sym) do |subcommand|
    subcommand[:subcommand] = handler
  end
end
thunder() click to toggle source

@api private Get the thunder configuration

# File lib/thunder.rb, line 139
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

Private Instance Methods

add_command(command, &block) click to toggle source
# File lib/thunder.rb, line 234
def add_command(command, &block)
  attributes = [:usage, :description, :options, :long_description]
  return unless attributes.reduce(nil) { |a, key| a || thunder[key] }
  thunder[:commands][command] = {
    name: command,
  }
  attributes.each do |key|
    thunder[:commands][command][key] = thunder.delete(key)
  end
  if block
    if block.arity == 0
      block.call
    else
      block.call thunder[:commands][command]
    end
  end
end