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

Returns a new instance of 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 = "Usage:\n\n  $ chid [COMMAND]\n\n   To see what the Command do:\n\n    $ chid [COMMAND] -h\n\nCommands:\n\n\#{commands}\n    DESC\n  end\n\n  puts summary\n  print description\nend\n"

.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']}

Parameters:

  • argv (Array<String>)

    The arguments passed from input.

Returns:

  • (Hash<String, Array>)

    Mapped options with your values The keys of hash are the options and the values of hash are all values for the option.



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