Module: Thunder

Defined in:
lib/thunder.rb,
lib/thunder/version.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods, OptParseAdapter Classes: Boolean, DefaultHelp, TrollopAdapter

Constant Summary collapse

VERSION =

Version string for gemspec

"0.6.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ 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.

Automatically extends the singleton with ClassMethods



117
118
119
# File 'lib/thunder.rb', line 117

def self.included(base)
  base.send :extend, ClassMethods
end

Instance Method Details

#start(args = ARGV.dup, options = {}) ⇒ Object

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

Parameters:

  • args (<String>) (defaults to: ARGV.dup)

    (ARGV) the command line arguments

  • options ({Symbol => *}) (defaults to: {})

    ({}) the default options to use



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thunder.rb', line 15

def start(args = ARGV.dup, options = {})
  command_spec = determine_command(args)
  return unless command_spec

  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

  return command_spec[:subcommand].start(args, options) if command_spec[:subcommand]

  args << options if parsed_options

  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