module Thunder

Provides a simple, yet powerful ability to quickly and easily tie Ruby methods with command line actions.

The syntax is very similar to Thor, so switching over should be extremely easy

Constants

VERSION

Version string for gemspec

Public Class Methods

included(base) click to toggle source

@api private Automatically extends the singleton with {ClassMethods}

# File lib/thunder.rb, line 130
def self.included(base)
  base.send :extend, ClassMethods
end

Public Instance Methods

start(args=ARGV.dup, options={}) click to toggle source

Start the object as a command line program, processing the given arguments and using the provided options.

@param args [<String>] the command line arguments [ARGV] @param options [{Symbol => *}] the default options to use [{}]

# File lib/thunder.rb, line 15
def start(args=ARGV.dup, options={})
  command_spec = determine_command(args)

  unless command_spec
    return
  end

  if command_spec[:name] == :help && command_spec[:default_help]
    return get_help(args, options)
  end

  parsed_options = process_options(args, command_spec)
  options.merge!(parsed_options) if parsed_options
  if command_spec[:subcommand]
    return command_spec[:subcommand].start(args, options)
  end
  if parsed_options
    args << options
  end

  if command_spec[:params]
    min = command_spec[:params].count { |param| param.first == :req}
    if args.size < min
      ARGV.insert((ARGV.size - args.size) - 1, "help")
      puts help_command(command_spec)
      return
    end
    max = if command_spec[:params].map(&:first).include?(:rest)
      nil
    else
      command_spec[:params].size
    end
    if !max.nil? && args.size > max
      ARGV.insert((ARGV.size - args.size) - 1, "help")
      puts help_command(command_spec)
      return
    end
  end
  return send command_spec[:name], *args
end

Protected Instance Methods

determine_command(args) click to toggle source

Determine the command to use from the given arguments

@param args [<String>] the arguments to process @return [Hash,nil] the command specification for the given arguments,

or nil if there is no appropriate command
# File lib/thunder.rb, line 62
def determine_command(args)
  if args.empty?
    return self.class.thunder[:commands][self.class.thunder[:default_command]]
  end
  command_name = args.first.to_sym
  command_spec = self.class.thunder[:commands][command_name]
  if command_spec
    args.shift
  else
    command_spec = self.class.thunder[:commands][self.class.thunder[:default_command]]
  end
  return command_spec
end
get_help(args, options) click to toggle source

get help on the provided subjects

@param args [<String>] the arguments list @param options [Hash] any included options

# File lib/thunder.rb, line 95
def get_help(args, options)
  if args.size == 0
    puts help_list(self.class.thunder[:commands])
  else
    puts help_command(determine_command(args))
  end
end
help_command(command_spec) click to toggle source

Render detailed help on a specific command

@param command_spec [Hash] the command to render detailed help for @return [String] the rendered help

# File lib/thunder.rb, line 119
def help_command(command_spec)
  unless self.class.thunder[:help_formatter]
    require 'thunder/help/default'
    self.class.thunder[:help_formatter] = Thunder::DefaultHelp
  end
  self.class.thunder[:help_formatter].help_command(command_spec)
end
help_list(commands) click to toggle source

Render a usage list of the given commands

@param commands [<Hash>] the commands to list @return [String] the rendered help

# File lib/thunder.rb, line 107
def help_list(commands)
  unless self.class.thunder[:help_formatter]
    require 'thunder/help/default'
    self.class.thunder[:help_formatter] = Thunder::DefaultHelp
  end
  self.class.thunder[:help_formatter].help_list(commands)
end
process_options(args, command_spec) click to toggle source

Process command line options from the given argument list

@param args [<String>] the argument list to process @param command_spec [Hash] the command specification to use @return [{Symbol => *},nil] the options

# File lib/thunder.rb, line 81
def process_options(args, command_spec)
  return nil unless command_spec[:options]

  unless self.class.thunder[:options_processor]
    require 'thunder/options/optparse'
    self.class.thunder[:options_processor] = Thunder::OptParseAdapter
  end
  self.class.thunder[:options_processor].process_options(args, command_spec)
end