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