rubycom 0.2.3 → 0.2.4

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzIyZGNiNTNlZGYwNTllMDcwOTFhNzE5YjMyMTE2M2YxZWFiNzM5MA==
4
+ ZDhjYTIwMGM1YWNmMjc3NWI0ZWY2NTM5NDY1NGM3MjliYTA4MTA2MA==
5
5
  data.tar.gz: !binary |-
6
- YmI0MWUwNWVkMzExYjUyNjVjOGMwNWVjYzE5ZTNhZDFhMTg4Yjk4OA==
6
+ NmU3YjBkMjYwYTg5ZTY5MjQ1NjBiNDE3ZDNkMWNhY2QyMDYyNDJlZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- Njc2YzE2YTBkNDNkN2FkZGI4MTZhNWIyNzVkNzdjYjE2YTg5ZDcyYmE1MTg0
10
- Y2RkMTg1YzY2Yzg2N2M4NTMzY2QzN2Y1MDQ1N2JkZGU2NzhjYWQ0ZWYwMmRk
11
- YjkyYTg2MTVlNzA3ZmRkZGZjMmExZDIwMmVmNTliMmMxM2JlMzA=
9
+ NmRmOThjZDFjYjRlZTNkMGFjYzViYzQ0M2M2ZGM5Y2U1NDlhNzEzOTFjYTcy
10
+ NDk0OTRmZDQ3ZjQ5M2JjY2E0ZGU2MzA4NjIyOWIyODIyMDg3ZGM1MTVhOGUy
11
+ ZTk3MzhhYTQzY2FjMDk1NGNlY2NiN2FjOWE4NTY2NDA5OWQ1YTA=
12
12
  data.tar.gz: !binary |-
13
- M2EzNTAwODE5OTdjYzNmNjYxYzc0YjRmZjRlMjU4NGVhNTE3YjI4MzZiNGFh
14
- ZWVkNDBlY2YxYmRjYzMxNGRiZWYwNzhlMzE2NzE0MmQwMjlkNDhmZjU2ZGQw
15
- Y2M5Mjg2MjM1OWIwYjE0OWI3OTVmMjBiYmU4OGE3MWU2M2JmNjI=
13
+ NGZmOTU5MDUyMjFmOGI2OTEzODdjY2Q5MTlmYTA1MDE1ZGI0OTE1ZTFmM2Vm
14
+ MDU3Mjg3NTYwOWVlOWNkOTk1MmFkZDU1ZTE3N2Q0OTkyNGUwNTg5Mzc0NmVj
15
+ NjJiNjc4ZDU4ODZhMjMzNzM3MjMzY2JkZjJkZTk1ZDZiYjIzYjU=
@@ -108,7 +108,13 @@ module Rubycom
108
108
  raise CLIError, "No public method found for symbol: #{command.to_sym}" if method.nil?
109
109
  param_defs = self.get_param_definitions(method)
110
110
  args = self.parse_arguments(param_defs, arguments)
111
- (arguments.nil? || arguments.empty?) ? method.call : method.call(*method.parameters.map { |arr| args[arr[1]]}.flatten)
111
+ flatten = false
112
+ params = method.parameters.map { |arr| flatten = true if arr[0]==:rest; args[arr[1]]}
113
+ if flatten
114
+ rest_arr = params.delete_at(-1)
115
+ rest_arr.each{|arg| params << arg}
116
+ end
117
+ (arguments.nil? || arguments.empty?) ? method.call : method.call(*params)
112
118
  end
113
119
  rescue CLIError => e
114
120
  $stderr.puts e
@@ -128,18 +134,19 @@ module Rubycom
128
134
  def self.parse_arguments(parameters={}, arguments=[])
129
135
  raise CLIError, 'parameters may not be nil' if parameters.nil?
130
136
  raise CLIError, 'arguments may not be nil' if arguments.nil?
131
- types = parameters.values.group_by { |hsh| hsh[:type] }.map { |type, defs_arr| Hash[type, defs_arr.length] }.reduce(&:merge) || {}
132
- raise CLIError, "Wrong number of arguments. Expected at least #{types[:req]}, received #{arguments.length}" if arguments.length < (types[:req]||0)
133
- raise CLIError, "Wrong number of arguments. Expected at most #{(types[:req]||0) + (types[:opt]||0)}, received #{arguments.length}" if types[:rest].nil? && (arguments.length > ((types[:req]||0) + (types[:opt]||0)))
134
-
135
137
  sorted_args = arguments.map { |arg|
136
138
  Rubycom.parse_arg(arg)
137
139
  }.group_by { |hsh|
138
140
  hsh.keys.first
139
141
  }.map { |key, arr|
140
- (key == :rubycom_non_opt_arg) ? Hash[key, arr.map { |hsh| hsh.values }.flatten] : Hash[key, arr.map { |hsh| hsh.values.first }.reduce(&:merge)]
142
+ (key == :rubycom_non_opt_arg) ? Hash[key, arr.map { |hsh| hsh.values }.flatten(1)] : Hash[key, arr.map { |hsh| hsh.values.first }.reduce(&:merge)]
141
143
  }.reduce(&:merge) || {}
142
144
 
145
+ sorted_arg_count = sorted_args.map{|key,val| val}.flatten(1).length
146
+ types = parameters.values.group_by { |hsh| hsh[:type] }.map { |type, defs_arr| Hash[type, defs_arr.length] }.reduce(&:merge) || {}
147
+ raise CLIError, "Wrong number of arguments. Expected at least #{types[:req]}, received #{sorted_arg_count}" if sorted_arg_count < (types[:req]||0)
148
+ raise CLIError, "Wrong number of arguments. Expected at most #{(types[:req]||0) + (types[:opt]||0)}, received #{sorted_arg_count}" if types[:rest].nil? && (sorted_arg_count > ((types[:req]||0) + (types[:opt]||0)))
149
+
143
150
  parameters.map { |param_sym, def_hash|
144
151
  if def_hash[:type] == :req
145
152
  raise CLIError, "No argument available for #{param_sym}" if sorted_args[:rubycom_non_opt_arg].nil? || sorted_args[:rubycom_non_opt_arg].length == 0
@@ -170,7 +177,12 @@ module Rubycom
170
177
  }
171
178
  Hash[k, v]
172
179
  else
173
- Hash[:rubycom_non_opt_arg, (YAML.load("#{arg}") rescue "#{arg}")]
180
+ begin
181
+ parsed_arg = YAML.load("#{arg}")
182
+ rescue Exception
183
+ parsed_arg = "#{arg}"
184
+ end
185
+ Hash[:rubycom_non_opt_arg, parsed_arg]
174
186
  end
175
187
  end
176
188
 
@@ -1,3 +1,3 @@
1
1
  module Rubycom
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
@@ -69,8 +69,9 @@ class TestRubycom < Test::Unit::TestCase
69
69
  def test_get_command_summary
70
70
  base = UtilTestModule
71
71
  command_name = 'test_command_with_options'
72
+ expected = "test_command_with_options - A test_command with an optional argument\n"
72
73
  result = Rubycom.get_command_summary(base, command_name)
73
- assert_equal("test_command_with_options - A test_command with an optional argument\n".gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
74
+ assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
74
75
  end
75
76
 
76
77
  def test_get_command_summary_no_command
@@ -128,6 +129,7 @@ class TestRubycom < Test::Unit::TestCase
128
129
  test_command_arg_false - A test_command with a Boolean argument
129
130
  test_command_arg_arr - A test_command with an array argument
130
131
  test_command_arg_hash - A test_command with an Hash argument
132
+ test_command_mixed_options - A test_command with several mixed options
131
133
 
132
134
  END
133
135
  assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
@@ -165,6 +167,7 @@ class TestRubycom < Test::Unit::TestCase
165
167
  test_command_arg_false - A test_command with a Boolean argument
166
168
  test_command_arg_arr - A test_command with an array argument
167
169
  test_command_arg_hash - A test_command with an Hash argument
170
+ test_command_mixed_options - A test_command with several mixed options
168
171
  END
169
172
  assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
170
173
  end
@@ -182,10 +185,11 @@ class TestRubycom < Test::Unit::TestCase
182
185
  end
183
186
 
184
187
  def test_get_top_level_commands
185
- test_command_list = [:test_command, :test_command_no_docs, :test_command_with_arg, :test_command_arg_named_arg, :test_command_with_args, :test_command_with_options,
186
- :test_command_all_options, :test_command_options_arr, :test_command_with_return,
187
- :test_command_arg_timestamp, :test_command_arg_false, :test_command_arg_arr,
188
- :test_command_arg_hash]
188
+ test_command_list = [:test_command, :test_command_no_docs, :test_command_with_arg, :test_command_arg_named_arg,
189
+ :test_command_with_args, :test_command_with_options, :test_command_all_options,
190
+ :test_command_options_arr, :test_command_with_return, :test_command_arg_timestamp,
191
+ :test_command_arg_false, :test_command_arg_arr, :test_command_arg_hash,
192
+ :test_command_mixed_options]
189
193
  result_command_list = Rubycom.get_top_level_commands(UtilTestModule)
190
194
  assert_equal(test_command_list.length, result_command_list.length)
191
195
  test_command_list.each { |sym|
@@ -414,6 +418,7 @@ class TestRubycom < Test::Unit::TestCase
414
418
  test_command_arg_false - A test_command with a Boolean argument
415
419
  test_command_arg_arr - A test_command with an array argument
416
420
  test_command_arg_hash - A test_command with an Hash argument
421
+ test_command_mixed_options - A test_command with several mixed options
417
422
  END
418
423
  expected_out = expected
419
424
  assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
@@ -700,4 +705,49 @@ class TestRubycom < Test::Unit::TestCase
700
705
  $stderr = o_stderr
701
706
  end
702
707
 
708
+ def test_full_run_mixed_args
709
+ mod = "util_test_module.rb"
710
+ command = "test_command_mixed_options"
711
+ args = "testing_arg \"[test1, test2]\" -test_opt='testing_option' \"{a: 'test_hsh_arg'}\" some other args"
712
+ expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_rest=["some", "other", "args"]'+"\n"
713
+ result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
714
+ assert_equal(expected, result)
715
+ end
716
+
717
+ def test_full_run_mixed_args_solid_arr
718
+ mod = "util_test_module.rb"
719
+ command = "test_command_mixed_options"
720
+ args = "testing_arg [test1,test2] -test_opt='testing_option' \"{a: 'test_hsh_arg'}\" some other args"
721
+ expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_rest=["some", "other", "args"]'+"\n"
722
+ result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
723
+ assert_equal(expected, result)
724
+ end
725
+
726
+ def test_full_run_mixed_args_quoted_solid_arr
727
+ mod = "util_test_module.rb"
728
+ command = "test_command_mixed_options"
729
+ args = 'testing_arg "[test1,test2]" -test_opt="testing_option" "{a: "test_hsh_arg"}" some other args'
730
+ expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_rest=["some", "other", "args"]'+"\n"
731
+ result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
732
+ assert_equal(expected, result)
733
+ end
734
+
735
+ def test_full_run_mixed_args_odd_sp
736
+ mod = "util_test_module.rb"
737
+ command = "test_command_mixed_options"
738
+ args = 'testing_arg "[ test1 , test2 ]" -test_opt="testing_option" "{ a: "test_hsh_arg" }" some other args'
739
+ expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_rest=["some", "other", "args"]'+"\n"
740
+ result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
741
+ assert_equal(expected, result)
742
+ end
743
+
744
+ def test_full_run_mixed_args_hash_rocket
745
+ mod = "util_test_module.rb"
746
+ command = "test_command_mixed_options"
747
+ args = 'testing_arg "[ test1 , test2 ]" -test_opt="testing_option" "{ :a => "test_hsh_arg" }" some other args'
748
+ expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={ :a => test_hsh_arg } test_rest=["some", "other", "args"]'+"\n"
749
+ result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
750
+ assert_equal(expected, result)
751
+ end
752
+
703
753
  end
@@ -103,5 +103,10 @@ module UtilTestModule
103
103
  test_hash
104
104
  end
105
105
 
106
+ # A test_command with several mixed options
107
+ def self.test_command_mixed_options(test_arg, test_arr=[], test_opt='test_opt_arg', test_hsh={}, *test_rest)
108
+ "test_arg=#{test_arg} test_arr=#{test_arr} test_opt=#{test_opt} test_hsh=#{test_hsh} test_rest=#{test_rest}"
109
+ end
110
+
106
111
  include Rubycom
107
112
  end
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.2.3
4
+ version: 0.2.4
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-06-15 00:00:00.000000000 Z
11
+ date: 2013-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler