Class: Command
- Inherits:
-
Object
show all
- Defined in:
- lib/command.rb
Overview
Abstract class with definitions of commands
Class Method Summary
collapse
Class Method Details
.childrens ⇒ Object
19
20
21
|
# File 'lib/command.rb', line 19
def self.childrens
raise 'return the childrens commands'
end
|
.childrens? ⇒ Boolean
23
24
25
|
# File 'lib/command.rb', line 23
def self.childrens?
!childrens.empty?
end
|
.command_name ⇒ Object
7
8
9
|
# File 'lib/command.rb', line 7
def self.command_name
raise 'return the command name'
end
|
.options_messages ⇒ Object
3
4
5
|
# File 'lib/command.rb', line 3
def self.options_messages
raise 'return a list with how to use this command'
end
|
.parent ⇒ Object
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
|
.usage ⇒ Object
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_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
|
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/command.rb', line 52
def self.
= ''
+= '[option]' if childrens?
= "#{command_name} " +
parent = self.parent
until parent.nil?
= "#{parent.command_name} " +
parent = parent.parent
end
= '$ ' +
end
|