Class: Command

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

Overview

Abstract class with definitions of commands

Direct Known Subclasses

Add, Init, Look, Push, SetRepository, Show, Status

Class Method Summary collapse

Class Method Details

.childrensObject



19
20
21
# File 'lib/command.rb', line 19

def self.childrens
  raise 'return the childrens commands'
end

.childrens?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/command.rb', line 23

def self.childrens?
  !childrens.empty?
end

.command_nameObject



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

def self.command_name
  raise 'return the command name'
end

.options_messagesObject



3
4
5
# File 'lib/command.rb', line 3

def self.options_messages
  raise 'return a list with how to use this command'
end

.parentObject



15
16
17
# File 'lib/command.rb', line 15

def self.parent
  raise 'return the parent command'
end

.run(argv) ⇒ Object



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

def self.run(argv)
  run_childrens(argv)
end

.run_childrens(argv) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/command.rb', line 27

def self.run_childrens(argv)
  children_command = argv.first
  children = childrens.detect { |c| c.command_name == children_command }
  return usage unless children

  argv.delete(children.command_name)
  children.run(argv)
end

.usageObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/command.rb', line 36

def self.usage
  usage_message = usage_header
  usage_message += "\n\n[options]\n"

  childrens.each do |command|
    messages = command.options_messages
    messages = [messages] unless messages.is_a?(Array)

    messages.each do |message|
      usage_message += "\n#{message}" if message
    end
  end

  puts usage_message
end

.usage_headerObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/command.rb', line 52

def self.usage_header
  header = ''
  header += '[option]' if childrens?
  header = "#{command_name} " + header

  parent = self.parent
  until parent.nil?
    header = "#{parent.command_name} " + header
    parent = parent.parent
  end

  header = '$ ' + header
  header
end