Module: Commandeer

Defined in:
lib/commandeer.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.add_command(opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/commandeer.rb', line 14

def self.add_command(opts)
  command = opts[:command].to_s
  parent = opts[:parent].to_s
  klass = opts[:klass].to_s
  parser = opts[:parser].to_s
  h = {}
  case parent
  when "top"
    h = {:parser => parser, :klass => klass}
    @commands[command] ||= {}
    @commands[command].merge!(h)
  else
    h[command] = {:parser => parser, :klass => klass}
    @commands[parent] ||= {}
    @commands[parent]["subcommands"] ||= {}
    @commands[parent]["subcommands"].merge!(h)
  end
end

.commandsObject



6
7
8
# File 'lib/commandeer.rb', line 6

def self.commands
  @commands
end

.constantize(camel_cased_word) ⇒ Object

Ripped from the ActiveSupport headlines!



114
115
116
117
118
119
120
121
122
123
# File 'lib/commandeer.rb', line 114

def self.constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

.included(base) ⇒ Object



125
126
127
# File 'lib/commandeer.rb', line 125

def self.included(base)
  base.extend ClassMethods
end

.parse!(args, script_name = __FILE__) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/commandeer.rb', line 33

def self.parse!(args, script_name=__FILE__)
  @script_name = script_name
  # We haz no commands registered
  if @commands.size == 0
    puts "No known commands!"
    exit(1)
  end

  # No args. Let's show what we have registered
  if (args.size == 0) || (args[0] =~ /^-/)
    puts "Usage: #{@script_name} [command options] or [command subcommand options]\n\n"
    puts "Registered commands\n"
    @commands.each do |command, options|
      next if (command==:klass || command==:parser)
      puts " #{command}"
      unless options["subcommands"].nil?
        puts "  subcommands:"
        options["subcommands"].each do |sub, opts|
          puts "   #{sub}"
        end
      end
    end
    exit
  end

  # Workflow
  # Check if current scope is a valid command
  scope = args.shift

  # set the current command or return help
  @commands.has_key?(scope) ? command=@commands[scope] : (puts "Unknown command: #{scope}\n"; self.parse!("-h"))

  subcommands = command["subcommands"]

  if subcommands
    output = ''
    output << "`#{scope}` has the following registered subcommands:\n"
    subcommands.keys.each {|x| output << "\t#{x}" }
    puts output
  end if args.size == 0

  if command.has_key?(:parser)
    output = ''
    output << "\n`#{scope}` also takes options"
    output << "\ntry running '#{@script_name} #{scope} --help'"
    puts output
  end if args.size == 0

  if args.size > 0 
    if subcommands.has_key?(args[0])
      # Okay so the next arg is a registered subcommand. Let's shift args
      new_scope = args.shift
      warning =<<-EOF
      Warning! `#{new_scope}` is a registered subcommand for `#{scope}` but `#{scope}` also takes options.
      This can cause unexpected results if `#{scope}` has an option named `#{new_scope}`
      EOF
      puts warning

      parser = subcommands[new_scope][:parser]
      klass = subcommands[new_scope][:klass]
    else
      parser = command[:parser]
      klass = command[:klass]
    end
  else
    parser = command[:parser]
    klass = command[:klass]
  end
  begin
    handler = constantize(klass)
    handler.send parser.to_sym, args
  rescue NoMethodError
    if command[:parser]
      puts "#{handler}##{parser} method does not exist"
    end
  rescue Exception => e
    puts e.message
  end
end

.reset!Object



10
11
12
# File 'lib/commandeer.rb', line 10

def self.reset!
  @commands = {}
end