libclimate-ruby 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c5040323c276e22e2dcf86fbebf1e66bf159904d9a28e764ff913aed1d9a508
4
- data.tar.gz: 9551c44bc9ca7bb1f69783d8c6b1e5ae0288548140dcd86023cd80df6fa98c14
3
+ metadata.gz: f003cbc8499db9924001bc35c3c5312de4e6423151d9acfaa8e835e0f7f331f3
4
+ data.tar.gz: 65e7371f5ded01f26074bad1413f329abd8c62237556162cef5ca0e9397deb9d
5
5
  SHA512:
6
- metadata.gz: 158c64020110e066c1f57be6c4820e9519bed9fccee5883b903dd8d8f4c49d91f971bcfef8e2ff3fd416e6074be0149562098305b56a7cd705aa2619199c7620
7
- data.tar.gz: 7672ba94c8724a7504f12d589ff20e703b31eb5ae5e27e6f1878ba8136a8ab2f0749e307b5d00ef11cfca277641c3c3479808c0a2d2406be1247abe887ae3f93
6
+ metadata.gz: 52fc0d2b96d707bedb7e12643e0cfc1d9a3724003f7454c8b64bc573b1704360fe8ce366bca0388411957f0420e777629e8e631038c3b6524aebaa2af92cfd9a
7
+ data.tar.gz: 5909f06e2b6ad0ca47e3da8d141382e20c93f17f80d2b8ea397364c9c0ebf7159e510373de0ed2a743ece65ae6a78202e9fed8c261f8fdbc692a044876f89f04
@@ -0,0 +1,225 @@
1
+ # libCLImate.Ruby Example - **flag_and_option_specifications.from_DATA**
2
+
3
+ ## Summary
4
+
5
+ Example illustrating various kinds of *flag* and *option* specifications, including the combination of short-names, loaded from the ``__END__``/``DATA`` section of the program file in **YAML** form.
6
+
7
+ ## Source
8
+
9
+ ```ruby
10
+ #!/usr/bin/env ruby
11
+
12
+ # examples/flag_and_option_specifications.from_DATA.rb
13
+
14
+
15
+ # requires
16
+
17
+ require 'libclimate'
18
+
19
+
20
+ # Specify aliases, parse, and checking standard flags
21
+
22
+ options = {}
23
+ climate = LibCLImate::Climate.load DATA do |cl|
24
+
25
+ cl.on_flag('--debug') { options[:debug] = true }
26
+
27
+ cl.on_option('--verbosity') { |o, a| options[:verbosity] = o.value }
28
+ end
29
+
30
+ r = climate.run ARGV
31
+
32
+
33
+ # Program-specific processing of flags/options
34
+
35
+ if options[:verbosity]
36
+
37
+ $stdout.puts "verbosity is specified as: #{options[:verbosity]}"
38
+ end
39
+
40
+ if options[:debug]
41
+
42
+ $stdout.puts 'Debug mode is specified'
43
+ end
44
+
45
+
46
+ # some notional output
47
+
48
+ $stdout.puts "processing in '#{r.values[0]}'" + (r.values.size > 1 ? " and '#{r.values[1]}'" : '')
49
+
50
+
51
+ __END__
52
+ ---
53
+ libclimate:
54
+ clasp:
55
+ specifications:
56
+ - flag:
57
+ name: --debug
58
+ alias: -d
59
+ help: runs in Debug mode
60
+ required: false
61
+ - option:
62
+ name: --verbosity
63
+ alias: -v
64
+ help: specifies the verbosity
65
+ values:
66
+ - silent
67
+ - quiet
68
+ - terse
69
+ - chatty
70
+ - verbose
71
+ - alias:
72
+ resolved: --verbosity=chatty
73
+ aliases:
74
+ - --chatty
75
+ - -c
76
+ constrain_values: !ruby/range 1..2
77
+ exit_on_missing: true
78
+ flags_and_options: "[... flags/options ...]"
79
+ usage_values: "<dir-1> [ <dir-2> ]"
80
+ value_names:
81
+ - first directory
82
+ - second directory
83
+ info_lines:
84
+ - libCLImate.Ruby examples
85
+ - :version
86
+ - Illustrates use of libCLImate.Ruby's specification of flags, options, and aliases, from DATA
87
+ -
88
+ version:
89
+ - 0
90
+ - 1
91
+ - "2"
92
+ ```
93
+ ```ruby
94
+ ```
95
+
96
+ ## Usage
97
+
98
+ NOTE: in order to demonstrate fully the utility of the *loading-from-source-file-YAML* facility, I've added a constraint for 1-2 values - notionally directories -
99
+
100
+ ### No arguments
101
+
102
+ If executed with no arguments
103
+
104
+ ```
105
+ ruby examples/flag_and_option_specifications.from_DATA.rb
106
+ ```
107
+
108
+ or (in a Unix shell):
109
+
110
+ ```
111
+ ./examples/flag_and_option_specifications.from_DATA.rb
112
+ ```
113
+
114
+ it gives the output (with an exit code of **1**):
115
+
116
+ ```
117
+ flag_and_option_specifications.from_DATA(.rb): first directory not specified; use --help for usage
118
+ ```
119
+
120
+
121
+ ### Show usage
122
+
123
+ If executed with the arguments
124
+
125
+ ```
126
+ ruby examples/flag_and_option_specifications.from_DATA.rb --help
127
+ ```
128
+
129
+ it gives the output:
130
+
131
+ ```
132
+ libCLImate.Ruby examples
133
+ flag_and_option_specifications.from_DATA(.rb) 0.1.2
134
+ Illustrates use of libCLImate.Ruby's specification of flags, options, and aliases, from DATA
135
+
136
+ USAGE: flag_and_option_specifications.from_DATA(.rb) [... flags/options ...] <dir-1> [ <dir-2> ]
137
+
138
+ flags/options:
139
+
140
+ --help
141
+ shows this help and terminates
142
+
143
+ --version
144
+ shows version and terminates
145
+
146
+ -d
147
+ --debug
148
+ runs in Debug mode
149
+
150
+ --chatty --verbosity=chatty
151
+ -c --verbosity=chatty
152
+ --verbosity=<value>
153
+ specifies the verbosity
154
+ where <value> one of:
155
+ silent
156
+ quiet
157
+ terse
158
+ chatty
159
+ verbose
160
+
161
+ ```
162
+
163
+ ### Specify flags and options in long-form
164
+
165
+ If executed with the arguments
166
+
167
+ ```
168
+ ruby examples/flag_and_option_specifications.from_DATA.rb dir-1 dir-2 --debug --verbosity=silent
169
+ ```
170
+
171
+ it gives the output:
172
+
173
+ ```
174
+ verbosity is specified as: silent
175
+ Debug mode is specified
176
+ processing in 'dir-1' and 'dir-2'
177
+ ```
178
+
179
+ ### Specify flags and options in short-form
180
+
181
+ If executed with the arguments
182
+
183
+ ```
184
+ ruby examples/flag_and_option_specifications.from_DATA.rb dir-1 dir-2 -v silent -d
185
+ ```
186
+
187
+ it gives the (same) output:
188
+
189
+ ```
190
+ verbosity is specified as: silent
191
+ Debug mode is specified
192
+ processing in 'dir-1' and 'dir-2'
193
+ ```
194
+
195
+ ### Specify flags and options in short-form, including an alias for an option-with-value
196
+
197
+ If executed with the arguments
198
+
199
+ ```
200
+ ruby examples/flag_and_option_specifications.from_DATA.rb dir-1 dir-2 -c -d
201
+ ```
202
+
203
+ it gives the output:
204
+
205
+ ```
206
+ verbosity is specified as: chatty
207
+ Debug mode is specified
208
+ processing in 'dir-1' and 'dir-2'
209
+ ```
210
+
211
+ ### Specify flags and options with combined short-form
212
+
213
+ If executed with the arguments
214
+
215
+ ```
216
+ ruby examples/flag_and_option_specifications.from_DATA.rb dir-1 dir-2 -dc
217
+ ```
218
+
219
+ it gives the (same) output:
220
+
221
+ ```
222
+ verbosity is specified as: chatty
223
+ Debug mode is specified
224
+ processing in 'dir-1' and 'dir-2'
225
+ ```
@@ -8,11 +8,6 @@
8
8
  require 'libclimate'
9
9
 
10
10
 
11
- # constants
12
-
13
- PROGRAM_VERSION = '0.0.1'
14
-
15
-
16
11
  # Specify aliases, parse, and checking standard flags
17
12
 
18
13
  options = {}
@@ -39,6 +34,11 @@ if options[:debug]
39
34
  end
40
35
 
41
36
 
37
+ # some notional output
38
+
39
+ $stdout.puts "processing in '#{r.values[0]}'" + (r.values.size > 1 ? " and '#{r.values[1]}'" : '')
40
+
41
+
42
42
  __END__
43
43
  ---
44
44
  libclimate:
@@ -51,6 +51,7 @@ libclimate:
51
51
  required: false
52
52
  - option:
53
53
  name: --verbosity
54
+ alias: -v
54
55
  help: specifies the verbosity
55
56
  values:
56
57
  - silent
@@ -66,10 +67,10 @@ libclimate:
66
67
  constrain_values: !ruby/range 1..2
67
68
  exit_on_missing: true
68
69
  flags_and_options: "[... flags/options ...]"
69
- usage_values: "<directory-1> [ <directory-2> ]"
70
+ usage_values: "<dir-1> [ <dir-2> ]"
70
71
  value_names:
71
- - directory-1
72
- - directory-2
72
+ - first directory
73
+ - second directory
73
74
  info_lines:
74
75
  - libCLImate.Ruby examples
75
76
  - :version
@@ -77,6 +78,6 @@ libclimate:
77
78
  -
78
79
  version:
79
80
  - 0
80
- - 3
81
- - "4"
81
+ - 1
82
+ - "2"
82
83
 
@@ -1,4 +1,4 @@
1
- # libCLImate.Ruby Example - **show_usage_and_version**
1
+ # libCLImate.Ruby Example - **flag_and_option_specifications**
2
2
 
3
3
  ## Summary
4
4
 
@@ -5,7 +5,7 @@
5
5
  # Purpose: Definition of the ::LibCLImate::Climate class
6
6
  #
7
7
  # Created: 13th July 2015
8
- # Updated: 13th April 2019
8
+ # Updated: 15th April 2019
9
9
  #
10
10
  # Home: http://github.com/synesissoftware/libCLImate.Ruby
11
11
  #
@@ -173,6 +173,17 @@ class Climate
173
173
  GIVEN_SPECS_ = "_Given_Specs_01B59422_8407_4c89_9432_8160C52BD5AD"
174
174
  end # module Climate_Constants_
175
175
 
176
+ def make_abort_message_ msg
177
+
178
+ if 0 != (usage_help_suffix || 0).size
179
+
180
+ "#{msg}; #{usage_help_suffix}"
181
+ else
182
+
183
+ msg
184
+ end
185
+ end
186
+
176
187
  def show_usage_
177
188
 
178
189
  options = {}
@@ -475,6 +486,7 @@ class Climate
475
486
  @stderr = $stderr
476
487
  @constrain_values = nil
477
488
  @flags_and_options = flags_and_options
489
+ @usage_help_suffix = 'use --help for usage'
478
490
  @usage_values = usage_values
479
491
  @value_names = []
480
492
  version_context = options[:version_context]
@@ -530,6 +542,8 @@ class Climate
530
542
  attr_accessor :constrain_values
531
543
  # (String) Optional string to describe the flags and options section. Defaults to "[ +...+ +flags+ +and+ +options+ +...+ ]"
532
544
  attr_accessor :flags_and_options
545
+ # (String) The string that is appended to #abort calls made during #run. Defaults to "use --help for usage"
546
+ attr_accessor :usage_help_suffix
533
547
  # @return (::String) Optional string to describe the program values, eg \<xyz "[ { <<directory> | &lt;file> } ]"
534
548
  attr_accessor :usage_values
535
549
  # ([String]) Zero-based array of names for values to be used when that value is not present (according to the +:constrain_values+ attribute)
@@ -625,7 +639,7 @@ class Climate
625
639
  results[:flags][selector] << f
626
640
  else
627
641
 
628
- message = "unrecognised flag '#{f}'; use --help for usage"
642
+ message = make_abort_message_("unrecognised flag '#{f}'")
629
643
 
630
644
  if false
631
645
 
@@ -687,7 +701,7 @@ class Climate
687
701
  results[:options][selector] << o
688
702
  else
689
703
 
690
- message = "unrecognised option '#{o}'; use --help for usage"
704
+ message = make_abort_message_("unrecognised option '#{o}'")
691
705
 
692
706
  if false
693
707
 
@@ -756,16 +770,37 @@ class Climate
756
770
  when nil
757
771
 
758
772
  ;
773
+ when ::Array
774
+
775
+ warn "value of 'constrain_values' attribute, if an #{::Array}, must not be empty and all elements must be of type #{::Integer}" if values_constraint.empty? || !values_constraint.all? { |v| ::Integer === v }
776
+
777
+ unless values_constraint.include? values.size
778
+
779
+ message = make_abort_message_("wrong number of values: #{values.size} given, #{values_constraint} required")
780
+
781
+ if exit_on_missing
782
+
783
+ self.abort message
784
+ else
785
+
786
+ if program_name && !program_name.empty?
787
+
788
+ message = "#{program_name}: #{message}"
789
+ end
790
+
791
+ stderr.puts message
792
+ end
793
+ end
759
794
  when ::Integer
760
795
 
761
796
  unless values.size == values_constraint
762
797
 
763
798
  if name = val_names[values.size]
764
799
 
765
- message = name + ' not specified; use --help for usage'
800
+ message = make_abort_message_(name + ' not specified')
766
801
  else
767
802
 
768
- message = "wrong number of values: #{values.size} given, #{values_constraint} required; use --help for usage"
803
+ message = make_abort_message_("wrong number of values: #{values.size} given, #{values_constraint} required")
769
804
  end
770
805
 
771
806
  if exit_on_missing
@@ -787,10 +822,10 @@ class Climate
787
822
 
788
823
  if name = val_names[values.size]
789
824
 
790
- message = name + ' not specified; use --help for usage'
825
+ message = make_abort_message_(name + ' not specified')
791
826
  else
792
827
 
793
- message = "wrong number of values: #{values.size} givens, #{values_constraint.begin} - #{values_constraint.end - (values_constraint.exclude_end? ? 1 : 0)} required; use --help for usage"
828
+ message = make_abort_message_("wrong number of values: #{values.size} givens, #{values_constraint.begin} - #{values_constraint.end - (values_constraint.exclude_end? ? 1 : 0)} required")
794
829
  end
795
830
 
796
831
  if exit_on_missing
@@ -808,7 +843,7 @@ class Climate
808
843
  end
809
844
  else
810
845
 
811
- warn "value of 'constrain_values' attribute - '#{constrain_values}' (#{constrain_values.class}) - of wrong type : must be #{::Integer}, #{::Range}, or nil"
846
+ warn "value of 'constrain_values' attribute - '#{constrain_values}' (#{constrain_values.class}) - of wrong type : must be #{::Array}, #{::Integer}, #{::Range}, or nil"
812
847
  end
813
848
 
814
849
 
@@ -4,7 +4,7 @@
4
4
  # Purpose: Version for libclimate.Ruby library
5
5
  #
6
6
  # Created: 13th July 2015
7
- # Updated: 13th April 2019
7
+ # Updated: 15th April 2019
8
8
  #
9
9
  # Home: http://github.com/synesissoftware/libCLImate.Ruby
10
10
  #
@@ -43,7 +43,7 @@
43
43
  module LibCLImate
44
44
 
45
45
  # Current version of the libCLImate.Ruby library
46
- VERSION = '0.13.0'
46
+ VERSION = '0.14.0'
47
47
 
48
48
  private
49
49
  VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
@@ -20,11 +20,11 @@ class Test_Climate_abort < Test::Unit::TestCase
20
20
  strout = StringIO.new
21
21
  strerr = StringIO.new
22
22
 
23
- climate = LibCLImate::Climate.new do |climate|
23
+ climate = LibCLImate::Climate.new do |cl|
24
24
 
25
- climate.program_name = 'myprog'
26
- climate.stdout = strout
27
- climate.stderr = strerr
25
+ cl.program_name = 'myprog'
26
+ cl.stdout = strout
27
+ cl.stderr = strerr
28
28
  end
29
29
 
30
30
  s = climate.abort 'something happened', exit: nil
@@ -42,11 +42,11 @@ class Test_Climate_abort < Test::Unit::TestCase
42
42
  strout = StringIO.new
43
43
  strerr = StringIO.new
44
44
 
45
- climate = LibCLImate::Climate.new do |climate|
45
+ climate = LibCLImate::Climate.new do |cl|
46
46
 
47
- climate.program_name = 'myprog'
48
- climate.stdout = strout
49
- climate.stderr = strerr
47
+ cl.program_name = 'myprog'
48
+ cl.stdout = strout
49
+ cl.stderr = strerr
50
50
  end
51
51
 
52
52
  s = climate.abort 'something happened', exit: nil, program_name: ''
@@ -64,11 +64,11 @@ class Test_Climate_abort < Test::Unit::TestCase
64
64
  strout = StringIO.new
65
65
  strerr = StringIO.new
66
66
 
67
- climate = LibCLImate::Climate.new do |climate|
67
+ climate = LibCLImate::Climate.new do |cl|
68
68
 
69
- climate.program_name = 'myprog'
70
- climate.stdout = strout
71
- climate.stderr = strerr
69
+ cl.program_name = 'myprog'
70
+ cl.stdout = strout
71
+ cl.stderr = strerr
72
72
  end
73
73
 
74
74
  s = climate.abort 'something happened', exit: nil, program_name: 'my-prog'
@@ -17,7 +17,7 @@ class Test_Climate_minimal < Test::Unit::TestCase
17
17
 
18
18
  def test_no_arguments_no_mods
19
19
 
20
- climate = LibCLImate::Climate.new do |climate|
20
+ climate = LibCLImate::Climate.new do |cl|
21
21
 
22
22
  ;
23
23
  end
@@ -28,10 +28,10 @@ class Test_Climate_minimal < Test::Unit::TestCase
28
28
 
29
29
  def test_no_arguments_set_streams
30
30
 
31
- climate = LibCLImate::Climate.new do |climate|
31
+ climate = LibCLImate::Climate.new do |cl|
32
32
 
33
- climate.stdout = $stdout
34
- climate.stderr = $stderr
33
+ cl.stdout = $stdout
34
+ cl.stderr = $stderr
35
35
  end
36
36
 
37
37
  assert $stdout.equal? climate.stdout
@@ -42,11 +42,11 @@ class Test_Climate_minimal < Test::Unit::TestCase
42
42
 
43
43
  str = StringIO.new
44
44
 
45
- climate = LibCLImate::Climate.new do |climate|
45
+ climate = LibCLImate::Climate.new do |cl|
46
46
 
47
- climate.program_name = 'program'
48
- climate.stdout = str
49
- climate.exit_on_usage = false
47
+ cl.program_name = 'program'
48
+ cl.stdout = str
49
+ cl.exit_on_usage = false
50
50
  end
51
51
 
52
52
  argv = %w{ --help }
@@ -89,12 +89,12 @@ class Test_Climate_minimal < Test::Unit::TestCase
89
89
 
90
90
  str = StringIO.new
91
91
 
92
- climate = LibCLImate::Climate.new do |climate|
92
+ climate = LibCLImate::Climate.new do |cl|
93
93
 
94
- climate.program_name = 'program'
95
- climate.info_lines = 'Synesis Software Open Source'
96
- climate.stdout = str
97
- climate.exit_on_usage = false
94
+ cl.program_name = 'program'
95
+ cl.info_lines = 'Synesis Software Open Source'
96
+ cl.stdout = str
97
+ cl.exit_on_usage = false
98
98
  end
99
99
 
100
100
  argv = %w{ --help }
@@ -139,12 +139,12 @@ class Test_Climate_minimal < Test::Unit::TestCase
139
139
 
140
140
  str = StringIO.new
141
141
 
142
- climate = LibCLImate::Climate.new do |climate|
142
+ climate = LibCLImate::Climate.new do |cl|
143
143
 
144
- climate.program_name = 'program'
145
- climate.version = [ 1, 2, 3, 4 ]
146
- climate.stdout = str
147
- climate.exit_on_usage = false
144
+ cl.program_name = 'program'
145
+ cl.version = [ 1, 2, 3, 4 ]
146
+ cl.stdout = str
147
+ cl.exit_on_usage = false
148
148
  end
149
149
 
150
150
  argv = %w{ --version }
@@ -164,12 +164,12 @@ class Test_Climate_minimal < Test::Unit::TestCase
164
164
  strout = StringIO.new
165
165
  strerr = StringIO.new
166
166
 
167
- climate = LibCLImate::Climate.new do |climate|
167
+ climate = LibCLImate::Climate.new do |cl|
168
168
 
169
- climate.program_name = 'program'
170
- climate.stdout = strout
171
- climate.stderr = strerr
172
- climate.exit_on_unknown = false
169
+ cl.program_name = 'program'
170
+ cl.stdout = strout
171
+ cl.stderr = strerr
172
+ cl.exit_on_unknown = false
173
173
  end
174
174
 
175
175
  argv = %w{ --unknown }
@@ -184,17 +184,43 @@ class Test_Climate_minimal < Test::Unit::TestCase
184
184
  assert_equal "program: unrecognised flag '--unknown'; use --help for usage", lines_err[0]
185
185
  end
186
186
 
187
+ def test_unrecognised_flag_terse
188
+
189
+ strout = StringIO.new
190
+ strerr = StringIO.new
191
+
192
+ climate = LibCLImate::Climate.new do |cl|
193
+
194
+ cl.program_name = 'program'
195
+ cl.stdout = strout
196
+ cl.stderr = strerr
197
+ cl.exit_on_unknown = false
198
+ cl.usage_help_suffix = ''
199
+ end
200
+
201
+ argv = %w{ --unknown }
202
+
203
+ climate.run argv
204
+
205
+ lines_out = strout.string.split /\n/
206
+ lines_err = strerr.string.split /\n/
207
+
208
+ assert_equal 0, lines_out.size
209
+ assert_equal 1, lines_err.size
210
+ assert_equal "program: unrecognised flag '--unknown'", lines_err[0]
211
+ end
212
+
187
213
  def test_unrecognised_option
188
214
 
189
215
  strout = StringIO.new
190
216
  strerr = StringIO.new
191
217
 
192
- climate = LibCLImate::Climate.new do |climate|
218
+ climate = LibCLImate::Climate.new do |cl|
193
219
 
194
- climate.program_name = 'program'
195
- climate.stdout = strout
196
- climate.stderr = strerr
197
- climate.exit_on_unknown = false
220
+ cl.program_name = 'program'
221
+ cl.stdout = strout
222
+ cl.stderr = strerr
223
+ cl.exit_on_unknown = false
198
224
  end
199
225
 
200
226
  argv = %w{ --unknown=10 }
@@ -215,16 +241,16 @@ class Test_Climate_minimal < Test::Unit::TestCase
215
241
 
216
242
  is_verbose = false
217
243
 
218
- climate = LibCLImate::Climate.new do |climate|
244
+ climate = LibCLImate::Climate.new do |cl|
219
245
 
220
- climate.program_name = 'program'
221
- climate.stdout = str
222
- climate.exit_on_usage = false
246
+ cl.program_name = 'program'
247
+ cl.stdout = str
248
+ cl.exit_on_usage = false
223
249
 
224
250
  bl = false#proc { is_verbose = true }
225
251
 
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 }})
252
+ cl.add_flag('--succinct', alias: '-s', help: 'operates succinctly')
253
+ cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { :handle => proc { is_verbose = true }})
228
254
  end
229
255
 
230
256
  argv = %w{ --help --verbose --succinct }
@@ -276,13 +302,13 @@ class Test_Climate_minimal < Test::Unit::TestCase
276
302
 
277
303
  is_verbose = false
278
304
 
279
- climate = LibCLImate::Climate.new do |climate|
305
+ climate = LibCLImate::Climate.new do |cl|
280
306
 
281
- climate.program_name = 'program'
282
- climate.stdout = str
283
- climate.exit_on_usage = false
307
+ cl.program_name = 'program'
308
+ cl.stdout = str
309
+ cl.exit_on_usage = false
284
310
 
285
- climate.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
311
+ cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
286
312
  end
287
313
 
288
314
  argv = %w{ --verbose }
@@ -321,13 +347,13 @@ class Test_Climate_minimal < Test::Unit::TestCase
321
347
 
322
348
  verbosity = 1
323
349
 
324
- climate = LibCLImate::Climate.new do |climate|
350
+ climate = LibCLImate::Climate.new do |cl|
325
351
 
326
- climate.program_name = 'program'
327
- climate.stdout = str
328
- climate.exit_on_usage = false
352
+ cl.program_name = 'program'
353
+ cl.stdout = str
354
+ cl.exit_on_usage = false
329
355
 
330
- climate.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
356
+ cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
331
357
  end
332
358
 
333
359
  argv = %w{ -v 2 }
@@ -364,11 +390,11 @@ class Test_Climate_minimal < Test::Unit::TestCase
364
390
 
365
391
  str = StringIO.new
366
392
 
367
- climate = LibCLImate::Climate.new do |climate|
393
+ climate = LibCLImate::Climate.new do |cl|
368
394
 
369
- climate.program_name = 'program'
395
+ cl.program_name = 'program'
370
396
 
371
- climate.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true)
397
+ cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true)
372
398
  end
373
399
 
374
400
  assert climate.specifications[2].required?
@@ -405,15 +431,15 @@ class Test_Climate_minimal < Test::Unit::TestCase
405
431
  str = StringIO.new
406
432
  stre = StringIO.new
407
433
 
408
- climate = LibCLImate::Climate.new do |climate|
434
+ climate = LibCLImate::Climate.new do |cl|
409
435
 
410
- climate.program_name = 'program'
411
- climate.stdout = str
412
- climate.stderr = stre
413
- climate.exit_on_usage = false
414
- climate.exit_on_missing = false
436
+ cl.program_name = 'program'
437
+ cl.stdout = str
438
+ cl.stderr = stre
439
+ cl.exit_on_usage = false
440
+ cl.exit_on_missing = false
415
441
 
416
- climate.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true)
442
+ cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true)
417
443
  end
418
444
 
419
445
  r = climate.run %w{ }
@@ -455,15 +481,15 @@ class Test_Climate_minimal < Test::Unit::TestCase
455
481
  str = StringIO.new
456
482
  stre = StringIO.new
457
483
 
458
- climate = LibCLImate::Climate.new do |climate|
484
+ climate = LibCLImate::Climate.new do |cl|
459
485
 
460
- climate.program_name = 'program'
461
- climate.stdout = str
462
- climate.stderr = stre
463
- climate.exit_on_usage = false
464
- climate.exit_on_missing = false
486
+ cl.program_name = 'program'
487
+ cl.stdout = str
488
+ cl.stderr = stre
489
+ cl.exit_on_usage = false
490
+ cl.exit_on_missing = false
465
491
 
466
- climate.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true, required_message: 'Verbosity not specified')
492
+ cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true, required_message: 'Verbosity not specified')
467
493
  end
468
494
 
469
495
  r = climate.run %w{ }
@@ -505,15 +531,15 @@ class Test_Climate_minimal < Test::Unit::TestCase
505
531
  str = StringIO.new
506
532
  stre = StringIO.new
507
533
 
508
- climate = LibCLImate::Climate.new do |climate|
534
+ climate = LibCLImate::Climate.new do |cl|
509
535
 
510
- climate.program_name = 'program'
511
- climate.stdout = str
512
- climate.stderr = stre
513
- climate.exit_on_usage = false
514
- climate.exit_on_missing = false
536
+ cl.program_name = 'program'
537
+ cl.stdout = str
538
+ cl.stderr = stre
539
+ cl.exit_on_usage = false
540
+ cl.exit_on_missing = false
515
541
 
516
- climate.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true, required_message: "\0Verbosity")
542
+ cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', required: true, required_message: "\0Verbosity")
517
543
  end
518
544
 
519
545
  r = climate.run %w{ }
@@ -17,7 +17,7 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
17
17
 
18
18
  def test_no_arguments_no_mods
19
19
 
20
- climate = LibCLImate::Climate.new do |climate|
20
+ climate = LibCLImate::Climate.new do |cl|
21
21
 
22
22
  ;
23
23
  end
@@ -28,10 +28,10 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
28
28
 
29
29
  def test_no_arguments_set_streams
30
30
 
31
- climate = LibCLImate::Climate.new do |climate|
31
+ climate = LibCLImate::Climate.new do |cl|
32
32
 
33
- climate.stdout = $stdout
34
- climate.stderr = $stderr
33
+ cl.stdout = $stdout
34
+ cl.stderr = $stderr
35
35
  end
36
36
 
37
37
  assert $stdout.equal? climate.stdout
@@ -42,11 +42,11 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
42
42
 
43
43
  str = StringIO.new
44
44
 
45
- climate = LibCLImate::Climate.new do |climate|
45
+ climate = LibCLImate::Climate.new do |cl|
46
46
 
47
- climate.program_name = 'program'
48
- climate.stdout = str
49
- climate.exit_on_usage = false
47
+ cl.program_name = 'program'
48
+ cl.stdout = str
49
+ cl.exit_on_usage = false
50
50
  end
51
51
 
52
52
  argv = %w{ --help }
@@ -89,12 +89,12 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
89
89
 
90
90
  str = StringIO.new
91
91
 
92
- climate = LibCLImate::Climate.new do |climate|
92
+ climate = LibCLImate::Climate.new do |cl|
93
93
 
94
- climate.program_name = 'program'
95
- climate.info_lines = 'Synesis Software Open Source'
96
- climate.stdout = str
97
- climate.exit_on_usage = false
94
+ cl.program_name = 'program'
95
+ cl.info_lines = 'Synesis Software Open Source'
96
+ cl.stdout = str
97
+ cl.exit_on_usage = false
98
98
  end
99
99
 
100
100
  argv = %w{ --help }
@@ -139,12 +139,12 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
139
139
 
140
140
  str = StringIO.new
141
141
 
142
- climate = LibCLImate::Climate.new do |climate|
142
+ climate = LibCLImate::Climate.new do |cl|
143
143
 
144
- climate.program_name = 'program'
145
- climate.version = [ 1, 2, 3, 4 ]
146
- climate.stdout = str
147
- climate.exit_on_usage = false
144
+ cl.program_name = 'program'
145
+ cl.version = [ 1, 2, 3, 4 ]
146
+ cl.stdout = str
147
+ cl.exit_on_usage = false
148
148
  end
149
149
 
150
150
  argv = %w{ --version }
@@ -164,12 +164,12 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
164
164
  strout = StringIO.new
165
165
  strerr = StringIO.new
166
166
 
167
- climate = LibCLImate::Climate.new do |climate|
167
+ climate = LibCLImate::Climate.new do |cl|
168
168
 
169
- climate.program_name = 'program'
170
- climate.stdout = strout
171
- climate.stderr = strerr
172
- climate.exit_on_unknown = false
169
+ cl.program_name = 'program'
170
+ cl.stdout = strout
171
+ cl.stderr = strerr
172
+ cl.exit_on_unknown = false
173
173
  end
174
174
 
175
175
  argv = %w{ --unknown }
@@ -189,12 +189,12 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
189
189
  strout = StringIO.new
190
190
  strerr = StringIO.new
191
191
 
192
- climate = LibCLImate::Climate.new do |climate|
192
+ climate = LibCLImate::Climate.new do |cl|
193
193
 
194
- climate.program_name = 'program'
195
- climate.stdout = strout
196
- climate.stderr = strerr
197
- climate.exit_on_unknown = false
194
+ cl.program_name = 'program'
195
+ cl.stdout = strout
196
+ cl.stderr = strerr
197
+ cl.exit_on_unknown = false
198
198
  end
199
199
 
200
200
  argv = %w{ --unknown=10 }
@@ -215,16 +215,14 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
215
215
 
216
216
  is_verbose = false
217
217
 
218
- climate = LibCLImate::Climate.new do |climate|
218
+ climate = LibCLImate::Climate.new do |cl|
219
219
 
220
- climate.program_name = 'program'
221
- climate.stdout = str
222
- climate.exit_on_usage = false
220
+ cl.program_name = 'program'
221
+ cl.stdout = str
222
+ cl.exit_on_usage = false
223
223
 
224
- bl = false#proc { is_verbose = true }
225
-
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 }})
224
+ cl.add_flag('--succinct', alias: '-s', help: 'operates succinctly')
225
+ cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { :handle => proc { is_verbose = true }})
228
226
  end
229
227
 
230
228
  argv = %w{ --help --verbose --succinct }
@@ -275,13 +273,13 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
275
273
 
276
274
  is_verbose = false
277
275
 
278
- climate = LibCLImate::Climate.new do |climate|
276
+ climate = LibCLImate::Climate.new do |cl|
279
277
 
280
- climate.program_name = 'program'
281
- climate.stdout = str
282
- climate.exit_on_usage = false
278
+ cl.program_name = 'program'
279
+ cl.stdout = str
280
+ cl.exit_on_usage = false
283
281
 
284
- climate.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
282
+ cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
285
283
  end
286
284
 
287
285
  argv = %w{ --verbose }
@@ -319,13 +317,13 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
319
317
 
320
318
  verbosity = 1
321
319
 
322
- climate = LibCLImate::Climate.new do |climate|
320
+ climate = LibCLImate::Climate.new do |cl|
323
321
 
324
- climate.program_name = 'program'
325
- climate.stdout = str
326
- climate.exit_on_usage = false
322
+ cl.program_name = 'program'
323
+ cl.stdout = str
324
+ cl.exit_on_usage = false
327
325
 
328
- climate.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
326
+ cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
329
327
  end
330
328
 
331
329
  argv = %w{ -v 2 }
@@ -19,11 +19,11 @@ class Test_Climate_minimal < Test::Unit::TestCase
19
19
 
20
20
  options = {}
21
21
 
22
- climate = LibCLImate::Climate.new do |climate|
22
+ climate = LibCLImate::Climate.new do |cl|
23
23
 
24
- climate.add_option('--action', alias: '-a') { |o, a| options[:action] = o.value }
25
- climate.add_alias('--action=list', '-l')
26
- climate.add_alias('--action=change', '-c')
24
+ cl.add_option('--action', alias: '-a') { |o, a| options[:action] = o.value }
25
+ cl.add_alias('--action=list', '-l')
26
+ cl.add_alias('--action=change', '-c')
27
27
  end
28
28
 
29
29
  # invoke via option
@@ -19,21 +19,21 @@ class Test_Climate_values_constraints < Test::Unit::TestCase
19
19
 
20
20
  climate = LibCLImate::Climate.new do |cl|
21
21
 
22
- cl.exit_on_unknown = false
22
+ cl.exit_on_missing = false
23
23
 
24
24
  cl.stdout = stdout
25
25
  cl.stderr = stderr
26
26
  end
27
27
 
28
- stdout.truncate(0)
29
- stderr.truncate(0)
28
+ stdout.string = ''
29
+ stderr.string = ''
30
30
  r = climate.run [ ]
31
31
  assert_equal 0, r.values.size
32
32
  assert_empty stdout.string
33
33
  assert_empty stderr.string
34
34
 
35
- stdout.truncate(0)
36
- stderr.truncate(0)
35
+ stdout.string = ''
36
+ stderr.string = ''
37
37
  r = climate.run [ 'value-1' ]
38
38
  assert_equal 1, r.values.size
39
39
  assert_empty stdout.string
@@ -47,7 +47,7 @@ class Test_Climate_values_constraints < Test::Unit::TestCase
47
47
 
48
48
  climate = LibCLImate::Climate.new do |cl|
49
49
 
50
- cl.exit_on_unknown = false
50
+ cl.exit_on_missing = false
51
51
 
52
52
  cl.stdout = stdout
53
53
  cl.stderr = stderr
@@ -55,31 +55,31 @@ class Test_Climate_values_constraints < Test::Unit::TestCase
55
55
  cl.constrain_values = 2
56
56
  end
57
57
 
58
- stdout.truncate(0)
59
- stderr.truncate(0)
58
+ stdout.string = ''
59
+ stderr.string = ''
60
60
  r = climate.run [ ]
61
61
  assert_equal 0, r.values.size
62
62
  assert_empty stdout.string
63
63
  assert_not_empty stderr.string
64
64
  assert_match /wrong number of values.*0 given.*2 required.*/, stderr.string
65
65
 
66
- stdout.truncate(0)
67
- stderr.truncate(0)
66
+ stdout.string = ''
67
+ stderr.string = ''
68
68
  r = climate.run [ 'value-1' ]
69
69
  assert_equal 1, r.values.size
70
70
  assert_empty stdout.string
71
71
  assert_not_empty stderr.string
72
72
  assert_match /wrong number of values.*1 given.*2 required.*/, stderr.string
73
73
 
74
- stdout.truncate(0)
75
- stderr.truncate(0)
74
+ stdout.string = ''
75
+ stderr.string = ''
76
76
  r = climate.run [ 'value-1', 'value-2' ]
77
77
  assert_equal 2, r.values.size
78
78
  assert_empty stdout.string
79
79
  assert_empty stderr.string
80
80
 
81
- stdout.truncate(0)
82
- stderr.truncate(0)
81
+ stdout.string = ''
82
+ stderr.string = ''
83
83
  r = climate.run [ 'value-1', 'value-2', 'value-3' ]
84
84
  assert_equal 3, r.values.size
85
85
  assert_empty stdout.string
@@ -94,7 +94,7 @@ class Test_Climate_values_constraints < Test::Unit::TestCase
94
94
 
95
95
  climate = LibCLImate::Climate.new do |cl|
96
96
 
97
- cl.exit_on_unknown = false
97
+ cl.exit_on_missing = false
98
98
 
99
99
  cl.stdout = stdout
100
100
  cl.stderr = stderr
@@ -102,40 +102,96 @@ class Test_Climate_values_constraints < Test::Unit::TestCase
102
102
  cl.constrain_values = 0..2
103
103
  end
104
104
 
105
- stdout.truncate(0)
106
- stderr.truncate(0)
105
+ stdout.string = ''
106
+ stderr.string = ''
107
107
  r = climate.run [ ]
108
108
  assert_equal 0, r.values.size
109
109
  assert_empty stdout.string
110
110
  assert_empty stderr.string
111
111
 
112
- stdout.truncate(0)
113
- stderr.truncate(0)
112
+ stdout.string = ''
113
+ stderr.string = ''
114
114
  r = climate.run [ 'value-1' ]
115
115
  assert_equal 1, r.values.size
116
116
  assert_empty stdout.string
117
117
  assert_empty stderr.string
118
118
 
119
- stdout.truncate(0)
120
- stderr.truncate(0)
119
+ stdout.string = ''
120
+ stderr.string = ''
121
121
  r = climate.run [ 'value-1', 'value-2' ]
122
122
  assert_equal 2, r.values.size
123
123
  assert_empty stdout.string
124
124
  assert_empty stderr.string
125
125
 
126
- stdout.truncate(0)
127
- stderr.truncate(0)
126
+ stdout.string = ''
127
+ stderr.string = ''
128
128
  r = climate.run [ 'value-1', 'value-2', 'value-3' ]
129
129
  assert_equal 3, r.values.size
130
130
  assert_empty stdout.string
131
131
  assert_not_empty stderr.string
132
132
 
133
- stdout.truncate(0)
134
- stderr.truncate(0)
133
+ stdout.string = ''
134
+ stderr.string = ''
135
135
  r = climate.run [ 'value-1', 'value-2', 'value-3', 'value-4' ]
136
136
  assert_equal 4, r.values.size
137
137
  assert_empty stdout.string
138
138
  assert_not_empty stderr.string
139
139
  end
140
+
141
+ def test_constrain_with_simple_array
142
+
143
+ stdout = StringIO.new
144
+ stderr = StringIO.new
145
+
146
+ climate = LibCLImate::Climate.new do |cl|
147
+
148
+ cl.exit_on_missing = false
149
+
150
+ cl.stdout = stdout
151
+ cl.stderr = stderr
152
+
153
+ cl.constrain_values = [ 1, 3 ]
154
+
155
+ cl.program_name = "myprog"
156
+ cl.usage_help_suffix = ''
157
+ end
158
+
159
+ stdout.string = ''
160
+ stderr.string = ''
161
+ r = climate.run [ ]
162
+ assert_equal 0, r.values.size
163
+ assert_empty stdout.string
164
+ assert_not_empty stderr.string
165
+
166
+ stdout.string = ''
167
+ stderr.string = ''
168
+ r = climate.run [ 'value-1' ]
169
+ assert_equal 1, r.values.size
170
+ assert_empty stdout.string
171
+ assert_empty stderr.string
172
+
173
+ stdout.string = ''
174
+ stderr.string = ''
175
+ r = climate.run [ 'value-1', 'value-2' ]
176
+ assert_equal 2, r.values.size
177
+ assert_empty stdout.string
178
+ assert_not_empty stderr.string
179
+
180
+ stdout.string = ''
181
+ stderr.string = ''
182
+ r = climate.run [ 'value-1', 'value-2', 'value-3' ]
183
+ assert_equal 3, r.values.size
184
+ assert_empty stdout.string
185
+ assert_empty stderr.string
186
+
187
+ stdout.string = ''
188
+ stderr.string = ''
189
+ assert_equal '', stderr.string
190
+ r = climate.run [ 'value-1', 'value-2', 'value-3', 'value-4' ]
191
+ assert_equal 4, r.values.size
192
+ assert_empty stdout.string
193
+ assert_not_empty stderr.string
194
+ assert_equal "myprog: wrong number of values: 4 given, [1, 3] required\n", stderr.string
195
+ end
140
196
  end
141
197
 
@@ -19,9 +19,9 @@ class Test_Climate_with_blocks < Test::Unit::TestCase
19
19
 
20
20
  is_verbose = false
21
21
 
22
- climate = LibCLImate::Climate.new do |climate|
22
+ climate = LibCLImate::Climate.new do |cl|
23
23
 
24
- climate.add_flag('--verbose') { is_verbose = true }
24
+ cl.add_flag('--verbose') { is_verbose = true }
25
25
  end
26
26
 
27
27
  argv = %w{ --verbose }
@@ -35,9 +35,9 @@ class Test_Climate_with_blocks < Test::Unit::TestCase
35
35
 
36
36
  flavour = nil
37
37
 
38
- climate = LibCLImate::Climate.new do |climate|
38
+ climate = LibCLImate::Climate.new do |cl|
39
39
 
40
- climate.add_option('--flavour') { |o| flavour = o.value }
40
+ cl.add_option('--flavour') { |o| flavour = o.value }
41
41
  end
42
42
 
43
43
  argv = %w{ --flavour=blueberry }
@@ -19,9 +19,9 @@ class Test_Climate_with_blocks_CLASP < Test::Unit::TestCase
19
19
 
20
20
  is_verbose = false
21
21
 
22
- climate = LibCLImate::Climate.new do |climate|
22
+ climate = LibCLImate::Climate.new do |cl|
23
23
 
24
- climate.add_flag('--verbose') { is_verbose = true }
24
+ cl.add_flag('--verbose') { is_verbose = true }
25
25
  end
26
26
 
27
27
  argv = %w{ --verbose }
@@ -35,9 +35,9 @@ class Test_Climate_with_blocks_CLASP < Test::Unit::TestCase
35
35
 
36
36
  flavour = nil
37
37
 
38
- climate = LibCLImate::Climate.new do |climate|
38
+ climate = LibCLImate::Climate.new do |cl|
39
39
 
40
- climate.add_option('--flavour') { |o| flavour = o.value }
40
+ cl.add_option('--flavour') { |o| flavour = o.value }
41
41
  end
42
42
 
43
43
  argv = %w{ --flavour=blueberry }
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.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-13 00:00:00.000000000 Z
11
+ date: 2019-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clasp-ruby
@@ -46,6 +46,7 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - LICENSE
48
48
  - README.md
49
+ - examples/flag_and_option_specifications.from_DATA.md
49
50
  - examples/flag_and_option_specifications.from_DATA.rb
50
51
  - examples/flag_and_option_specifications.md
51
52
  - examples/flag_and_option_specifications.rb