Class: Chid::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/chid/command.rb

Constant Summary collapse

COMMANDS =
{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Command



145
146
147
# File 'lib/chid/command.rb', line 145

def initialize(options)
  @options = options
end

Class Attribute Details

.argumentsObject

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

.descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

.summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



143
144
145
# File 'lib/chid/command.rb', line 143

def options
  @options
end

Class Method Details

.command(cmd) ⇒ Object



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

def command(cmd)
  COMMANDS[cmd] = self.to_s
end

.helpObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chid/command.rb', line 14

def help
  if self.description.nil?
    commands = String.new
    COMMANDS.keys.each {|k| commands <<  "  #{k.to_s}\n" }
    self.description = <<-DESC
Usage:

  $ chid [COMMAND]

   To see what the Command do:

    $ chid [COMMAND] -h

Commands:

#{commands}
    DESC
  end

  puts summary
  print description
end

.map_options_with_values(argv) ⇒ Hash<String, Array>

Returns a mapped options with your values from @argv

Examples:

Map an argv

argv = ['init', '-option_1', 'value_for_option_1']

map_options_with_values(argv) #=> {'-option1' => ['value_for_option_1']}


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chid/command.rb', line 57

def map_options_with_values(argv)
  return argv.reduce({}) do |options, arg|
    new_options = options

    if arg_is_an_option?(arg)
      new_options[arg] = []
      next(new_options)
    end

   options_with_values(new_options, arg)
  end
end

.run(argv) ⇒ Object



37
38
39
40
41
# File 'lib/chid/command.rb', line 37

def run(argv)
  command_key = command_key(argv)
  return self.help unless command_key_is_included?(command_key)
  invoke(argv)
end