clasp-ruby 0.23.0.1 → 0.23.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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +63 -52
  3. data/examples/cr-example.rb +16 -16
  4. data/examples/flag_and_option_specifications.md +25 -25
  5. data/examples/flag_and_option_specifications.rb +15 -15
  6. data/examples/show_usage_and_version.md +5 -5
  7. data/examples/show_usage_and_version.rb +10 -11
  8. data/examples/simple_command_line_no_specifications.rb +1 -1
  9. data/lib/clasp/arguments.rb +543 -543
  10. data/lib/clasp/clasp.rb +15 -11
  11. data/lib/clasp/cli.rb +145 -139
  12. data/lib/clasp/doc_.rb +3 -3
  13. data/lib/clasp/old_module.rb +9 -9
  14. data/lib/clasp/specifications.rb +346 -339
  15. data/lib/clasp/util/exceptions.rb +22 -23
  16. data/lib/clasp/util/value_parser.rb +101 -103
  17. data/lib/clasp/version.rb +20 -20
  18. data/lib/clasp-ruby.rb +9 -7
  19. data/lib/clasp.rb +9 -7
  20. data/test/scratch/test_list_command_line.rb +6 -6
  21. data/test/scratch/test_specifications.rb +14 -14
  22. data/test/scratch/test_usage.rb +6 -6
  23. data/test/scratch/test_usage_from_DATA.rb +1 -1
  24. data/test/scratch/test_usage_with_duplicate_specifications.rb +6 -6
  25. data/test/unit/tc_ARGV_rewrite.rb +36 -38
  26. data/test/unit/tc_arguments_1.rb +694 -694
  27. data/test/unit/tc_arguments_2.rb +52 -53
  28. data/test/unit/tc_arguments_3.rb +77 -77
  29. data/test/unit/tc_arguments_inspect.rb +55 -56
  30. data/test/unit/tc_cli.rb +4 -4
  31. data/test/unit/tc_default_value.rb +91 -91
  32. data/test/unit/tc_defaults_1.rb +38 -38
  33. data/test/unit/tc_examples_Arguments.rb +130 -132
  34. data/test/unit/tc_extras.rb +24 -26
  35. data/test/unit/tc_option_required.rb +38 -39
  36. data/test/unit/tc_option_value_aliases.rb +45 -45
  37. data/test/unit/tc_specifications.rb +7 -8
  38. data/test/unit/tc_typed_options.rb +204 -204
  39. data/test/unit/tc_usage.rb +112 -55
  40. data/test/unit/tc_with_action.rb +23 -24
  41. data/test/unit/ts_all.rb +1 -1
  42. metadata +12 -10
@@ -1,16 +1,17 @@
1
1
 
2
2
  # ######################################################################## #
3
- # File: clasp/specifications.rb
3
+ # File: clasp/specifications.rb
4
4
  #
5
- # Purpose: Argument specification classes
5
+ # Purpose: Argument specification classes
6
6
  #
7
- # Created: 25th October 2014
8
- # Updated: 29th April 2019
7
+ # Created: 25th October 2014
8
+ # Updated: 6th March 2025
9
9
  #
10
- # Home: http://github.com/synesissoftware/CLASP.Ruby
10
+ # Home: http://github.com/synesissoftware/CLASP.Ruby
11
11
  #
12
- # Author: Matthew Wilson
12
+ # Author: Matthew Wilson
13
13
  #
14
+ # Copyright (c) 2019-2025, Matthew Wilson and Synesis Information Systems
14
15
  # Copyright (c) 2014-2019, Matthew Wilson and Synesis Software
15
16
  # All rights reserved.
16
17
  #
@@ -54,327 +55,329 @@
54
55
 
55
56
  module CLASP
56
57
 
58
+
57
59
  # ######################################################################## #
58
60
  # classes
59
61
 
60
62
  # @!visibility private
61
63
  class SpecificationBase # :nodoc: all
62
64
 
63
- private
64
- # @!visibility private
65
- def check_arity_(blk, range, label) # :nodoc:
65
+ private
66
+ # @!visibility private
67
+ def check_arity_(blk, range, label) # :nodoc:
66
68
 
67
- raise ArgumentError, "block must be a #{Proc}; #{blk.class} given" unless blk.nil? || Proc === blk
69
+ raise ArgumentError, "block must be a #{Proc}; #{blk.class} given" unless blk.nil? || Proc === blk
68
70
 
69
- if blk
71
+ if blk
70
72
 
71
- case blk.arity
72
- when range
73
+ case blk.arity
74
+ when range
73
75
 
74
- ;
75
- else
76
+ ;
77
+ else
76
78
 
77
- msg = "wrong arity for #{label}"
79
+ msg = "wrong arity for #{label}"
78
80
 
79
- if $DEBUG
81
+ if $DEBUG
80
82
 
81
- raise ArgumentError, msg
82
- else
83
+ raise ArgumentError, msg
84
+ else
83
85
 
84
- warn msg
85
- end
86
- end
87
- end
88
- end
89
- public
86
+ warn msg
87
+ end
88
+ end
89
+ end
90
+ end
91
+ public
90
92
  end
91
93
 
92
94
  # A class that represents the specification for a command-line flag
93
95
  class FlagSpecification < SpecificationBase
94
96
 
95
- # Creates a FlagSpecification instance from the given name, aliases, and help
96
- #
97
- # === Signature
98
- #
99
- # * *Parameters*
100
- # - +name+ (+String+) The name, or long-form, of the flag
101
- # - +aliases+ (+Array+) 0 or more strings specifying short-form or option-value aliases
102
- # - +help+ (+String+) The help string, which may be +nil+
103
- # - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+
104
- #
105
- # * *Block* An optional block that is called when a matching flag argument is found
106
- #
107
- # *NOTE:* Users should prefer the +CLASP::Flag()+ method
108
- def initialize(name, aliases, help, extras = nil, &blk)
97
+ # Creates a FlagSpecification instance from the given name, aliases, and help
98
+ #
99
+ # === Signature
100
+ #
101
+ # * *Parameters*
102
+ # - +name+ (+String+) The name, or long-form, of the flag
103
+ # - +aliases+ (+Array+) 0 or more strings specifying short-form or option-value aliases
104
+ # - +help+ (+String+) The help string, which may be +nil+
105
+ # - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+
106
+ #
107
+ # * *Block* An optional block that is called when a matching flag argument is found
108
+ #
109
+ # *NOTE:* Users should prefer the +CLASP::Flag()+ method
110
+ def initialize(name, aliases, help, extras = nil, &blk)
109
111
 
110
- check_arity_(blk, 0..3, "flag")
112
+ check_arity_(blk, 0..3, "flag")
111
113
 
112
- @name = name
113
- @aliases = (aliases || []).select { |a| a and not a.empty? }
114
- @help = help
115
- @extras = extras || {}
116
- @action = blk
117
- end
114
+ @name = name
115
+ @aliases = (aliases || []).select { |a| a and not a.empty? }
116
+ @help = help
117
+ @extras = extras || {}
118
+ @action = blk
119
+ end
118
120
 
119
- # The flag's name string
120
- attr_reader :name
121
- # The flag's aliases array
122
- attr_reader :aliases
123
- # The flag's help string
124
- attr_reader :help
125
- # The flag's extras
126
- attr_reader :extras
121
+ # The flag's name string
122
+ attr_reader :name
123
+ # The flag's aliases array
124
+ attr_reader :aliases
125
+ # The flag's help string
126
+ attr_reader :help
127
+ # The flag's extras
128
+ attr_reader :extras
127
129
 
128
- # (Proc) The procedure
129
- attr_reader :action
130
+ # (Proc) The procedure
131
+ attr_reader :action
130
132
 
131
- # @!visibility private
132
- def action=(blk) # :nodoc: all
133
+ # @!visibility private
134
+ def action=(blk) # :nodoc: all
133
135
 
134
- check_arity_(blk, 0..3, "flag")
136
+ check_arity_(blk, 0..3, "flag")
135
137
 
136
- @action = blk
137
- end
138
+ @action = blk
139
+ end
138
140
 
139
- # String form of the flag
140
- def to_s
141
+ # String form of the flag
142
+ def to_s
141
143
 
142
- "{#{name}; aliases=#{aliases.join(', ')}; help='#{help}'; extras=#{extras}}"
143
- end
144
+ "{#{name}; aliases=#{aliases.join(', ')}; help='#{help}'; extras=#{extras}}"
145
+ end
144
146
 
145
- # @!visibility private
146
- def eql? rhs # :nodoc:
147
+ # @!visibility private
148
+ def eql? rhs # :nodoc:
147
149
 
148
- case rhs
149
- when self.class
150
+ case rhs
151
+ when self.class
150
152
 
151
- return true if equal?(rhs)
152
- else
153
+ return true if equal?(rhs)
154
+ else
153
155
 
154
- return false
155
- end
156
+ return false
157
+ end
156
158
 
157
- return false unless name == rhs.name
158
- return false unless aliases == rhs.aliases
159
- return false unless help == rhs.help
160
- return false unless extras == rhs.extras
159
+ return false unless name == rhs.name
160
+ return false unless aliases == rhs.aliases
161
+ return false unless help == rhs.help
162
+ return false unless extras == rhs.extras
161
163
 
162
- return true
163
- end
164
+ return true
165
+ end
164
166
 
165
- # Compares instance against another FlagSpecification or against a name (String)
166
- def == rhs
167
+ # Compares instance against another FlagSpecification or against a name (String)
168
+ def == rhs
167
169
 
168
- case rhs
169
- when self.class
170
+ case rhs
171
+ when self.class
170
172
 
171
- return self.eql? rhs
172
- when String
173
+ return self.eql? rhs
174
+ when String
173
175
 
174
- return name == rhs
175
- else
176
+ return name == rhs
177
+ else
176
178
 
177
- false
178
- end
179
- end
179
+ false
180
+ end
181
+ end
180
182
 
181
- private
182
- @@Help_ = self.new('--help', [], 'shows this help and terminates')
183
- @@Version_ = self.new('--version', [], 'shows version and terminates')
184
- public
185
- # An instance of FlagSpecification that provides default '--help' information
186
- #
187
- # If you wish to specify +extras+ or attach a block, you may do so
188
- def self.Help(extras = nil, &blk)
183
+ private
184
+ @@Help_ = self.new('--help', [], 'shows this help and terminates')
185
+ @@Version_ = self.new('--version', [], 'shows version and terminates')
186
+ public
187
+ # An instance of FlagSpecification that provides default '--help' information
188
+ #
189
+ # If you wish to specify +extras+ or attach a block, you may do so
190
+ def self.Help(extras = nil, &blk)
189
191
 
190
- h = @@Help_
192
+ h = @@Help_
191
193
 
192
- if extras || blk
194
+ if extras || blk
193
195
 
194
- return self.new(h.name, h.aliases, h.help, extras, &blk)
195
- end
196
+ return self.new(h.name, h.aliases, h.help, extras, &blk)
197
+ end
196
198
 
197
- h
198
- end
199
+ h
200
+ end
199
201
 
200
- # An instance of FlagSpecification that provides default '--version' information
201
- #
202
- # If you wish to specify +extras+ or attach a block, you may do so
203
- def self.Version(extras = nil, &blk)
202
+ # An instance of FlagSpecification that provides default '--version' information
203
+ #
204
+ # If you wish to specify +extras+ or attach a block, you may do so
205
+ def self.Version(extras = nil, &blk)
204
206
 
205
- h = @@Version_
207
+ h = @@Version_
206
208
 
207
- if extras || blk
209
+ if extras || blk
208
210
 
209
- return self.new(h.name, h.aliases, h.help, extras, &blk)
210
- end
211
+ return self.new(h.name, h.aliases, h.help, extras, &blk)
212
+ end
211
213
 
212
- h
213
- end
214
+ h
215
+ end
214
216
  end
215
217
 
216
218
  # A class that represents the specification for a command-line option
217
219
  class OptionSpecification < SpecificationBase
218
220
 
219
- # Creates an OptionSpecification instance from the given name, aliases, help,
220
- # values_range, and default_value
221
- #
222
- # === Signature
223
- #
224
- # * *Parameters*
225
- # - +name+ (+String+) The name, or long-form, of the option
226
- # - +aliases+ (+Array+) 0 or more strings specifying short-form or option-value aliases
227
- # - +help+ (+String+) The help string, which may be +nil+
228
- # - +values_range+ (+Array+) 0 or more strings specifying values supported by the option
229
- # - +default_value+ (+String+) The default value of the option, which will be used in the case where an option is specified without a value. May be +nil+
230
- # - +required+ (boolean) Whether the option is required. May be +nil+
231
- # - +required_message+ (::String) Message to be used when reporting that a required option is missing. May be +nil+ in which case a message of the form "<option-name> not specified; use --help for usage". If begins with the nul character ("\0"), then is used in the place of the <option-name> and placed into the rest of the standard form message
232
- # - +constraint+ (Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version
233
- # - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+
234
- #
235
- # * *Block* An optional block that is called when a matching option argument is found
236
- #
237
- # *NOTE:* Users should prefer the +CLASP::Option()+ method
238
- def initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil, &blk)
239
-
240
- check_arity_(blk, 0..3, "option")
241
-
242
- @name = name
243
- @aliases = (aliases || []).select { |a| a and not a.empty? }
244
- @help = help
245
- @values_range = values_range || []
246
- @default_value = default_value
247
- @required = required
248
- @required_message = nil
249
- @constraint = constraint || {}
250
- @extras = extras || {}
251
- @action = blk
252
-
253
- rm_name = nil
254
-
255
- if required_message
256
-
257
- if "\0" == required_message[0]
258
-
259
- rm_name = required_message[1..-1]
260
- end
261
- else
262
-
263
- rm_name = "'#{name}'"
264
- end
265
-
266
- if rm_name
267
-
268
- required_message = "#{rm_name} not specified; use --help for usage"
269
- end
270
-
271
- @required_message = required_message
272
- end
273
-
274
- # The option's name string
275
- attr_reader :name
276
- # The option's aliases array
277
- attr_reader :aliases
278
- # The option's help string
279
- attr_reader :help
280
- # The range of values supported by the option
281
- attr_reader :values_range
282
- # The default value of the option
283
- attr_reader :default_value
284
- # Indicates whether the option is required
285
- def required?; @required; end
286
- # The message to be used when reporting that a required option is missing
287
- attr_reader :required_message
288
- # The value constraint
289
- attr_reader :constraint
290
- # The option's extras
291
- attr_reader :extras
292
-
293
- # (Proc) The procedure
294
- attr_reader :action
295
-
296
- # @!visibility private
297
- def action=(blk) # :nodoc: all
298
-
299
- check_arity_(blk, 0..3, "flag")
300
-
301
- @action = blk
302
- end
303
-
304
- # String form of the option
305
- def to_s
306
-
307
- "{#{name}; aliases=#{aliases.join(', ')}; values_range=[ #{values_range.join(', ')} ]; default_value='#{default_value}'; help='#{help}'; required?=#{required?}; required_message=#{required_message}; constraint=#{constraint}; extras=#{extras}}"
308
- end
309
-
310
- # @!visibility private
311
- def eql? rhs # :nodoc:
312
-
313
- case rhs
314
- when self.class
315
-
316
- return true if equal?(rhs)
317
- else
318
-
319
- return false
320
- end
321
-
322
- return false unless name == rhs.name
323
- return false unless aliases == rhs.aliases
324
- return false unless help == rhs.help
325
- return false unless values_range == rhs.values_range
326
- return false unless default_value == rhs.default_value
327
- return false unless required? == rhs.required?
328
- return false unless required_message == rhs.required_message
329
- return false unless extras == rhs.extras
330
-
331
- return true
332
- end
333
-
334
- # Compares instance against another OptionSpecification or against a name (String)
335
- def == rhs
221
+ # Creates an OptionSpecification instance from the given name, aliases, help,
222
+ # values_range, and default_value
223
+ #
224
+ # === Signature
225
+ #
226
+ # * *Parameters*
227
+ # - +name+ (+String+) The name, or long-form, of the option
228
+ # - +aliases+ (+Array+) 0 or more strings specifying short-form or option-value aliases
229
+ # - +help+ (+String+) The help string, which may be +nil+
230
+ # - +values_range+ (+Array+) 0 or more strings specifying values supported by the option
231
+ # - +default_value+ (+String+) The default value of the option, which will be used in the case where an option is specified without a value. May be +nil+
232
+ # - +required+ (boolean) Whether the option is required. May be +nil+
233
+ # - +required_message+ (::String) Message to be used when reporting that a required option is missing. May be +nil+ in which case a message of the form "<option-name> not specified; use --help for usage". If begins with the nul character ("\0"), then is used in the place of the <option-name> and placed into the rest of the standard form message
234
+ # - +constraint+ (Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version
235
+ # - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+
236
+ #
237
+ # * *Block* An optional block that is called when a matching option argument is found
238
+ #
239
+ # *NOTE:* Users should prefer the +CLASP::Option()+ method
240
+ def initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil, &blk)
241
+
242
+ check_arity_(blk, 0..3, "option")
243
+
244
+ @name = name
245
+ @aliases = (aliases || []).select { |a| a and not a.empty? }
246
+ @help = help
247
+ @values_range = values_range || []
248
+ @default_value = default_value
249
+ @required = required
250
+ @required_message = nil
251
+ @constraint = constraint || {}
252
+ @extras = extras || {}
253
+ @action = blk
254
+
255
+ rm_name = nil
256
+
257
+ if required_message
258
+
259
+ if "\0" == required_message[0]
260
+
261
+ rm_name = required_message[1..-1]
262
+ end
263
+ else
264
+
265
+ rm_name = "'#{name}'"
266
+ end
267
+
268
+ if rm_name
269
+
270
+ required_message = "#{rm_name} not specified; use --help for usage"
271
+ end
272
+
273
+ @required_message = required_message
274
+ end
275
+
276
+ # The option's name string
277
+ attr_reader :name
278
+ # The option's aliases array
279
+ attr_reader :aliases
280
+ # The option's help string
281
+ attr_reader :help
282
+ # The range of values supported by the option
283
+ attr_reader :values_range
284
+ # The default value of the option
285
+ attr_reader :default_value
286
+ # Indicates whether the option is required
287
+ def required?; @required; end
288
+ # The message to be used when reporting that a required option is missing
289
+ attr_reader :required_message
290
+ # The value constraint
291
+ attr_reader :constraint
292
+ # The option's extras
293
+ attr_reader :extras
294
+
295
+ # (Proc) The procedure
296
+ attr_reader :action
297
+
298
+ # @!visibility private
299
+ def action=(blk) # :nodoc: all
300
+
301
+ check_arity_(blk, 0..3, "flag")
302
+
303
+ @action = blk
304
+ end
305
+
306
+ # String form of the option
307
+ def to_s
308
+
309
+ "{#{name}; aliases=#{aliases.join(', ')}; values_range=[ #{values_range.join(', ')} ]; default_value='#{default_value}'; help='#{help}'; required?=#{required?}; required_message=#{required_message}; constraint=#{constraint}; extras=#{extras}}"
310
+ end
311
+
312
+ # @!visibility private
313
+ def eql? rhs # :nodoc:
314
+
315
+ case rhs
316
+ when self.class
317
+
318
+ return true if equal?(rhs)
319
+ else
320
+
321
+ return false
322
+ end
323
+
324
+ return false unless name == rhs.name
325
+ return false unless aliases == rhs.aliases
326
+ return false unless help == rhs.help
327
+ return false unless values_range == rhs.values_range
328
+ return false unless default_value == rhs.default_value
329
+ return false unless required? == rhs.required?
330
+ return false unless required_message == rhs.required_message
331
+ return false unless extras == rhs.extras
332
+
333
+ return true
334
+ end
335
+
336
+ # Compares instance against another OptionSpecification or against a name (String)
337
+ def == rhs
336
338
 
337
- case rhs
338
- when self.class
339
+ case rhs
340
+ when self.class
339
341
 
340
- return self.eql? rhs
341
- when String
342
+ return self.eql? rhs
343
+ when String
342
344
 
343
- return name == rhs
344
- else
345
+ return name == rhs
346
+ else
345
347
 
346
- false
347
- end
348
- end
348
+ false
349
+ end
350
+ end
349
351
  end
350
352
 
351
353
  # A class that represents an explicit alias for a flag or an option
352
354
  class AliasSpecification
353
355
 
354
- def initialize(name, aliases)
356
+ def initialize(name, aliases)
355
357
 
356
- @name = name
357
- @aliases = (aliases || []).select { |a| a and not a.empty? }
358
- @extras = nil
359
- @help = nil
360
- end
358
+ @name = name
359
+ @aliases = (aliases || []).select { |a| a and not a.empty? }
360
+ @extras = nil
361
+ @help = nil
362
+ end
361
363
 
362
- # The alias' name string
363
- attr_reader :name
364
- # The alias' aliases array
365
- attr_reader :aliases
366
- # The flag's help string
367
- attr_reader :help
368
- # The flag's extras
369
- attr_reader :extras
364
+ # The alias' name string
365
+ attr_reader :name
366
+ # The alias' aliases array
367
+ attr_reader :aliases
368
+ # The flag's help string
369
+ attr_reader :help
370
+ # The flag's extras
371
+ attr_reader :extras
370
372
 
371
- # String form of the option
372
- def to_s
373
+ # String form of the option
374
+ def to_s
373
375
 
374
- "{#{name}; aliases=#{aliases.join(', ')}}"
375
- end
376
+ "{#{name}; aliases=#{aliases.join(', ')}}"
377
+ end
376
378
  end
377
379
 
380
+
378
381
  # ######################################################################## #
379
382
  # functions
380
383
 
@@ -396,38 +399,39 @@ end
396
399
  # * *Block* An optional block that is called when a matching flag argument is found
397
400
  def CLASP.Flag(name, options = {}, &blk)
398
401
 
399
- aliases = nil
400
- help = nil
401
- extras = nil
402
+ aliases = nil
403
+ help = nil
404
+ extras = nil
402
405
 
403
- options.each do |k, v|
406
+ options.each do |k, v|
404
407
 
405
- case k
406
- when Symbol
407
- case k
408
- when :alias
408
+ case k
409
+ when Symbol
409
410
 
410
- aliases = [ v ] if v
411
- when :aliases
411
+ case k
412
+ when :alias
412
413
 
413
- aliases = v unless aliases
414
- when :help
414
+ aliases = [ v ] if v
415
+ when :aliases
415
416
 
416
- help = v
417
- when :extras
417
+ aliases = v unless aliases
418
+ when :help
418
419
 
419
- extras = v
420
- else
420
+ help = v
421
+ when :extras
421
422
 
422
- raise ArgumentError, "invalid option for flag: '#{k}' => '#{v}'"
423
- end
424
- else
423
+ extras = v
424
+ else
425
425
 
426
- raise ArgumentError, "invalid option type for flag: '#{k}' (#{k.class}) => '#{v}'"
427
- end
428
- end
426
+ raise ArgumentError, "invalid option for flag: '#{k}' => '#{v}'"
427
+ end
428
+ else
429
429
 
430
- CLASP::FlagSpecification.new(name, aliases, help, extras, &blk)
430
+ raise ArgumentError, "invalid option type for flag: '#{k}' (#{k.class}) => '#{v}'"
431
+ end
432
+ end
433
+
434
+ CLASP::FlagSpecification.new(name, aliases, help, extras, &blk)
431
435
  end
432
436
 
433
437
  # Generator method that obtains a CLASP::OptionSpecification according to the given
@@ -446,103 +450,106 @@ end
446
450
  # - +:default+ [DEPRECATED] Alternative to +:default_value+
447
451
  # - +:extras+ An application-defined object, usually a hash of custom attributes
448
452
  # - +:help+ (::String) A help string
449
- # - +required+ (boolean) Whether the option is required. May be +nil+
450
- # - +required_message+ (::String) Message to be used when reporting that a required option is missing. May be +nil+ in which case a message of the form "<option-name> not specified; use --help for usage". If begins with the nul character ("\0"), then is used in the place of the <option-name> and placed into the rest of the standard form message
451
- # - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+.
452
- # - +constraint+ (Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version
453
+ # - +:required+ (boolean) Whether the option is required. May be +nil+
454
+ # - +:required_message+ (::String) Message to be used when reporting that a required option is missing. May be +nil+ in which case a message of the form "<option-name> not specified; use --help for usage". If begins with the nul character ("\0"), then is used in the place of the <option-name> and placed into the rest of the standard form message
455
+ # - +:extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+.
456
+ # - +:constraint+ (Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version
453
457
  # - +:values_range+ (::Array) An array defining the accepted values for the option
454
458
  # - +:values+ [DEPRECATED] Alternative to +:values_range+
455
459
  #
456
460
  # * *Block* An optional block that is called when a matching option argument is found
457
461
  def CLASP.Option(name, options = {}, &blk)
458
462
 
459
- aliases = nil
460
- help = nil
461
- values_range = nil
462
- default_value = nil
463
- required = false
464
- require_message = nil
465
- constraint = nil
466
- extras = nil
463
+ aliases = nil
464
+ help = nil
465
+ values_range = nil
466
+ default_value = nil
467
+ required = false
468
+ require_message = nil
469
+ constraint = nil
470
+ extras = nil
471
+
472
+ options.each do |k, v|
467
473
 
468
- options.each do |k, v|
474
+ case k
475
+ when Symbol
469
476
 
470
- case k
471
- when Symbol
472
- case k
473
- when :alias
477
+ case k
478
+ when :alias
474
479
 
475
- aliases = [ v ] if v
476
- when :aliases
480
+ aliases = [ v ] if v
481
+ when :aliases
477
482
 
478
- aliases = v unless aliases
479
- when :help
483
+ aliases = v unless aliases
484
+ when :help
480
485
 
481
- help = v
482
- when :values_range, :values
486
+ help = v
487
+ when :values_range, :values
483
488
 
484
- values_range = v
485
- when :default_value, :default
489
+ values_range = v
490
+ when :default_value, :default
486
491
 
487
- default_value = v
488
- when :required
492
+ default_value = v
493
+ when :required
489
494
 
490
- required = v
491
- when :required_message
495
+ required = v
496
+ when :required_message
492
497
 
493
- require_message = v
494
- when :extras
498
+ require_message = v
499
+ when :extras
495
500
 
496
- extras = v
497
- when :constraint
501
+ extras = v
502
+ when :constraint
498
503
 
499
- constraint = v
500
- else
504
+ constraint = v
505
+ else
501
506
 
502
- raise ArgumentError, "invalid option for option: '#{k}' => '#{v}'"
503
- end
504
- else
507
+ raise ArgumentError, "invalid option for option: '#{k}' => '#{v}'"
508
+ end
509
+ else
505
510
 
506
- raise ArgumentError, "invalid option type for option: '#{k}' (#{k.class}) => '#{v}'"
507
- end
508
- end
511
+ raise ArgumentError, "invalid option type for option: '#{k}' (#{k.class}) => '#{v}'"
512
+ end
513
+ end
509
514
 
510
- CLASP::OptionSpecification.new(name, aliases, help, values_range, default_value, required, require_message, constraint, extras, &blk)
515
+ CLASP::OptionSpecification.new(name, aliases, help, values_range, default_value, required, require_message, constraint, extras, &blk)
511
516
  end
512
517
 
513
518
  def CLASP.Alias(name, *args)
514
519
 
515
- options = args.pop if args[-1].is_a?(::Hash)
516
- options ||= {}
520
+ options = args.pop if args[-1].is_a?(::Hash)
521
+ options ||= {}
517
522
 
518
- if options[:alias]
523
+ if options[:alias]
519
524
 
520
- aliases = [ options[:alias] ]
521
- elsif options[:aliases]
525
+ aliases = [ options[:alias] ]
526
+ elsif options[:aliases]
522
527
 
523
- aliases = options[:aliases]
524
- else
528
+ aliases = options[:aliases]
529
+ else
525
530
 
526
- aliases = args
527
- end
531
+ aliases = args
532
+ end
528
533
 
529
- CLASP::AliasSpecification.new name, aliases
534
+ CLASP::AliasSpecification.new name, aliases
530
535
  end
531
536
 
537
+
532
538
  # ######################################################################## #
533
539
  # backwards-compatible
534
540
 
535
- Alias = AliasSpecification
536
- Flag = FlagSpecification
537
- FlagAlias = FlagSpecification
538
- Option = OptionSpecification
539
- OptionAlias = OptionSpecification
541
+ Alias = AliasSpecification
542
+ Flag = FlagSpecification
543
+ FlagAlias = FlagSpecification
544
+ Option = OptionSpecification
545
+ OptionAlias = OptionSpecification
546
+
540
547
 
541
548
  # ######################################################################## #
542
549
  # module
543
550
 
544
551
  end # module CLASP
545
552
 
546
- # ############################## end of file ############################# #
547
553
 
554
+ # ############################## end of file ############################# #
548
555