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.
- checksums.yaml +4 -4
- data/README.md +63 -52
- data/examples/cr-example.rb +16 -16
- data/examples/flag_and_option_specifications.md +25 -25
- data/examples/flag_and_option_specifications.rb +15 -15
- data/examples/show_usage_and_version.md +5 -5
- data/examples/show_usage_and_version.rb +10 -11
- data/examples/simple_command_line_no_specifications.rb +1 -1
- data/lib/clasp/arguments.rb +543 -543
- data/lib/clasp/clasp.rb +15 -11
- data/lib/clasp/cli.rb +145 -139
- data/lib/clasp/doc_.rb +3 -3
- data/lib/clasp/old_module.rb +9 -9
- data/lib/clasp/specifications.rb +346 -339
- data/lib/clasp/util/exceptions.rb +22 -23
- data/lib/clasp/util/value_parser.rb +101 -103
- data/lib/clasp/version.rb +20 -20
- data/lib/clasp-ruby.rb +9 -7
- data/lib/clasp.rb +9 -7
- data/test/scratch/test_list_command_line.rb +6 -6
- data/test/scratch/test_specifications.rb +14 -14
- data/test/scratch/test_usage.rb +6 -6
- data/test/scratch/test_usage_from_DATA.rb +1 -1
- data/test/scratch/test_usage_with_duplicate_specifications.rb +6 -6
- data/test/unit/tc_ARGV_rewrite.rb +36 -38
- data/test/unit/tc_arguments_1.rb +694 -694
- data/test/unit/tc_arguments_2.rb +52 -53
- data/test/unit/tc_arguments_3.rb +77 -77
- data/test/unit/tc_arguments_inspect.rb +55 -56
- data/test/unit/tc_cli.rb +4 -4
- data/test/unit/tc_default_value.rb +91 -91
- data/test/unit/tc_defaults_1.rb +38 -38
- data/test/unit/tc_examples_Arguments.rb +130 -132
- data/test/unit/tc_extras.rb +24 -26
- data/test/unit/tc_option_required.rb +38 -39
- data/test/unit/tc_option_value_aliases.rb +45 -45
- data/test/unit/tc_specifications.rb +7 -8
- data/test/unit/tc_typed_options.rb +204 -204
- data/test/unit/tc_usage.rb +112 -55
- data/test/unit/tc_with_action.rb +23 -24
- data/test/unit/ts_all.rb +1 -1
- metadata +12 -10
data/lib/clasp/specifications.rb
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
|
2
2
|
# ######################################################################## #
|
3
|
-
# File:
|
3
|
+
# File: clasp/specifications.rb
|
4
4
|
#
|
5
|
-
# Purpose:
|
5
|
+
# Purpose: Argument specification classes
|
6
6
|
#
|
7
|
-
# Created:
|
8
|
-
# Updated:
|
7
|
+
# Created: 25th October 2014
|
8
|
+
# Updated: 6th March 2025
|
9
9
|
#
|
10
|
-
# Home:
|
10
|
+
# Home: http://github.com/synesissoftware/CLASP.Ruby
|
11
11
|
#
|
12
|
-
# Author:
|
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
|
-
|
64
|
-
|
65
|
-
|
65
|
+
private
|
66
|
+
# @!visibility private
|
67
|
+
def check_arity_(blk, range, label) # :nodoc:
|
66
68
|
|
67
|
-
|
69
|
+
raise ArgumentError, "block must be a #{Proc}; #{blk.class} given" unless blk.nil? || Proc === blk
|
68
70
|
|
69
|
-
|
71
|
+
if blk
|
70
72
|
|
71
|
-
|
72
|
-
|
73
|
+
case blk.arity
|
74
|
+
when range
|
73
75
|
|
74
|
-
|
75
|
-
|
76
|
+
;
|
77
|
+
else
|
76
78
|
|
77
|
-
|
79
|
+
msg = "wrong arity for #{label}"
|
78
80
|
|
79
|
-
|
81
|
+
if $DEBUG
|
80
82
|
|
81
|
-
|
82
|
-
|
83
|
+
raise ArgumentError, msg
|
84
|
+
else
|
83
85
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
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
|
-
|
112
|
+
check_arity_(blk, 0..3, "flag")
|
111
113
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
-
|
129
|
-
|
130
|
+
# (Proc) The procedure
|
131
|
+
attr_reader :action
|
130
132
|
|
131
|
-
|
132
|
-
|
133
|
+
# @!visibility private
|
134
|
+
def action=(blk) # :nodoc: all
|
133
135
|
|
134
|
-
|
136
|
+
check_arity_(blk, 0..3, "flag")
|
135
137
|
|
136
|
-
|
137
|
-
|
138
|
+
@action = blk
|
139
|
+
end
|
138
140
|
|
139
|
-
|
140
|
-
|
141
|
+
# String form of the flag
|
142
|
+
def to_s
|
141
143
|
|
142
|
-
|
143
|
-
|
144
|
+
"{#{name}; aliases=#{aliases.join(', ')}; help='#{help}'; extras=#{extras}}"
|
145
|
+
end
|
144
146
|
|
145
|
-
|
146
|
-
|
147
|
+
# @!visibility private
|
148
|
+
def eql? rhs # :nodoc:
|
147
149
|
|
148
|
-
|
149
|
-
|
150
|
+
case rhs
|
151
|
+
when self.class
|
150
152
|
|
151
|
-
|
152
|
-
|
153
|
+
return true if equal?(rhs)
|
154
|
+
else
|
153
155
|
|
154
|
-
|
155
|
-
|
156
|
+
return false
|
157
|
+
end
|
156
158
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
163
|
-
|
164
|
+
return true
|
165
|
+
end
|
164
166
|
|
165
|
-
|
166
|
-
|
167
|
+
# Compares instance against another FlagSpecification or against a name (String)
|
168
|
+
def == rhs
|
167
169
|
|
168
|
-
|
169
|
-
|
170
|
+
case rhs
|
171
|
+
when self.class
|
170
172
|
|
171
|
-
|
172
|
-
|
173
|
+
return self.eql? rhs
|
174
|
+
when String
|
173
175
|
|
174
|
-
|
175
|
-
|
176
|
+
return name == rhs
|
177
|
+
else
|
176
178
|
|
177
|
-
|
178
|
-
|
179
|
-
|
179
|
+
false
|
180
|
+
end
|
181
|
+
end
|
180
182
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
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
|
-
|
192
|
+
h = @@Help_
|
191
193
|
|
192
|
-
|
194
|
+
if extras || blk
|
193
195
|
|
194
|
-
|
195
|
-
|
196
|
+
return self.new(h.name, h.aliases, h.help, extras, &blk)
|
197
|
+
end
|
196
198
|
|
197
|
-
|
198
|
-
|
199
|
+
h
|
200
|
+
end
|
199
201
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
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
|
-
|
207
|
+
h = @@Version_
|
206
208
|
|
207
|
-
|
209
|
+
if extras || blk
|
208
210
|
|
209
|
-
|
210
|
-
|
211
|
+
return self.new(h.name, h.aliases, h.help, extras, &blk)
|
212
|
+
end
|
211
213
|
|
212
|
-
|
213
|
-
|
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
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
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
|
-
|
338
|
-
|
339
|
+
case rhs
|
340
|
+
when self.class
|
339
341
|
|
340
|
-
|
341
|
-
|
342
|
+
return self.eql? rhs
|
343
|
+
when String
|
342
344
|
|
343
|
-
|
344
|
-
|
345
|
+
return name == rhs
|
346
|
+
else
|
345
347
|
|
346
|
-
|
347
|
-
|
348
|
-
|
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
|
-
|
356
|
+
def initialize(name, aliases)
|
355
357
|
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
358
|
+
@name = name
|
359
|
+
@aliases = (aliases || []).select { |a| a and not a.empty? }
|
360
|
+
@extras = nil
|
361
|
+
@help = nil
|
362
|
+
end
|
361
363
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
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
|
-
|
372
|
-
|
373
|
+
# String form of the option
|
374
|
+
def to_s
|
373
375
|
|
374
|
-
|
375
|
-
|
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
|
-
|
400
|
-
|
401
|
-
|
402
|
+
aliases = nil
|
403
|
+
help = nil
|
404
|
+
extras = nil
|
402
405
|
|
403
|
-
|
406
|
+
options.each do |k, v|
|
404
407
|
|
405
|
-
|
406
|
-
|
407
|
-
case k
|
408
|
-
when :alias
|
408
|
+
case k
|
409
|
+
when Symbol
|
409
410
|
|
410
|
-
|
411
|
-
|
411
|
+
case k
|
412
|
+
when :alias
|
412
413
|
|
413
|
-
|
414
|
-
|
414
|
+
aliases = [ v ] if v
|
415
|
+
when :aliases
|
415
416
|
|
416
|
-
|
417
|
-
|
417
|
+
aliases = v unless aliases
|
418
|
+
when :help
|
418
419
|
|
419
|
-
|
420
|
-
|
420
|
+
help = v
|
421
|
+
when :extras
|
421
422
|
|
422
|
-
|
423
|
-
|
424
|
-
else
|
423
|
+
extras = v
|
424
|
+
else
|
425
425
|
|
426
|
-
|
427
|
-
|
428
|
-
|
426
|
+
raise ArgumentError, "invalid option for flag: '#{k}' => '#{v}'"
|
427
|
+
end
|
428
|
+
else
|
429
429
|
|
430
|
-
|
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
|
-
# -
|
450
|
-
# -
|
451
|
-
# -
|
452
|
-
# -
|
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
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
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
|
-
|
474
|
+
case k
|
475
|
+
when Symbol
|
469
476
|
|
470
|
-
|
471
|
-
|
472
|
-
case k
|
473
|
-
when :alias
|
477
|
+
case k
|
478
|
+
when :alias
|
474
479
|
|
475
|
-
|
476
|
-
|
480
|
+
aliases = [ v ] if v
|
481
|
+
when :aliases
|
477
482
|
|
478
|
-
|
479
|
-
|
483
|
+
aliases = v unless aliases
|
484
|
+
when :help
|
480
485
|
|
481
|
-
|
482
|
-
|
486
|
+
help = v
|
487
|
+
when :values_range, :values
|
483
488
|
|
484
|
-
|
485
|
-
|
489
|
+
values_range = v
|
490
|
+
when :default_value, :default
|
486
491
|
|
487
|
-
|
488
|
-
|
492
|
+
default_value = v
|
493
|
+
when :required
|
489
494
|
|
490
|
-
|
491
|
-
|
495
|
+
required = v
|
496
|
+
when :required_message
|
492
497
|
|
493
|
-
|
494
|
-
|
498
|
+
require_message = v
|
499
|
+
when :extras
|
495
500
|
|
496
|
-
|
497
|
-
|
501
|
+
extras = v
|
502
|
+
when :constraint
|
498
503
|
|
499
|
-
|
500
|
-
|
504
|
+
constraint = v
|
505
|
+
else
|
501
506
|
|
502
|
-
|
503
|
-
|
504
|
-
|
507
|
+
raise ArgumentError, "invalid option for option: '#{k}' => '#{v}'"
|
508
|
+
end
|
509
|
+
else
|
505
510
|
|
506
|
-
|
507
|
-
|
508
|
-
|
511
|
+
raise ArgumentError, "invalid option type for option: '#{k}' (#{k.class}) => '#{v}'"
|
512
|
+
end
|
513
|
+
end
|
509
514
|
|
510
|
-
|
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
|
-
|
516
|
-
|
520
|
+
options = args.pop if args[-1].is_a?(::Hash)
|
521
|
+
options ||= {}
|
517
522
|
|
518
|
-
|
523
|
+
if options[:alias]
|
519
524
|
|
520
|
-
|
521
|
-
|
525
|
+
aliases = [ options[:alias] ]
|
526
|
+
elsif options[:aliases]
|
522
527
|
|
523
|
-
|
524
|
-
|
528
|
+
aliases = options[:aliases]
|
529
|
+
else
|
525
530
|
|
526
|
-
|
527
|
-
|
531
|
+
aliases = args
|
532
|
+
end
|
528
533
|
|
529
|
-
|
534
|
+
CLASP::AliasSpecification.new name, aliases
|
530
535
|
end
|
531
536
|
|
537
|
+
|
532
538
|
# ######################################################################## #
|
533
539
|
# backwards-compatible
|
534
540
|
|
535
|
-
Alias
|
536
|
-
Flag
|
537
|
-
FlagAlias
|
538
|
-
Option
|
539
|
-
OptionAlias
|
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
|
|