rubycom 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/rubycom.rb +40 -15
- data/lib/rubycom/arguments.rb +51 -20
- data/lib/rubycom/documentation.rb +19 -11
- data/lib/rubycom/version.rb +1 -1
- data/test/rubycom/arguments_test.rb +141 -0
- data/test/rubycom/rubycom_test.rb +41 -20
- data/test/rubycom/util_test_module.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzljMjJkOTA5OGI4NTlmNTNjMjQ0NDdkMzQ1NzY5YjYxMGRkMmQxNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MTY3NjdiMTkyOTBhM2UyNjZiNjc4ODE5YzQ0MDJiNzQzNmNlOGJiOQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NDRmZWFhNTRhMWFmMTBjZjY1ZjE3N2JhODFjNThiMDc3MDk3YjQzMzc3NWM4
|
10
|
+
OWQwNWE3NzA3ZjgzMDdlODAzZjQzZmY3NWFjNDdhYjMxNDU5YzE2ZjkyM2Jl
|
11
|
+
OWU4ODYzMWFlZWY0NzYzOGQ5YzQwYWZlM2VmMDlmMzcxYzdmZTk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Njg1YWIxMzI5ODA1NGViMTA1NGMyMjYxOGViNDI0NTZjOWQyY2U1OTE3Mzgx
|
14
|
+
YmQ0NGNhOGVkNTg0OGE2OWNjMTBkNWI2YWE5ZjJkNjZjYWY0YzlmN2Q2ZWVm
|
15
|
+
ZTRmMTVlMDdhMDUxY2NjYzg4NjA1NjMwZWNkZmQyNzUxNzUxZGE=
|
data/lib/rubycom.rb
CHANGED
@@ -18,15 +18,25 @@ module Rubycom
|
|
18
18
|
#
|
19
19
|
# @param [Module] base the module which invoked 'include Rubycom'
|
20
20
|
def self.included(base)
|
21
|
-
raise CLIError, 'base must be a module' if base.class != Module
|
22
21
|
base_file_path = caller.first.gsub(/:\d+:.+/, '')
|
23
|
-
if base_file_path == $0
|
22
|
+
if base.class == Module && (base_file_path == $0 || self.is_executed_by_gem?(base_file_path))
|
24
23
|
base.module_eval {
|
25
24
|
Rubycom.run(base, ARGV)
|
26
25
|
}
|
27
26
|
end
|
28
27
|
end
|
29
28
|
|
29
|
+
# Determines whether the including module was executed by a gem binary
|
30
|
+
#
|
31
|
+
# @param [String] base_file_path the path to the including module's source file
|
32
|
+
def self.is_executed_by_gem?(base_file_path)
|
33
|
+
Gem.loaded_specs.map{|k,s|
|
34
|
+
{k => {name: "#{s.name}-#{s.version}", executables: s.executables}}
|
35
|
+
}.reduce(&:merge).map{|k,s|
|
36
|
+
base_file_path.include?(s[:name]) && s[:executables].include?(File.basename(base_file_path))
|
37
|
+
}.flatten.reduce(&:|)
|
38
|
+
end
|
39
|
+
|
30
40
|
# Looks up the command specified in the first arg and executes with the rest of the args
|
31
41
|
#
|
32
42
|
# @param [Module] base the module which invoked 'include Rubycom'
|
@@ -112,10 +122,11 @@ module Rubycom
|
|
112
122
|
end
|
113
123
|
end
|
114
124
|
|
115
|
-
#
|
125
|
+
# Handles the method call according to the given arguments. If the specified command is a Module then a recursive search
|
126
|
+
# is performed until a Method is found in the specified arguments.
|
116
127
|
#
|
117
128
|
# @param [Module] base the module which invoked 'include Rubycom'
|
118
|
-
# @param [String] command the name of the
|
129
|
+
# @param [String] command the name of the command to call, may be a Module name or a Method
|
119
130
|
# @param [Array] arguments a String Array representing the arguments for the given command
|
120
131
|
def self.run_command(base, command, arguments=[])
|
121
132
|
arguments = [] if arguments.nil?
|
@@ -125,17 +136,7 @@ module Rubycom
|
|
125
136
|
if base.included_modules.map { |mod| mod.name.to_sym }.include?(command.to_sym)
|
126
137
|
self.run_command(eval(command), arguments[0], arguments[1..-1])
|
127
138
|
else
|
128
|
-
|
129
|
-
raise CLIError, "No public method found for symbol: #{command.to_sym}" if method.nil?
|
130
|
-
param_defs = Arguments.get_param_definitions(method)
|
131
|
-
args = Arguments.parse_arguments(param_defs, arguments)
|
132
|
-
flatten = false
|
133
|
-
params = method.parameters.map { |arr| flatten = true if arr[0]==:rest; args[arr[1]] }
|
134
|
-
if flatten
|
135
|
-
rest_arr = params.delete_at(-1)
|
136
|
-
rest_arr.each { |arg| params << arg }
|
137
|
-
end
|
138
|
-
(arguments.nil? || arguments.empty?) ? method.call : method.call(*params)
|
139
|
+
self.call_method(base, command, arguments)
|
139
140
|
end
|
140
141
|
rescue CLIError => e
|
141
142
|
$stderr.puts e
|
@@ -143,6 +144,30 @@ module Rubycom
|
|
143
144
|
end
|
144
145
|
end
|
145
146
|
|
147
|
+
# Calls the given command on the given Module after parsing the given Array of arguments
|
148
|
+
#
|
149
|
+
# @param [Module] base the module wherein the specified command is defined
|
150
|
+
# @param [String] command the name of the Method to call
|
151
|
+
# @param [Array] arguments a String Array representing the arguments for the given command
|
152
|
+
# @return the result of the specified Method call
|
153
|
+
def self.call_method(base, command, arguments=[])
|
154
|
+
method = base.public_method(command.to_sym)
|
155
|
+
raise CLIError, "No public method found for symbol: #{command.to_sym}" if method.nil?
|
156
|
+
param_defs = Arguments.get_param_definitions(method)
|
157
|
+
args = Arguments.resolve(param_defs, arguments)
|
158
|
+
flatten = false
|
159
|
+
params = method.parameters.map { |arr| flatten = true if arr[0]==:rest; args[arr[1]] }
|
160
|
+
if flatten
|
161
|
+
rest_arr = params.delete_at(-1)
|
162
|
+
if rest_arr.respond_to?(:each)
|
163
|
+
rest_arr.each { |arg| params << arg }
|
164
|
+
else
|
165
|
+
params << rest_arr
|
166
|
+
end
|
167
|
+
end
|
168
|
+
(arguments.nil? || arguments.empty?) ? method.call : method.call(*params)
|
169
|
+
end
|
170
|
+
|
146
171
|
# Inserts a tab completion into the current user's .bash_profile with a command entry to register the function for
|
147
172
|
# the current running ruby file
|
148
173
|
#
|
data/lib/rubycom/arguments.rb
CHANGED
@@ -13,41 +13,61 @@ module Rubycom
|
|
13
13
|
# }
|
14
14
|
# @param [Array] arguments an Array of Strings representing the arguments to be parsed
|
15
15
|
# @return [Hash] a Hash mapping the defined parameters to their matching argument values
|
16
|
-
def self.
|
16
|
+
def self.resolve(parameters={}, arguments=[])
|
17
17
|
raise CLIError, 'parameters may not be nil' if parameters.nil?
|
18
18
|
raise CLIError, 'arguments may not be nil' if arguments.nil?
|
19
|
-
|
20
|
-
self.parse_arg(arg)
|
21
|
-
}.group_by { |hsh|
|
22
|
-
hsh.keys.first
|
23
|
-
}.map { |key, arr|
|
24
|
-
(key == :rubycom_non_opt_arg) ? Hash[key, arr.map { |hsh| hsh.values }.flatten(1)] : Hash[key, arr.map { |hsh| hsh.values.first }.reduce(&:merge)]
|
25
|
-
}.reduce(&:merge) || {}
|
19
|
+
parsed_args = self.parse_args(arguments)
|
26
20
|
|
27
|
-
|
21
|
+
args = parsed_args[:rubycom_non_opt_arg] || []
|
22
|
+
options = parsed_args.select{|key,_| key != :rubycom_non_opt_arg } || {}
|
23
|
+
parsed_arg_count = args.length + options.length
|
28
24
|
types = parameters.values.group_by { |hsh| hsh[:type] }.map { |type, defs_arr| Hash[type, defs_arr.length] }.reduce(&:merge) || {}
|
29
|
-
raise CLIError, "Wrong number of arguments. Expected at least #{types[:req]}, received #{
|
30
|
-
raise CLIError, "Wrong number of arguments. Expected at most #{(types[:req]||0) + (types[:opt]||0)}, received #{
|
25
|
+
raise CLIError, "Wrong number of arguments. Expected at least #{types[:req]}, received #{parsed_arg_count}" if parsed_arg_count < (types[:req]||0)
|
26
|
+
raise CLIError, "Wrong number of arguments. Expected at most #{(types[:req]||0) + (types[:opt]||0)}, received #{parsed_arg_count}" if types[:rest].nil? && (parsed_arg_count > ((types[:req]||0) + (types[:opt]||0)))
|
27
|
+
|
28
|
+
self.merge_params(parameters, parsed_args)
|
29
|
+
end
|
31
30
|
|
31
|
+
# Matches the given parameters to the given pre-parsed arguments
|
32
|
+
#
|
33
|
+
# @param [Hash] parameters a Hash representing the parameters to match.
|
34
|
+
# Entries should match :param_name => { type: :req||:opt||:rest,
|
35
|
+
# def:(source_definition),
|
36
|
+
# default:(default_value || :nil_rubycom_required_param)
|
37
|
+
# }
|
38
|
+
# @param [Hash] parsed_args a Hash of parsed arguments where the keys are either the name of the optional argument
|
39
|
+
# the value to designated for or :rubycom_non_opt_arg if the argument was not sent for a specified optional parameter
|
40
|
+
# @return [Hash] a Hash mapping the defined parameters to their matching argument values
|
41
|
+
def self.merge_params(parameters={}, parsed_args={})
|
32
42
|
parameters.map { |param_sym, def_hash|
|
33
43
|
if def_hash[:type] == :req
|
34
|
-
raise CLIError, "No argument available for #{param_sym}" if
|
35
|
-
Hash[param_sym,
|
44
|
+
raise CLIError, "No argument available for #{param_sym}" if parsed_args[:rubycom_non_opt_arg].nil? || parsed_args[:rubycom_non_opt_arg].length == 0
|
45
|
+
Hash[param_sym, parsed_args[:rubycom_non_opt_arg].shift]
|
36
46
|
elsif def_hash[:type] == :opt
|
37
|
-
if
|
38
|
-
arg = (
|
47
|
+
if parsed_args[param_sym].nil?
|
48
|
+
arg = (parsed_args[:rubycom_non_opt_arg].nil? || parsed_args[:rubycom_non_opt_arg].empty?) ? parameters[param_sym][:default] : parsed_args[:rubycom_non_opt_arg].shift
|
39
49
|
else
|
40
|
-
arg =
|
50
|
+
arg = parsed_args[param_sym]
|
41
51
|
end
|
42
52
|
Hash[param_sym, arg]
|
43
53
|
elsif def_hash[:type] == :rest
|
44
|
-
ret = Hash[param_sym, ((!
|
45
|
-
|
54
|
+
ret = Hash[param_sym, ((!parsed_args[param_sym].nil?) ? parsed_args[param_sym] : parsed_args[:rubycom_non_opt_arg])]
|
55
|
+
parsed_args[:rubycom_non_opt_arg] = []
|
46
56
|
ret
|
47
57
|
end
|
48
58
|
}.reduce(&:merge)
|
49
59
|
end
|
50
60
|
|
61
|
+
def self.parse_args(arguments)
|
62
|
+
arguments.map { |arg|
|
63
|
+
self.parse_arg(arg)
|
64
|
+
}.group_by { |hsh|
|
65
|
+
hsh.keys.first
|
66
|
+
}.map { |key, arr|
|
67
|
+
(key == :rubycom_non_opt_arg) ? Hash[key, arr.map { |hsh| hsh.values }.flatten(1)] : Hash[key, arr.map { |hsh| hsh.values.first }.reduce(&:merge)]
|
68
|
+
}.reduce(&:merge) || {}
|
69
|
+
end
|
70
|
+
|
51
71
|
# Uses YAML.load to parse the given String
|
52
72
|
#
|
53
73
|
# @param [String] arg a String representing the argument to be parsed
|
@@ -60,12 +80,23 @@ module Rubycom
|
|
60
80
|
k, v = arg.partition(/^(-|--)\w+[=|\s]{1}/).select { |part|
|
61
81
|
!part.empty?
|
62
82
|
}.each_with_index.map { |part, index|
|
63
|
-
index == 0
|
83
|
+
if index == 0
|
84
|
+
part.chomp('=').gsub(/^--/, '').gsub(/^-/, '').strip.to_sym
|
85
|
+
else
|
86
|
+
if part.start_with?("#") || part.start_with?("!")
|
87
|
+
"#{part}"
|
88
|
+
else
|
89
|
+
(YAML.load(part) rescue "#{part}")
|
90
|
+
end
|
91
|
+
end
|
64
92
|
}
|
65
93
|
Hash[k, v]
|
66
94
|
else
|
67
95
|
begin
|
68
|
-
parsed_arg =
|
96
|
+
parsed_arg = "#{arg}"
|
97
|
+
unless arg.start_with?("#") || arg.start_with?("!")
|
98
|
+
parsed_arg = YAML.load("#{arg}")
|
99
|
+
end
|
69
100
|
rescue Exception
|
70
101
|
parsed_arg = "#{arg}"
|
71
102
|
end
|
@@ -31,17 +31,7 @@ module Rubycom
|
|
31
31
|
raise CLIError, "Can not get usage for #{command_name} with base: #{base||"nil"}" if base.nil? || !base.respond_to?(:included_modules)
|
32
32
|
return 'No command specified.' if command_name.nil? || command_name.length == 0
|
33
33
|
if base.included_modules.map { |mod| mod.name.to_sym }.include?(command_name.to_sym)
|
34
|
-
|
35
|
-
mod_const = Kernel.const_get(command_name.to_sym)
|
36
|
-
desc = File.read(mod_const.public_method(mod_const.singleton_methods().first).source_location.first).split(//).reduce("") { |str, c|
|
37
|
-
unless str.gsub("\n", '').gsub(/\s+/, '').include?("module#{mod_const}")
|
38
|
-
str << c
|
39
|
-
end
|
40
|
-
str
|
41
|
-
}.split("\n").select { |line| line.strip.match(/^#/) && !line.strip.match(/^#!/) }.map { |line| line.strip.gsub(/^#+/, '') }.join("\n")
|
42
|
-
rescue
|
43
|
-
desc = ""
|
44
|
-
end
|
34
|
+
desc = self.get_module_doc(command_name)
|
45
35
|
else
|
46
36
|
raise CLIError, "Invalid command for #{base}, #{command_name}" unless base.public_methods.include?(command_name.to_sym)
|
47
37
|
desc = self.get_doc(base.public_method(command_name.to_sym))[:desc].join("\n") rescue ""
|
@@ -128,6 +118,24 @@ module Rubycom
|
|
128
118
|
}.join
|
129
119
|
end
|
130
120
|
|
121
|
+
# Retrieves the given module's documentation from it's source code
|
122
|
+
#
|
123
|
+
# @param [String] base_name
|
124
|
+
# @return [String] the documentation text from the top of the specified module
|
125
|
+
def self.get_module_doc(base_name)
|
126
|
+
begin
|
127
|
+
mod_const = Kernel.const_get(base_name.to_sym)
|
128
|
+
File.read(mod_const.public_method(mod_const.singleton_methods().first).source_location.first).split(//).reduce("") { |str, c|
|
129
|
+
unless str.gsub("\n", '').gsub(/\s+/, '').include?("module#{mod_const}")
|
130
|
+
str << c
|
131
|
+
end
|
132
|
+
str
|
133
|
+
}.split("\n").select { |line| line.strip.match(/^#/) && !line.strip.match(/^#!/) }.map { |line| line.strip.gsub(/^#+/, '') }.join("\n")
|
134
|
+
rescue
|
135
|
+
""
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
131
139
|
# Retrieves the given method's documentation from it's source code
|
132
140
|
#
|
133
141
|
# @param [Method] method the Method who's documentation should be retrieved
|
data/lib/rubycom/version.rb
CHANGED
@@ -145,4 +145,145 @@ class ArgumentsTest < Test::Unit::TestCase
|
|
145
145
|
assert_equal(expected, result)
|
146
146
|
end
|
147
147
|
|
148
|
+
def test_parse_args_no_params
|
149
|
+
test_method = UtilTestModule.public_method(:test_command)
|
150
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
151
|
+
arguments = []
|
152
|
+
expected = nil
|
153
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
154
|
+
assert_equal(expected, result)
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_parse_args_one_param
|
158
|
+
test_method = UtilTestModule.public_method(:test_command_with_arg)
|
159
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
160
|
+
arguments = ["abc"]
|
161
|
+
expected = {:test_arg=>"abc"}
|
162
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
163
|
+
assert_equal(expected, result)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_parse_args_one_param_array
|
167
|
+
test_method = UtilTestModule.public_method(:test_command_with_arg)
|
168
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
169
|
+
arguments = ["[a,b,c,1,2,3]"]
|
170
|
+
expected = {:test_arg=>['a','b','c',1,2,3]}
|
171
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
172
|
+
assert_equal(expected, result)
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_parse_args_one_param_hash
|
176
|
+
test_method = UtilTestModule.public_method(:test_command_with_arg)
|
177
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
178
|
+
arguments = ["{abc: 123}"]
|
179
|
+
expected = {:test_arg=>{"abc"=>123}}
|
180
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
181
|
+
assert_equal(expected, result)
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_parse_args_too_many_args
|
185
|
+
test_method = UtilTestModule.public_method(:test_command)
|
186
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
187
|
+
arguments = ["123"]
|
188
|
+
expected = nil
|
189
|
+
result = nil
|
190
|
+
assert_raise Rubycom::CLIError do
|
191
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
192
|
+
end
|
193
|
+
assert_equal(expected, result)
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_parse_args_too_few_args
|
197
|
+
test_method = UtilTestModule.public_method(:test_command_with_arg)
|
198
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
199
|
+
arguments = []
|
200
|
+
expected = nil
|
201
|
+
result = nil
|
202
|
+
assert_raise Rubycom::CLIError do
|
203
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
204
|
+
end
|
205
|
+
assert_equal(expected, result)
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_parse_args_with_options
|
209
|
+
test_method = UtilTestModule.public_method(:test_command_with_options)
|
210
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
211
|
+
arguments = [";asd"]
|
212
|
+
expected = {:test_arg=>";asd", :test_option=>"option_default"}
|
213
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
214
|
+
assert_equal(expected, result)
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_parse_args_with_options_filled
|
218
|
+
test_method = UtilTestModule.public_method(:test_command_with_options)
|
219
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
220
|
+
arguments = ["[1,2,3]","--test_option=abc"]
|
221
|
+
expected = {:test_arg=>[1, 2, 3], :test_option=>"abc"}
|
222
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
223
|
+
assert_equal(expected, result)
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_parse_args_all_options
|
227
|
+
test_method = UtilTestModule.public_method(:test_command_all_options)
|
228
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
229
|
+
arguments = []
|
230
|
+
expected = {:test_arg=>"test_arg_default", :test_option=>"test_option_default"}
|
231
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
232
|
+
assert_equal(expected, result)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_parse_args_all_opts_args_entered
|
236
|
+
test_method = UtilTestModule.public_method(:test_command_all_options)
|
237
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
238
|
+
arguments = ["[1,2,3]","abc"]
|
239
|
+
expected = {:test_arg=>[1, 2, 3], :test_option=>"abc"}
|
240
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
241
|
+
assert_equal(expected, result)
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_parse_args_all_opts_filled
|
245
|
+
test_method = UtilTestModule.public_method(:test_command_all_options)
|
246
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
247
|
+
arguments = ["-test_arg='123asd'","--test_option=[b,c,d]"]
|
248
|
+
expected = {:test_arg=>"123asd", :test_option=>["b","c","d"]}
|
249
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
250
|
+
assert_equal(expected, result)
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_parse_args_arr_option
|
254
|
+
test_method = UtilTestModule.public_method(:test_command_arg_arr)
|
255
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
256
|
+
arguments = ["--test_arr=['123','abc']"]
|
257
|
+
expected = {:test_arr=>["123","abc"]}
|
258
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
259
|
+
assert_equal(expected, result)
|
260
|
+
end
|
261
|
+
|
262
|
+
def test_parse_args_options_arr
|
263
|
+
test_method = UtilTestModule.public_method(:test_command_options_arr)
|
264
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
265
|
+
arguments = [",a2","1","2","3","a","b","c"]
|
266
|
+
expected = {:test_option=>",a2", :test_options=>[1, 2, 3, "a", "b", "c"]}
|
267
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
268
|
+
assert_equal(expected, result)
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_parse_args_options_arr_with_arr
|
272
|
+
test_method = UtilTestModule.public_method(:test_command_options_arr)
|
273
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
274
|
+
arguments = [",a2","[1,2,3,a,b,c]"]
|
275
|
+
expected = {:test_option=>",a2", :test_options=>[[1, 2, 3, "a", "b", "c"]]}
|
276
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
277
|
+
assert_equal(expected, result)
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_parse_args_hash
|
281
|
+
test_method = UtilTestModule.public_method(:test_command_arg_hash)
|
282
|
+
params = Rubycom::Arguments.get_param_definitions(test_method)
|
283
|
+
arguments = ["{testing: 'arg1', 123: 'abc', abc: 123}"]
|
284
|
+
expected = {:test_hash=>{"testing"=>"arg1", 123=>"abc", "abc"=>123}}
|
285
|
+
result = Rubycom::Arguments.resolve(params, arguments)
|
286
|
+
assert_equal(expected, result)
|
287
|
+
end
|
288
|
+
|
148
289
|
end
|
@@ -281,28 +281,22 @@ class RubycomTest < Test::Unit::TestCase
|
|
281
281
|
end
|
282
282
|
|
283
283
|
def test_run_options_arr
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
o_stderr, $stderr = $stderr, tst_out
|
292
|
-
|
293
|
-
base = UtilTestModule
|
294
|
-
args = ["test_command_options_arr", "test_option1", "test_option2", 1.0, false]
|
295
|
-
result = Rubycom.run(base, args)
|
284
|
+
mod = "util_test_module.rb"
|
285
|
+
command = "test_command_options_arr"
|
286
|
+
args = "test_option1 test_option2 1.0 false"
|
287
|
+
expected = 'Output is test_option=test_option1,test_option_arr=["test_option2", 1.0, false]'+"\n\n"
|
288
|
+
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
289
|
+
assert_equal(expected, result)
|
290
|
+
end
|
296
291
|
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
292
|
+
def test_run_multi_args
|
293
|
+
mod = "util_test_composite.rb"
|
294
|
+
sub_mod = "UtilTestModule"
|
295
|
+
command = "test_command_with_args"
|
296
|
+
args = "a b"
|
297
|
+
expected = 'test_arg=a,another_test_arg=b'+"\n\n"
|
298
|
+
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{sub_mod} #{command} #{args})
|
301
299
|
assert_equal(expected, result)
|
302
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
303
|
-
ensure
|
304
|
-
$stdout = o_stdout
|
305
|
-
$stderr = o_stderr
|
306
300
|
end
|
307
301
|
|
308
302
|
def test_run_missing_required_arg
|
@@ -369,6 +363,15 @@ class RubycomTest < Test::Unit::TestCase
|
|
369
363
|
assert_equal(expected, result)
|
370
364
|
end
|
371
365
|
|
366
|
+
def test_full_run_args_for_opts
|
367
|
+
mod = "util_test_module.rb"
|
368
|
+
command = "test_command_mixed_options"
|
369
|
+
args = "testing_arg test2 testing_option test_hsh_arg true some other args"
|
370
|
+
expected = "test_arg=testing_arg test_arr=test2 test_opt=testing_option test_hsh=test_hsh_arg test_bool=true test_rest=[\"some\", \"other\", \"args\"]"+"\n"
|
371
|
+
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
372
|
+
assert_equal(expected, result)
|
373
|
+
end
|
374
|
+
|
372
375
|
def test_full_run_mixed_args_solid_arr
|
373
376
|
mod = "util_test_module.rb"
|
374
377
|
command = "test_command_mixed_options"
|
@@ -414,6 +417,24 @@ class RubycomTest < Test::Unit::TestCase
|
|
414
417
|
assert_equal(expected, result)
|
415
418
|
end
|
416
419
|
|
420
|
+
def test_full_run_sharp_arg
|
421
|
+
mod = "util_test_module.rb"
|
422
|
+
command = "test_command_mixed_options"
|
423
|
+
args = '#' + " \"[test1, test2]\" -test_opt='testing_option' \"{a: 'test_hsh_arg'}\" -test_bool=true some other args"
|
424
|
+
expected = 'test_arg=# test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_bool=true test_rest=["some", "other", "args"]'+"\n"
|
425
|
+
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
426
|
+
assert_equal(expected, result)
|
427
|
+
end
|
428
|
+
|
429
|
+
def test_full_run_bang_arg
|
430
|
+
mod = "util_test_module.rb"
|
431
|
+
command = "test_command_mixed_options"
|
432
|
+
args = "! \"[test1, test2]\" -test_opt='testing_option' \"{a: 'test_hsh_arg'}\" -test_bool=true some other args"
|
433
|
+
expected = 'test_arg=! test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_bool=true test_rest=["some", "other", "args"]'+"\n"
|
434
|
+
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
435
|
+
assert_equal(expected, result)
|
436
|
+
end
|
437
|
+
|
417
438
|
def test_tab_complete_nil_arg
|
418
439
|
mod = UtilTestComposite
|
419
440
|
args = nil
|
@@ -53,7 +53,7 @@ module UtilTestModule
|
|
53
53
|
|
54
54
|
# A test_command with an options array
|
55
55
|
# @param [String] test_option an optional test argument
|
56
|
-
# @param [
|
56
|
+
# @param [Array] test_options an optional array of arguments
|
57
57
|
def self.test_command_options_arr (
|
58
58
|
test_option="test_option_default",
|
59
59
|
*test_options
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubycom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Purcell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|