Class: Podage::Command

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

Constant Summary collapse

@@indent =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, &block) ⇒ Command

Returns a new instance of Command.



39
40
41
42
43
44
45
46
47
48
# File 'lib/podage/cli/command.rb', line 39

def initialize(name, description, &block)

  @name = name
  @description = description
  @block = block
  
  @options = []
  @commands = []

end

Instance Attribute Details

#base_commandObject

Returns the value of attribute base_command.



30
31
32
# File 'lib/podage/cli/command.rb', line 30

def base_command
  @base_command
end

#descriptionObject

Returns the value of attribute description.



29
30
31
# File 'lib/podage/cli/command.rb', line 29

def description
  @description
end

#nameObject

Returns the value of attribute name.



28
29
30
# File 'lib/podage/cli/command.rb', line 28

def name
  @name
end

#usageObject

Returns the value of attribute usage.



31
32
33
# File 'lib/podage/cli/command.rb', line 31

def usage
  @usage
end

Instance Method Details

#add_command(command) ⇒ Object



56
57
58
59
60
# File 'lib/podage/cli/command.rb', line 56

def add_command(command)

  @commands << command

end

#add_option(option) ⇒ Object



50
51
52
53
54
# File 'lib/podage/cli/command.rb', line 50

def add_option(option)

  @options << option

end

#parse(args) ⇒ Object



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
112
113
114
115
116
117
118
119
120
# File 'lib/podage/cli/command.rb', line 62

def parse(args)

  args_tmp = args.dup

  if args_tmp.empty?

    @block.call self
    
  else
    
    args_tmp.each do |arg|
    
      @commands.each do |command|
    
        if arg == command.name

          args_tmp.delete(arg)

          command.parse(args_tmp)
      
        end
    
      end
    
      @options.each do |option|
    
        if arg == option.name || arg == option.short_cut
      
          args_tmp.delete(arg)
          
          option.execute
      
        end
    
      end
    
    end
  
  end
  
  unless args_tmp.empty?
  
    error = "Unknown arguments: "
    
    args_tmp.each do |arg|
    
      error += arg + " "
    
    end
    
    puts error
    
    print
    
    exit
  
  end

end


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/podage/cli/command.rb', line 122

def print

  puts
  puts "Usage".underline
  puts
  cmd = "     $ ".bold + base_command
   
  unless @commands.empty?
  
    cmd += " " + "[command]".green.bold
  
  end
  
  unless @options.empty?
  
    cmd += " [options]".blue.bold
  
  end
  
  puts cmd
  
  unless @usage.nil?
  
    puts 
    puts "     " + @usage
  
  end
  
  
  if !@commands.empty?
  
    puts
    puts "Commands".underline
    puts
  
    @commands.each do |command|
  
      puts "     ".green + command.name.green.bold + whitespace(command.name) + command.description  
  
    end
  
  end
  
  if !@options.empty?
  
    puts
    puts "Options".underline
    puts
  
    @options.each do |option|
  
      output = option.name + ", " + option.short_cut
      puts "     " + output.bold.blue + whitespace(output) + option.description  
  
    end
  end
  
  puts

end