cli_utils 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cli_utils.rb +8 -12
- metadata +1 -1
data/lib/cli_utils.rb
CHANGED
@@ -16,7 +16,7 @@ end
|
|
16
16
|
|
17
17
|
class CliUtils
|
18
18
|
include Errors
|
19
|
-
attr_accessor :commands, :command, :optional, :required, :config
|
19
|
+
attr_accessor :commands, :command, :optional, :required, :config, :eval
|
20
20
|
|
21
21
|
def initialize(commands_filepath=nil, config_filepath=nil, suggestions_count=nil)
|
22
22
|
@s_count = suggestions_count || 4
|
@@ -27,11 +27,7 @@ class CliUtils
|
|
27
27
|
parse_options
|
28
28
|
|
29
29
|
if @command
|
30
|
-
|
31
|
-
if method
|
32
|
-
eval method
|
33
|
-
exit
|
34
|
-
end
|
30
|
+
@eval = @commands[@command]['eval']
|
35
31
|
end
|
36
32
|
|
37
33
|
rescue ParseError => e
|
@@ -113,7 +109,7 @@ class CliUtils
|
|
113
109
|
begin
|
114
110
|
commands = JSON.parse(File.open(commands_filepath,'r').read)
|
115
111
|
rescue JSON::ParserError => e
|
116
|
-
raise ArgumentError.new("#{commands_filepath} contents is not valid JSON
|
112
|
+
raise ArgumentError.new("#{commands_filepath} contents is not valid JSON")
|
117
113
|
end
|
118
114
|
|
119
115
|
raise ArgumentError.new("#{commands_filepath} is not an array") unless commands.is_a?(Array)
|
@@ -185,13 +181,13 @@ class CliUtils
|
|
185
181
|
|
186
182
|
def usage(command)
|
187
183
|
c = @commands[command]
|
188
|
-
long = c['long']
|
189
|
-
short = c['short']
|
190
|
-
required = c['required'].map{|r| "<#{r}>"}.join(' ')
|
184
|
+
long = c['long'] || ''
|
185
|
+
short = c['short'] ? "(#{c['short']})" : ''
|
186
|
+
required = (c['required'] || []).map{|r| "<#{r}>"}.join(' ')
|
191
187
|
|
192
|
-
optional = c['optional'].map{|o| "[#{o}#{o.start_with?('--') ? ' foo' : ''}]"}.join(' ')
|
188
|
+
optional = (c['optional'] || []).map{|o| "[#{o}#{o.start_with?('--') ? ' foo' : ''}]"}.join(' ')
|
193
189
|
|
194
|
-
"#{long}
|
190
|
+
"#{long} #{short} #{required} #{optional}".gsub(/ +/,' ')
|
195
191
|
end
|
196
192
|
|
197
193
|
def is_command?(str)
|