libclimate-ruby 0.4.1 → 0.5.1

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d55c53e5b2ddc3f8726d800b40b4d3b314abf8c5
4
- data.tar.gz: 8846d76a971949a876c3ef83c6c022d41e6435d6
3
+ metadata.gz: 81a934726e041f8d8de3c7b1d74eee4f331320ca
4
+ data.tar.gz: d6d9d783ff08b0db0bfc25842ee76ac8dd263062
5
5
  SHA512:
6
- metadata.gz: 56e9b36042061363132ed2a8464c965947b472a484a4fb3831e482b7ae64bc9de4833713cd624fbea76744eff4001840334fe4b3735806526e6dc22cc76c451d
7
- data.tar.gz: 5ea0e2a5fa012615cb101d77047ac2c373e3fa96e395e7b45000f58f00cec22ce0ddc695095c15ada8774daedfc61d61211adb0256464dde0d2b3aac959480ec
6
+ metadata.gz: 22e38caf3b72cdaedc405ee5c36f84aadce0de9fa7e9c3dc3819037d880513f3a48af59254ebe67a4d631a9218fae4c33387155ed1857b4e88fd767edb16909c
7
+ data.tar.gz: 5b4592c4bee450fbdae5b1b074e05812e04fbc28226f0bfc354c3a1680b39b7f1053cccf206160c596bf6913a2977bb93ae8964f4b4f24c4176ad36af6feb6fd
@@ -5,7 +5,7 @@
5
5
  # Purpose: Definition of the ::LibCLImate::Climate class
6
6
  #
7
7
  # Created: 13th July 2015
8
- # Updated: 14th June 2016
8
+ # Updated: 17th June 2016
9
9
  #
10
10
  # Home: http://github.com/synesissoftware/libCLImate.Ruby
11
11
  #
@@ -42,6 +42,7 @@
42
42
 
43
43
  require 'clasp'
44
44
  require 'xqsr3/extensions/io'
45
+ require 'xqsr3/quality/parameter_checking'
45
46
 
46
47
  if !defined? Colcon
47
48
 
@@ -292,13 +293,18 @@ class Climate
292
293
  results[:flags][selector] << f
293
294
  else
294
295
 
295
- message = "#{program_name}: unrecognised flag '#{f}'; use --help for usage"
296
+ message = "unrecognised flag '#{f}'; use --help for usage"
296
297
 
297
298
  if exit_on_unknown
298
299
 
299
- abort message
300
+ self.abort message
300
301
  else
301
302
 
303
+ if program_name && !program_name.empty?
304
+
305
+ message = "#{program_name}: #{message}"
306
+ end
307
+
302
308
  stderr.puts message
303
309
  end
304
310
 
@@ -343,13 +349,18 @@ class Climate
343
349
  results[:options][selector] << o
344
350
  else
345
351
 
346
- message = "#{program_name}: unrecognised option '#{o}'; use --help for usage"
352
+ message = "unrecognised option '#{o}'; use --help for usage"
347
353
 
348
354
  if exit_on_unknown
349
355
 
350
- abort message
356
+ self.abort message
351
357
  else
352
358
 
359
+ if program_name && !program_name.empty?
360
+
361
+ message = "#{program_name}: #{message}"
362
+ end
363
+
353
364
  stderr.puts message
354
365
  end
355
366
 
@@ -417,6 +428,54 @@ class Climate
417
428
 
418
429
  msg
419
430
  end
431
+
432
+ # Adds a flag to +aliases+
433
+ #
434
+ # === Signature
435
+ #
436
+ # * *Parameters*
437
+ # - +name+:: The flag name
438
+ # - +options+:: An options hash, containing any of the following options.
439
+ #
440
+ # * *Options*
441
+ # - +:help+::
442
+ # - +:alias+::
443
+ # - +:aliases+::
444
+ # - +:extras+::
445
+ def add_flag(name, options={})
446
+
447
+ aliases << CLASP.Flag(name, **options)
448
+ end
449
+
450
+ #
451
+ # * *Options*
452
+ # - +:alias+::
453
+ # - +:aliases+::
454
+ # - +:help+::
455
+ # - +:values_range+::
456
+ # - +:default_value+::
457
+ # - +:extras+::
458
+ def add_option(name, options={})
459
+
460
+ aliases << CLASP.Option(name, **options)
461
+ end
462
+
463
+ # Adds a flag to +aliases+
464
+ #
465
+ # === Signature
466
+ #
467
+ # * *Parameters*
468
+ # - +name+:: The flag name
469
+ # - +options+:: An options hash, containing any of the following options.
470
+ def add_alias(name, *aliases)
471
+
472
+ ::Xqsr3::Quality::ParameterChecking.check_parameter name, 'name', allow_nil: false, types: [ ::String, ::Symbol ]
473
+ raise ArgumentError, "must supply at least one alias" if aliases.empty?
474
+
475
+ klass = CLASP.Option
476
+
477
+ self.aliases << klass(name, aliases: aliases)
478
+ end
420
479
  end # class Climate
421
480
 
422
481
  end # module LibCLImate
@@ -4,7 +4,7 @@
4
4
  # Purpose: Version for libclimate.Ruby library
5
5
  #
6
6
  # Created: 13th July 2015
7
- # Updated: 14th June 2016
7
+ # Updated: 16th June 2016
8
8
  #
9
9
  # Home: http://github.com/synesissoftware/libCLImate.Ruby
10
10
  #
@@ -40,7 +40,7 @@
40
40
  module LibCLImate
41
41
 
42
42
  # Current version of the libCLImate.Ruby library
43
- VERSION = '0.4.1'
43
+ VERSION = '0.5.1'
44
44
 
45
45
  private
46
46
  VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
@@ -223,8 +223,8 @@ class Test_Climate_minimal < Test::Unit::TestCase
223
223
 
224
224
  bl = false#proc { is_verbose = true }
225
225
 
226
- climate.aliases << CLASP.Flag('--succinct', alias: '-s', help: 'operates succinctly')
227
- climate.aliases << CLASP.Flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { :handle => proc { is_verbose = true }})
226
+ climate.add_flag('--succinct', alias: '-s', help: 'operates succinctly')
227
+ climate.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { :handle => proc { is_verbose = true }})
228
228
  end
229
229
 
230
230
  argv = %w{ --help --verbose --succinct }
@@ -281,7 +281,7 @@ class Test_Climate_minimal < Test::Unit::TestCase
281
281
  climate.stdout = str
282
282
  climate.exit_on_usage = false
283
283
 
284
- climate.aliases << CLASP.Flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
284
+ climate.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
285
285
  end
286
286
 
287
287
  argv = %w{ --verbose }
@@ -325,7 +325,7 @@ class Test_Climate_minimal < Test::Unit::TestCase
325
325
  climate.stdout = str
326
326
  climate.exit_on_usage = false
327
327
 
328
- climate.aliases << CLASP.Option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
328
+ climate.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
329
329
  end
330
330
 
331
331
  argv = %w{ -v 2 }
@@ -0,0 +1,363 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # test simple scenarios
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
6
+
7
+
8
+ require 'libclimate'
9
+
10
+ require 'xqsr3/extensions/test/unit'
11
+
12
+ require 'test/unit'
13
+
14
+ require 'stringio'
15
+
16
+ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
17
+
18
+ def test_no_arguments_no_mods
19
+
20
+ climate = LibCLImate::Climate.new do |climate|
21
+
22
+ ;
23
+ end
24
+
25
+ assert $stdout.equal? climate.stdout
26
+ assert $stderr.equal? climate.stderr
27
+ end
28
+
29
+ def test_no_arguments_set_streams
30
+
31
+ climate = LibCLImate::Climate.new do |climate|
32
+
33
+ climate.stdout = $stdout
34
+ climate.stderr = $stderr
35
+ end
36
+
37
+ assert $stdout.equal? climate.stdout
38
+ assert $stderr.equal? climate.stderr
39
+ end
40
+
41
+ def test_help_to_string
42
+
43
+ str = StringIO.new
44
+
45
+ climate = LibCLImate::Climate.new do |climate|
46
+
47
+ climate.program_name = 'program'
48
+ climate.stdout = str
49
+ climate.exit_on_usage = false
50
+ end
51
+
52
+ argv = %w{ --help }
53
+
54
+ r = climate.run argv
55
+
56
+ assert_not_nil r
57
+ assert_kind_of ::Hash, r
58
+ assert_equal 3, r.size
59
+ assert_not_nil r[:flags]
60
+ assert_not_nil r[:options]
61
+ assert_not_nil r[:values]
62
+ assert_equal 4, r[:flags].size
63
+ assert_equal 4, r.flags.size
64
+ assert_equal 1, r[:flags][:given].size
65
+ assert_equal 1, r[:flags][:handled].size
66
+ assert_equal 0, r[:flags][:unhandled].size
67
+ assert_equal 0, r[:flags][:unknown].size
68
+ assert_equal 4, r[:options].size
69
+ assert_equal 0, r[:options][:given].size
70
+ assert_equal 0, r[:options][:handled].size
71
+ assert_equal 0, r[:options][:unhandled].size
72
+ assert_equal 0, r[:options][:unknown].size
73
+ assert_equal 0, r[:values].size
74
+
75
+ lines = str.string.split(/\n/)
76
+ lines = lines.reject { |line| line.chomp.strip.empty? }
77
+ lines = lines.map { |line| line.chomp.strip }
78
+
79
+ assert_equal 6, lines.size
80
+ assert_equal "USAGE: program [ ... flags and options ... ]", lines[0]
81
+ assert_equal "flags/options:", lines[1]
82
+ assert_equal "--help", lines[2]
83
+ assert_equal "shows this help and terminates", lines[3]
84
+ assert_equal "--version", lines[4]
85
+ assert_equal "shows version and terminates", lines[5]
86
+ end
87
+
88
+ def test_help_to_string_with_info_line
89
+
90
+ str = StringIO.new
91
+
92
+ climate = LibCLImate::Climate.new do |climate|
93
+
94
+ climate.program_name = 'program'
95
+ climate.info_lines = 'Synesis Software Open Source'
96
+ climate.stdout = str
97
+ climate.exit_on_usage = false
98
+ end
99
+
100
+ argv = %w{ --help }
101
+
102
+ r = climate.run argv
103
+
104
+ assert_not_nil r
105
+ assert_kind_of ::Hash, r
106
+ assert_equal 3, r.size
107
+ assert_not_nil r[:flags]
108
+ assert_not_nil r[:options]
109
+ assert_not_nil r[:values]
110
+ assert_equal 4, r[:flags].size
111
+ assert_equal 4, r.flags.size
112
+ assert_equal 1, r[:flags][:given].size
113
+ assert_equal 1, r[:flags][:handled].size
114
+ assert_equal 0, r[:flags][:unhandled].size
115
+ assert_equal 0, r[:flags][:unknown].size
116
+ assert_equal 4, r[:options].size
117
+ assert_equal 0, r[:options][:given].size
118
+ assert_equal 0, r[:options][:handled].size
119
+ assert_equal 0, r[:options][:unhandled].size
120
+ assert_equal 0, r[:options][:unknown].size
121
+ assert_equal 0, r[:values].size
122
+
123
+ lines = str.string.split(/\n/)
124
+ lines = lines.reject { |line| line.chomp.strip.empty? }
125
+ lines = lines.map { |line| line.chomp.strip }
126
+
127
+ assert_equal 7, lines.size
128
+ index = -1
129
+ assert_equal 'Synesis Software Open Source', lines[index += 1]
130
+ assert_equal "USAGE: program [ ... flags and options ... ]", lines[index += 1]
131
+ assert_equal "flags/options:", lines[index += 1]
132
+ assert_equal "--help", lines[index += 1]
133
+ assert_equal "shows this help and terminates", lines[index += 1]
134
+ assert_equal "--version", lines[index += 1]
135
+ assert_equal "shows version and terminates", lines[index += 1]
136
+ end
137
+
138
+ def test_version_to_string
139
+
140
+ str = StringIO.new
141
+
142
+ climate = LibCLImate::Climate.new do |climate|
143
+
144
+ climate.program_name = 'program'
145
+ climate.version = [ 1, 2, 3, 4 ]
146
+ climate.stdout = str
147
+ climate.exit_on_usage = false
148
+ end
149
+
150
+ argv = %w{ --version }
151
+
152
+ climate.run argv
153
+
154
+ lines = str.string.split(/\n/)
155
+ lines = lines.reject { |line| line.chomp.strip.empty? }
156
+ lines = lines.map { |line| line.chomp.strip }
157
+
158
+ assert_equal 1, lines.size
159
+ assert_equal "program 1.2.3.4", lines[0]
160
+ end
161
+
162
+ def test_unrecognised_flag
163
+
164
+ strout = StringIO.new
165
+ strerr = StringIO.new
166
+
167
+ climate = LibCLImate::Climate.new do |climate|
168
+
169
+ climate.program_name = 'program'
170
+ climate.stdout = strout
171
+ climate.stderr = strerr
172
+ climate.exit_on_unknown = false
173
+ end
174
+
175
+ argv = %w{ --unknown }
176
+
177
+ climate.run argv
178
+
179
+ lines_out = strout.string.split /\n/
180
+ lines_err = strerr.string.split /\n/
181
+
182
+ assert_equal 0, lines_out.size
183
+ assert_equal 1, lines_err.size
184
+ assert_equal "program: unrecognised flag '--unknown'; use --help for usage", lines_err[0]
185
+ end
186
+
187
+ def test_unrecognised_option
188
+
189
+ strout = StringIO.new
190
+ strerr = StringIO.new
191
+
192
+ climate = LibCLImate::Climate.new do |climate|
193
+
194
+ climate.program_name = 'program'
195
+ climate.stdout = strout
196
+ climate.stderr = strerr
197
+ climate.exit_on_unknown = false
198
+ end
199
+
200
+ argv = %w{ --unknown=10 }
201
+
202
+ climate.run argv
203
+
204
+ lines_out = strout.string.split /\n/
205
+ lines_err = strerr.string.split /\n/
206
+
207
+ assert_equal 0, lines_out.size
208
+ assert_equal 1, lines_err.size
209
+ assert_equal "program: unrecognised option '--unknown=10'; use --help for usage", lines_err[0]
210
+ end
211
+
212
+ def test_one_custom_flag_help_to_string
213
+
214
+ str = StringIO.new
215
+
216
+ is_verbose = false
217
+
218
+ climate = LibCLImate::Climate.new do |climate|
219
+
220
+ climate.program_name = 'program'
221
+ climate.stdout = str
222
+ climate.exit_on_usage = false
223
+
224
+ bl = false#proc { is_verbose = true }
225
+
226
+ climate.aliases << CLASP.Flag('--succinct', alias: '-s', help: 'operates succinctly')
227
+ climate.aliases << CLASP.Flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { :handle => proc { is_verbose = true }})
228
+ end
229
+
230
+ argv = %w{ --help --verbose --succinct }
231
+
232
+ r = climate.run argv
233
+
234
+ assert_not_nil r
235
+ assert_kind_of ::Hash, r
236
+ assert_equal 3, r.size
237
+ assert_not_nil r[:flags]
238
+ assert_not_nil r[:options]
239
+ assert_not_nil r[:values]
240
+ assert_equal 4, r[:flags].size
241
+ assert_equal 3, r[:flags][:given].size
242
+ assert_equal 2, r[:flags][:handled].size
243
+ assert_equal 1, r[:flags][:unhandled].size
244
+ assert_equal 0, r[:flags][:unknown].size
245
+ assert_equal 4, r[:options].size
246
+ assert_equal 0, r[:options][:given].size
247
+ assert_equal 0, r[:options][:handled].size
248
+ assert_equal 0, r[:options][:unhandled].size
249
+ assert_equal 0, r[:options][:unknown].size
250
+ assert_equal 0, r[:values].size
251
+ assert_equal 0, r.values.size
252
+ lines = str.string.split(/\n/)
253
+ lines = lines.reject { |line| line.chomp.strip.empty? }
254
+ lines = lines.map { |line| line.chomp.strip }
255
+
256
+ assert_equal 12, lines.size
257
+ index = -1
258
+ assert_equal "USAGE: program [ ... flags and options ... ]", lines[index += 1]
259
+ assert_equal "flags/options:", lines[index += 1]
260
+ assert_equal "--help", lines[index += 1]
261
+ assert_equal "shows this help and terminates", lines[index += 1]
262
+ assert_equal "--version", lines[index += 1]
263
+ assert_equal "shows version and terminates", lines[index += 1]
264
+ assert_equal "-s", lines[index += 1]
265
+ assert_equal "--succinct", lines[index += 1]
266
+ assert_equal "operates succinctly", lines[index += 1]
267
+ assert_equal "-v", lines[index += 1]
268
+ assert_equal "--verbose", lines[index += 1]
269
+ assert_equal "operates verbosely", lines[index += 1]
270
+ end
271
+
272
+ def test_one_custom_flag_with_select
273
+
274
+ str = StringIO.new
275
+
276
+ is_verbose = false
277
+
278
+ climate = LibCLImate::Climate.new do |climate|
279
+
280
+ climate.program_name = 'program'
281
+ climate.stdout = str
282
+ climate.exit_on_usage = false
283
+
284
+ climate.aliases << CLASP.Flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
285
+ end
286
+
287
+ argv = %w{ --verbose }
288
+
289
+ r = climate.run argv
290
+
291
+ assert_not_nil r
292
+ assert_kind_of ::Hash, r
293
+ assert_equal 3, r.size
294
+ assert_not_nil r[:flags]
295
+ assert_not_nil r[:options]
296
+ assert_not_nil r[:values]
297
+ assert_equal 4, r[:flags].size
298
+ assert_equal 1, r[:flags][:given].size
299
+ assert_equal 1, r[:flags][:handled].size
300
+ assert_equal 0, r[:flags][:unhandled].size
301
+ assert_equal 0, r[:flags][:unknown].size
302
+ assert_equal 4, r[:options].size
303
+ assert_equal 0, r[:options][:given].size
304
+ assert_equal 0, r[:options][:handled].size
305
+ assert_equal 0, r[:options][:unhandled].size
306
+ assert_equal 0, r[:options][:unknown].size
307
+ assert_equal 0, r[:values].size
308
+ lines = str.string.split(/\n/)
309
+ lines = lines.reject { |line| line.chomp.strip.empty? }
310
+ lines = lines.map { |line| line.chomp.strip }
311
+
312
+ assert_equal 0, lines.size
313
+ assert is_verbose, "is_verbose not altered"
314
+ end
315
+
316
+ def test_one_custom_option_with_select
317
+
318
+ str = StringIO.new
319
+
320
+ verbosity = 1
321
+
322
+ climate = LibCLImate::Climate.new do |climate|
323
+
324
+ climate.program_name = 'program'
325
+ climate.stdout = str
326
+ climate.exit_on_usage = false
327
+
328
+ climate.aliases << CLASP.Option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
329
+ end
330
+
331
+ argv = %w{ -v 2 }
332
+
333
+ r = climate.run argv
334
+
335
+ assert_not_nil r
336
+ assert_kind_of ::Hash, r
337
+ assert_equal 3, r.size
338
+ assert_not_nil r[:flags]
339
+ assert_not_nil r[:options]
340
+ assert_not_nil r[:values]
341
+ assert_equal 4, r[:flags].size
342
+ assert_equal 0, r[:flags][:given].size
343
+ assert_equal 0, r[:flags][:handled].size
344
+ assert_equal 0, r[:flags][:unhandled].size
345
+ assert_equal 0, r[:flags][:unknown].size
346
+ assert_equal 4, r[:options].size
347
+ assert_equal 1, r[:options][:given].size
348
+ assert_equal 1, r[:options][:handled].size
349
+ assert_equal 0, r[:options][:unhandled].size
350
+ assert_equal 0, r[:options][:unknown].size
351
+ assert_equal 0, r[:values].size
352
+ lines = str.string.split(/\n/)
353
+ lines = lines.reject { |line| line.chomp.strip.empty? }
354
+ lines = lines.map { |line| line.chomp.strip }
355
+
356
+ assert_equal 0, lines.size
357
+ assert_equal '2', verbosity
358
+ end
359
+ end
360
+
361
+ # ############################## end of file ############################# #
362
+
363
+
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libclimate-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-14 00:00:00.000000000 Z
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clasp-ruby
@@ -57,18 +57,19 @@ executables: []
57
57
  extensions: []
58
58
  extra_rdoc_files: []
59
59
  files:
60
+ - LICENSE
61
+ - README.md
62
+ - lib/libclimate.rb
60
63
  - lib/libclimate/climate.rb
61
64
  - lib/libclimate/libclimate.rb
62
65
  - lib/libclimate/version.rb
63
- - lib/libclimate.rb
64
66
  - test/scratch/blankzeroes.rb
65
67
  - test/unit/tc_abort.rb
66
68
  - test/unit/tc_minimal.rb
69
+ - test/unit/tc_minimal_clasp.rb
67
70
  - test/unit/tc_test_aliases.rb
68
71
  - test/unit/tc_with_blocks.rb
69
72
  - test/unit/ts_all.rb
70
- - README.md
71
- - LICENSE
72
73
  homepage: http://www.libclimate.org/
73
74
  licenses:
74
75
  - Modified BSD
@@ -89,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
90
  version: '0'
90
91
  requirements: []
91
92
  rubyforge_project:
92
- rubygems_version: 2.0.14.1
93
+ rubygems_version: 2.4.2
93
94
  signing_key:
94
95
  specification_version: 4
95
96
  summary: libCLImate.Ruby