clasp-ruby 0.13.3 → 0.15.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 +5 -5
- data/README.md +41 -15
- data/lib/clasp/aliases.rb +20 -14
- data/lib/clasp/arguments.rb +61 -36
- data/lib/clasp/cli.rb +9 -6
- data/lib/clasp/doc_.rb +9 -9
- data/lib/clasp/version.rb +3 -3
- data/test/unit/tc_ARGV_rewrite.rb +8 -2
- data/test/unit/tc_arguments_1.rb +8 -0
- data/test/unit/tc_arguments_inspect.rb +89 -0
- data/test/unit/tc_cli.rb +2 -2
- data/test/unit/tc_usage.rb +3 -3
- metadata +8 -10
- data/lib/clasp/util/immutable_array.rb +0 -169
- data/test/unit/tc_immutable_array.rb +0 -72
- data/test/unit/tc_util_immutable_array.rb +0 -72
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3f7f769b1c131cece4a31779001e4ffc049137a7
|
4
|
+
data.tar.gz: 5aada90053e599010596a966403341056ec9d793
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eb9f3f04abef57af05007a02c9797cc5b628862ea6cecbb350feb4ececcb9026e2f44498bc6fe73f375b296afc89ce45e05cdb1aa395a70cd2d5537a37ea554
|
7
|
+
data.tar.gz: 38e6c9bc1b26ebdeab1559cec077e031404d0b7c48b500c9665bbc9e40a91f97684d445478e71cc1b3da2444ee733b08ce27c38d4ede5f14ed66d11238b0883b
|
data/README.md
CHANGED
@@ -5,17 +5,29 @@ Command-Line Argument Sorting and Parsing for Ruby
|
|
5
5
|
|
6
6
|
## Installation & usage
|
7
7
|
|
8
|
-
Install
|
8
|
+
Install via **gem** as in:
|
9
|
+
|
10
|
+
```
|
11
|
+
gem install clasp-ruby
|
12
|
+
```
|
13
|
+
|
14
|
+
or add it to your `Gemfile`.
|
15
|
+
|
16
|
+
Use via **require***, as in:
|
17
|
+
|
18
|
+
```Ruby
|
19
|
+
require 'clasp-ruby'
|
20
|
+
```
|
9
21
|
|
10
22
|
## Description
|
11
23
|
|
12
|
-
**CLASP** stands for **C
|
13
|
-
**P
|
24
|
+
**CLASP** stands for **C**ommand-**L**ine **A**rgument **S**orting and
|
25
|
+
**P**arsing. The first CLASP library was a C library with a C++ wrapper. There
|
14
26
|
have been several implementations in other languages. **CLASP.Ruby** is the
|
15
27
|
Ruby version.
|
16
28
|
|
17
|
-
All
|
18
|
-
**I
|
29
|
+
All CLASP libraries provide the following facilities to **C**ommand **L**ine
|
30
|
+
**I**nterface (**CLI**) programs:
|
19
31
|
|
20
32
|
### Command-line parsing
|
21
33
|
|
@@ -27,7 +39,9 @@ All **CLASP** libraries discriminate between three types of command-line argumen
|
|
27
39
|
|
28
40
|
For example, in the command line
|
29
41
|
|
30
|
-
|
42
|
+
```
|
43
|
+
myprog --all -c --opt1=val1 infile outfile
|
44
|
+
```
|
31
45
|
|
32
46
|
there are:
|
33
47
|
|
@@ -37,19 +51,27 @@ there are:
|
|
37
51
|
|
38
52
|
*Flags* and *options* may have alias. If the alias for `--all` is `-a` and the alias for `--opt1` is `-o` then the following command-line is exactly equivalent to the previous one:
|
39
53
|
|
40
|
-
|
54
|
+
```
|
55
|
+
myprog -a -c -o val1 infile outfile
|
56
|
+
```
|
41
57
|
|
42
58
|
One-letter *flags* may be combined. Hence, the following command-line is exactly equivalent to the previous ones:
|
43
59
|
|
44
|
-
|
60
|
+
```
|
61
|
+
myprog -ac -o val1 infile outfile
|
62
|
+
```
|
45
63
|
|
46
64
|
Option aliases may specify a value. If the alias `-v1` means `--opt1=val1` then the following command-line is exactly equivalent to the previous ones:
|
47
65
|
|
48
|
-
|
66
|
+
```
|
67
|
+
myprog -ac -v1 infile outfile
|
68
|
+
```
|
49
69
|
|
50
70
|
Option aliases that are one letter may be combined with one-letter flags. If the alias `-v` means `--opt1=val1` then the following command-line is exactly equivalent to the previous ones:
|
51
71
|
|
52
|
-
|
72
|
+
```
|
73
|
+
myprog -acv infile outfile
|
74
|
+
```
|
53
75
|
|
54
76
|
UNIX standard arguments confer specific meanings:
|
55
77
|
|
@@ -59,7 +81,7 @@ UNIX standard arguments confer specific meanings:
|
|
59
81
|
|
60
82
|
### Declarative specification of the flags and options for a CLI
|
61
83
|
|
62
|
-
To support such above special processing,
|
84
|
+
To support such above special processing, CLASP libraries provide facilities
|
63
85
|
for declarative specification of command-line *flags* and *options*, and
|
64
86
|
aliases thereof. For the previous example, the **CLASP.Ruby** code would look
|
65
87
|
like the following:
|
@@ -101,7 +123,7 @@ puts Args.values[1] # => "outfile"
|
|
101
123
|
|
102
124
|
### Utility functions for displaying usage and version information
|
103
125
|
|
104
|
-
There are aspects common to all CLI programs, such as responding to `--help` and `--version`. All
|
126
|
+
There are aspects common to all CLI programs, such as responding to `--help` and `--version`. All **CLASP** libraries provide facilities to assist the programmer: **CLASP.Ruby** provides the two module methods CLASP.show_usage() and CLASP.show_version(), as shown in the following code extending the example above:
|
105
127
|
|
106
128
|
```ruby
|
107
129
|
|
@@ -124,7 +146,9 @@ Args.flags.each do |f|
|
|
124
146
|
|
125
147
|
Given the command
|
126
148
|
|
127
|
-
|
149
|
+
```
|
150
|
+
./cr-example.rb --help
|
151
|
+
```
|
128
152
|
|
129
153
|
then the program will output the following
|
130
154
|
|
@@ -157,7 +181,9 @@ flags/options:
|
|
157
181
|
|
158
182
|
and given the command
|
159
183
|
|
160
|
-
|
184
|
+
```
|
185
|
+
./cr-example.rb --version
|
186
|
+
```
|
161
187
|
|
162
188
|
then the program will output the following
|
163
189
|
|
@@ -177,7 +203,7 @@ Defect reports, feature requests, and pull requests are welcome on https://githu
|
|
177
203
|
|
178
204
|
**CLASP.Ruby** is inspired by the [C/C++ CLASP library](https://github.com/synesissoftware/CLASP), which is documented in the articles:
|
179
205
|
|
180
|
-
* _An Introduction to
|
206
|
+
* _An Introduction to CLASP_, Matthew Wilson, [CVu](http://accu.org/index.php/journals/c77/), January 2012;
|
181
207
|
* _[Anatomy of a CLI Program written in C](http://synesis.com.au/publishing/software-anatomies/anatomy-of-a-cli-program-written-in-c.html)_, Matthew Wilson, [CVu](http://accu.org/index.php/journals/c77/), September 2012; and
|
182
208
|
* _[Anatomy of a CLI Program written in C++](http://synesis.com.au/publishing/software-anatomies/anatomy-of-a-cli-program-written-in-c++.html)_, Matthew Wilson, [CVu](http://accu.org/index.php/journals/c77/), September 2015.
|
183
209
|
|
data/lib/clasp/aliases.rb
CHANGED
@@ -5,13 +5,13 @@
|
|
5
5
|
# Purpose: Alias classes
|
6
6
|
#
|
7
7
|
# Created: 25th October 2014
|
8
|
-
# Updated:
|
8
|
+
# Updated: 14th February 2019
|
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-2019, 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
|
@@ -58,9 +58,9 @@ module CLASP
|
|
58
58
|
# classes
|
59
59
|
|
60
60
|
# A class that represents the specification for a command-line flag
|
61
|
-
class
|
61
|
+
class FlagAlias
|
62
62
|
|
63
|
-
# Creates a
|
63
|
+
# Creates a FlagAlias instance from the given name, aliases, and help
|
64
64
|
#
|
65
65
|
# === Signature
|
66
66
|
#
|
@@ -96,7 +96,7 @@ class Flag
|
|
96
96
|
@@Help_ = self.new('--help', [], 'shows this help and terminates')
|
97
97
|
@@Version_ = self.new('--version', [], 'shows version and terminates')
|
98
98
|
public
|
99
|
-
# An instance of
|
99
|
+
# An instance of FlagAlias that provides default '--help' information
|
100
100
|
def self.Help(extras = nil)
|
101
101
|
|
102
102
|
h = @@Help_
|
@@ -106,7 +106,7 @@ class Flag
|
|
106
106
|
h
|
107
107
|
end
|
108
108
|
|
109
|
-
# An instance of
|
109
|
+
# An instance of FlagAlias that provides default '--version' information
|
110
110
|
def self.Version(extras = nil)
|
111
111
|
|
112
112
|
h = @@Version_
|
@@ -118,9 +118,9 @@ class Flag
|
|
118
118
|
end
|
119
119
|
|
120
120
|
# A class that represents the specification for a command-line option
|
121
|
-
class
|
121
|
+
class OptionAlias
|
122
122
|
|
123
|
-
# Creates an
|
123
|
+
# Creates an OptionAlias instance from the given name, aliases, help,
|
124
124
|
# values_range, and default_value
|
125
125
|
#
|
126
126
|
# === Signature
|
@@ -227,7 +227,7 @@ end
|
|
227
227
|
# ######################################################################## #
|
228
228
|
# functions
|
229
229
|
|
230
|
-
# Generator method that obtains a CLASP::
|
230
|
+
# Generator method that obtains a CLASP::FlagAlias according to the given
|
231
231
|
# parameters
|
232
232
|
#
|
233
233
|
# === Signature
|
@@ -277,10 +277,10 @@ def CLASP.Flag(name, options = {})
|
|
277
277
|
end
|
278
278
|
end
|
279
279
|
|
280
|
-
CLASP::
|
280
|
+
CLASP::FlagAlias.new(name, aliases, help, extras)
|
281
281
|
end
|
282
282
|
|
283
|
-
# Generator method that obtains a CLASP::
|
283
|
+
# Generator method that obtains a CLASP::OptionAlias according to the given
|
284
284
|
# parameters
|
285
285
|
#
|
286
286
|
# === Signature
|
@@ -352,15 +352,15 @@ def CLASP.Option(name, options = {})
|
|
352
352
|
extras = v
|
353
353
|
else
|
354
354
|
|
355
|
-
raise ArgumentError, "invalid option for
|
355
|
+
raise ArgumentError, "invalid option for option: '#{k}' => '#{v}'"
|
356
356
|
end
|
357
357
|
else
|
358
358
|
|
359
|
-
raise ArgumentError, "invalid option type for
|
359
|
+
raise ArgumentError, "invalid option type for option: '#{k}' (#{k.class}) => '#{v}'"
|
360
360
|
end
|
361
361
|
end
|
362
362
|
|
363
|
-
CLASP::
|
363
|
+
CLASP::OptionAlias.new(name, aliases, help, values_range, default_value, required, require_message, extras)
|
364
364
|
end
|
365
365
|
|
366
366
|
def CLASP.Alias(name, *args)
|
@@ -382,6 +382,12 @@ def CLASP.Alias(name, *args)
|
|
382
382
|
CLASP::Alias.new name, aliases
|
383
383
|
end
|
384
384
|
|
385
|
+
# ######################################################################## #
|
386
|
+
# backwards-compatible
|
387
|
+
|
388
|
+
Flag = FlagAlias
|
389
|
+
Option = OptionAlias
|
390
|
+
|
385
391
|
# ######################################################################## #
|
386
392
|
# module
|
387
393
|
|
data/lib/clasp/arguments.rb
CHANGED
@@ -6,13 +6,13 @@
|
|
6
6
|
# CLASP.Ruby
|
7
7
|
#
|
8
8
|
# Created: 14th February 2014
|
9
|
-
# Updated:
|
9
|
+
# Updated: 14th February 2019
|
10
10
|
#
|
11
11
|
# Home: http://github.com/synesissoftware/CLASP.Ruby
|
12
12
|
#
|
13
13
|
# Author: Matthew Wilson
|
14
14
|
#
|
15
|
-
# Copyright (c) 2014-
|
15
|
+
# Copyright (c) 2014-2019, Matthew Wilson and Synesis Software
|
16
16
|
# All rights reserved.
|
17
17
|
#
|
18
18
|
# Redistribution and use in source and binary forms, with or without
|
@@ -46,8 +46,6 @@
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
-
require 'clasp/util/immutable_array'
|
50
|
-
|
51
49
|
=begin
|
52
50
|
=end
|
53
51
|
|
@@ -61,7 +59,7 @@ class Arguments
|
|
61
59
|
|
62
60
|
#:stopdoc:
|
63
61
|
private
|
64
|
-
class
|
62
|
+
class FlagArgument #:nodoc: all
|
65
63
|
|
66
64
|
#:nodoc:
|
67
65
|
def initialize(arg, given_index, given_name, resolved_name, argument_alias, given_hyphens, given_label, extras)
|
@@ -119,7 +117,7 @@ class Arguments
|
|
119
117
|
end
|
120
118
|
end
|
121
119
|
|
122
|
-
class
|
120
|
+
class OptionArgument #:nodoc: all
|
123
121
|
|
124
122
|
#:nodoc:
|
125
123
|
def initialize(arg, given_index, given_name, resolved_name, argument_alias, given_hyphens, given_label, value, extras)
|
@@ -181,23 +179,6 @@ class Arguments
|
|
181
179
|
|
182
180
|
#:startdoc:
|
183
181
|
|
184
|
-
class ImmutableArray < ::CLASP::Util::ImmutableArray
|
185
|
-
|
186
|
-
def initialize a
|
187
|
-
|
188
|
-
super a
|
189
|
-
end
|
190
|
-
|
191
|
-
# returns truthy if the given flag/option is found or the
|
192
|
-
# given block is truthy
|
193
|
-
def specified? id = nil
|
194
|
-
|
195
|
-
return find(id) { |o| yield o } if block_given?
|
196
|
-
|
197
|
-
find { |item| item == id }
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
182
|
# ######################
|
202
183
|
# Construction
|
203
184
|
|
@@ -226,7 +207,8 @@ class Arguments
|
|
226
207
|
|
227
208
|
|
228
209
|
@argv = argv
|
229
|
-
|
210
|
+
argv = argv.dup
|
211
|
+
@argv_original_copy = argv.dup.freeze
|
230
212
|
|
231
213
|
@aliases = aliases
|
232
214
|
|
@@ -234,9 +216,40 @@ class Arguments
|
|
234
216
|
|
235
217
|
flags, options, values = Arguments.parse(argv, aliases)
|
236
218
|
|
237
|
-
|
238
|
-
|
239
|
-
|
219
|
+
[ flags, options, values ].each do |ar|
|
220
|
+
|
221
|
+
class << ar
|
222
|
+
|
223
|
+
undef :inspect
|
224
|
+
undef :to_s
|
225
|
+
|
226
|
+
def to_s
|
227
|
+
|
228
|
+
s = ''
|
229
|
+
|
230
|
+
s += '['
|
231
|
+
s += self.map { |v| %Q<"#{v}"> }.join(', ')
|
232
|
+
s += ']'
|
233
|
+
|
234
|
+
s
|
235
|
+
end
|
236
|
+
|
237
|
+
def inspect
|
238
|
+
|
239
|
+
s = ''
|
240
|
+
|
241
|
+
s += "#<#{self.class}:0x#{(object_id << 1).to_s(16)} ["
|
242
|
+
s += self.map { |v| v.inspect }.join(', ')
|
243
|
+
s += "]>"
|
244
|
+
|
245
|
+
s
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
@flags = flags.freeze
|
251
|
+
@options = options.freeze
|
252
|
+
@values = values.freeze
|
240
253
|
|
241
254
|
|
242
255
|
# do argv-mutation, if required
|
@@ -300,6 +313,7 @@ class Arguments
|
|
300
313
|
resolved_name = "#$1#$2"
|
301
314
|
value ||= $'
|
302
315
|
end
|
316
|
+
|
303
317
|
break
|
304
318
|
end
|
305
319
|
end
|
@@ -316,7 +330,7 @@ class Arguments
|
|
316
330
|
flag_alias = nil
|
317
331
|
|
318
332
|
# special case where the flag's actual name is short form and found here
|
319
|
-
flag_alias ||= aliases.detect { |a| a.is_a?(CLASP::
|
333
|
+
flag_alias ||= aliases.detect { |a| a.is_a?(CLASP::FlagAlias) && a.name == new_flag }
|
320
334
|
|
321
335
|
# if not found as a flag, look in each aliases' aliases
|
322
336
|
flag_alias ||= aliases.detect { |a| a.aliases.include? new_flag }
|
@@ -342,8 +356,8 @@ class Arguments
|
|
342
356
|
|
343
357
|
grp_flags, grp_options, grp_value = Arguments.parse flags_argv, aliases
|
344
358
|
|
345
|
-
grp_flags.map! { |f|
|
346
|
-
grp_options.map! { |o|
|
359
|
+
grp_flags.map! { |f| FlagArgument.new(arg, index, given_name, f.name, f.argument_alias, hyphens.size, given_label, argument_alias ? argument_alias.extras : nil) }
|
360
|
+
grp_options.map! { |o| OptionArgument.new(arg, index, given_name, o.name, o.argument_alias, hyphens.size, given_label, o.value, argument_alias ? argument_alias.extras : nil) }
|
347
361
|
|
348
362
|
flags.push(*grp_flags)
|
349
363
|
options.push(*grp_options)
|
@@ -353,18 +367,18 @@ class Arguments
|
|
353
367
|
end
|
354
368
|
end
|
355
369
|
|
356
|
-
if argument_alias and argument_alias.is_a? CLASP::
|
370
|
+
if argument_alias and argument_alias.is_a? CLASP::OptionAlias and not value
|
357
371
|
|
358
372
|
want_option_value = true
|
359
|
-
options <<
|
373
|
+
options << OptionArgument.new(arg, index, given_name, resolved_name, argument_alias, hyphens.size, given_label, nil, argument_alias ? argument_alias.extras : nil)
|
360
374
|
elsif value
|
361
375
|
|
362
376
|
want_option_value = false
|
363
|
-
options <<
|
377
|
+
options << OptionArgument.new(arg, index, given_name, resolved_name, argument_alias, hyphens.size, given_label, value, argument_alias ? argument_alias.extras : nil)
|
364
378
|
else
|
365
379
|
|
366
380
|
want_option_value = false
|
367
|
-
flags <<
|
381
|
+
flags << FlagArgument.new(arg, index, given_name, resolved_name, argument_alias, hyphens.size, given_label, argument_alias ? argument_alias.extras : nil)
|
368
382
|
end
|
369
383
|
|
370
384
|
next
|
@@ -378,6 +392,11 @@ class Arguments
|
|
378
392
|
want_option_value = false
|
379
393
|
else
|
380
394
|
|
395
|
+
arg = arg.dup
|
396
|
+
arg_ix = ::Integer === index ? index : index.dup
|
397
|
+
|
398
|
+
arg.define_singleton_method(:given_index) { arg_ix }
|
399
|
+
|
381
400
|
values << arg
|
382
401
|
end
|
383
402
|
end
|
@@ -422,16 +441,22 @@ class Arguments
|
|
422
441
|
|
423
442
|
flags.each do |f|
|
424
443
|
|
425
|
-
return f unless aliases.any? { |al| al.is_a?(::CLASP::
|
444
|
+
return f unless aliases.any? { |al| al.is_a?(::CLASP::FlagAlias) && al.name == f.name }
|
426
445
|
end
|
427
446
|
|
428
447
|
options.each do |o|
|
429
448
|
|
430
|
-
return o unless aliases.any? { |al| al.is_a?(::CLASP::
|
449
|
+
return o unless aliases.any? { |al| al.is_a?(::CLASP::OptionAlias) && al.name == o.name }
|
431
450
|
end
|
432
451
|
|
433
452
|
nil
|
434
453
|
end
|
454
|
+
|
455
|
+
# #################################################################### #
|
456
|
+
# backwards-compatible
|
457
|
+
|
458
|
+
Flag = FlagArgument
|
459
|
+
Option = OptionArgument
|
435
460
|
end
|
436
461
|
|
437
462
|
# ######################################################################## #
|
data/lib/clasp/cli.rb
CHANGED
@@ -5,13 +5,13 @@
|
|
5
5
|
# Purpose: Command-line interface
|
6
6
|
#
|
7
7
|
# Created: 27th July 2015
|
8
|
-
# Updated:
|
8
|
+
# Updated: 14th February 2019
|
9
9
|
#
|
10
10
|
# Home: http://github.com/synesissoftware/CLASP.Ruby
|
11
11
|
#
|
12
12
|
# Author: Matthew Wilson
|
13
13
|
#
|
14
|
-
# Copyright (c) 2015-
|
14
|
+
# Copyright (c) 2015-2019, 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
|
@@ -64,7 +64,7 @@ module CLI_helpers_
|
|
64
64
|
|
65
65
|
module Constants
|
66
66
|
|
67
|
-
VALID_ALIAS_TYPES = [
|
67
|
+
VALID_ALIAS_TYPES = [ FlagAlias, OptionAlias, Alias ]
|
68
68
|
VALID_ALIAS_TYPES_STRING = VALID_ALIAS_TYPES[0...-1].join(', ') + ', or ' + VALID_ALIAS_TYPES[-1].to_s
|
69
69
|
end # module Constants
|
70
70
|
|
@@ -146,13 +146,16 @@ def self.show_usage aliases, options={}
|
|
146
146
|
info_lines = options[:info_lines]
|
147
147
|
case info_lines
|
148
148
|
when ::Array
|
149
|
+
|
149
150
|
;
|
150
151
|
when ::NilClass
|
152
|
+
|
151
153
|
info_lines = []
|
152
154
|
else
|
155
|
+
|
153
156
|
info_lines = [ info_lines ] unless [ :each, :empty? ].all? { |m| info_lines.respond_to? m }
|
154
157
|
end
|
155
|
-
info_lines.map
|
158
|
+
info_lines = info_lines.map do |line|
|
156
159
|
|
157
160
|
case line
|
158
161
|
when :version
|
@@ -207,7 +210,7 @@ def self.show_usage aliases, options={}
|
|
207
210
|
when Alias
|
208
211
|
|
209
212
|
next
|
210
|
-
when
|
213
|
+
when FlagAlias
|
211
214
|
|
212
215
|
if fas.has_key? a.name
|
213
216
|
|
@@ -219,7 +222,7 @@ def self.show_usage aliases, options={}
|
|
219
222
|
a.aliases.each { |al| stream.puts "\t#{al}" }
|
220
223
|
stream.puts "\t#{a.name}"
|
221
224
|
stream.puts "\t\t#{a.help}"
|
222
|
-
when
|
225
|
+
when OptionAlias
|
223
226
|
|
224
227
|
if voas.has_key? a.name
|
225
228
|
|
data/lib/clasp/doc_.rb
CHANGED
@@ -5,13 +5,13 @@
|
|
5
5
|
# Purpose: Documentation of the CLASP.Ruby modules
|
6
6
|
#
|
7
7
|
# Created: 11th June 2016
|
8
|
-
# Updated:
|
8
|
+
# Updated: 14th February 2019
|
9
9
|
#
|
10
10
|
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
12
12
|
# Author: Matthew Wilson
|
13
13
|
#
|
14
|
-
# Copyright (c) 2016, Matthew Wilson and Synesis Software
|
14
|
+
# Copyright (c) 2016-2019, 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
|
@@ -58,9 +58,9 @@
|
|
58
58
|
# args = CLASP::Arguments.new(argv)
|
59
59
|
#
|
60
60
|
# puts args.flags.size # => 1
|
61
|
-
# puts args.flags[0] # => "#<CLASP::Arguments::
|
61
|
+
# puts args.flags[0] # => "#<CLASP::Arguments::FlagArgument:0x007fd23aa3aa98 @arg="-c", @given_index=1, @given_name="-c", @argument_alias=nil, @given_hyphens=1, @given_label="c", @name="-c", @extras={}>"
|
62
62
|
# puts args.options.size # => 1
|
63
|
-
# puts args.options[0] # => "#<CLASP::Arguments::
|
63
|
+
# puts args.options[0] # => "#<CLASP::Arguments::OptionArgument:0x007fd23aa3aca0 @arg="--show-all=true", @given_index=0, @given_name="--show-all", @argument_alias=nil, @given_hyphens=2, @given_label="show-all", @value="true", @name="--show-all", @extras={}>"
|
64
64
|
# puts args.options[0].value # => "true"
|
65
65
|
# puts args.values.size # => 2
|
66
66
|
# puts args.values[0] # => "infile"
|
@@ -74,7 +74,7 @@
|
|
74
74
|
#
|
75
75
|
# puts args.flags.size # => 0
|
76
76
|
# puts args.options.size # => 1
|
77
|
-
# puts args.options[0] # => "#<CLASP::Arguments::
|
77
|
+
# puts args.options[0] # => "#<CLASP::Arguments::OptionArgument:0x007fd23aa3aca0 @arg="--show-all=true", @given_index=0, @given_name="--show-all", @argument_alias=nil, @given_hyphens=2, @given_label="show-all", @value="true", @name="--show-all", @extras={}>"
|
78
78
|
# puts args.values.size # => 3
|
79
79
|
# puts args.values[0] # => "infile"
|
80
80
|
# puts args.values[1] # => "outfile"
|
@@ -127,17 +127,17 @@
|
|
127
127
|
# args = CLASP::Arguments.new(argv, aliases)
|
128
128
|
#
|
129
129
|
# puts args.flags.size # => 1
|
130
|
-
# puts args.flags[0] # => "#<CLASP::Arguments::
|
130
|
+
# puts args.flags[0] # => "#<CLASP::Arguments::FlagArgument:0x007f8593b0ddd8 @arg="-c", @given_index=0, @given_name="-c", @argument_alias=nil, @given_hyphens=1, @given_label="c", @name="-c", @extras={}>"
|
131
131
|
# puts args.options.size # => 1
|
132
|
-
# puts args.options[0] # => "#<CLASP::Arguments::
|
132
|
+
# puts args.options[0] # => "#<CLASP::Arguments::OptionArgument:0x007f8593b0db80 @arg="-a", @given_index=1, @given_name="-a", @argument_alias=#<CLASP::OptionArgument:0x007f8593b2ea10 @name="--show-all", @aliases=["-a"], @help=nil, @values_range=[], @default_value=nil, @extras={}>, @given_hyphens=1, @given_label="a", @value="true", @name="--show-all", @extras={}>"
|
133
133
|
# puts args.values.size # => 2
|
134
134
|
# puts args.values[0] # => "infile"
|
135
135
|
# puts args.values[1] # => "outfile"
|
136
136
|
#
|
137
137
|
# === Classes of interest
|
138
138
|
# * ::CLASP::Arguments
|
139
|
-
# * ::CLASP::
|
140
|
-
# * ::CLASP::
|
139
|
+
# * ::CLASP::FlagArgument
|
140
|
+
# * ::CLASP::OptionArgument
|
141
141
|
#
|
142
142
|
# === Functions of interest
|
143
143
|
# * ::CLASP.show_version()
|
data/lib/clasp/version.rb
CHANGED
@@ -5,13 +5,13 @@
|
|
5
5
|
# Purpose: Version for CLASP.Ruby library
|
6
6
|
#
|
7
7
|
# Created: 16th November 2014
|
8
|
-
# Updated:
|
8
|
+
# Updated: 17th March 2019
|
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-2019, 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
|
@@ -51,7 +51,7 @@
|
|
51
51
|
module CLASP
|
52
52
|
|
53
53
|
# Current version of the CLASP.Ruby library
|
54
|
-
VERSION = '0.
|
54
|
+
VERSION = '0.15.1'
|
55
55
|
|
56
56
|
private
|
57
57
|
VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
|
@@ -12,9 +12,15 @@ class Test_Arguments_4 < Test::Unit::TestCase
|
|
12
12
|
|
13
13
|
aliases = nil
|
14
14
|
argv = [ 'val1', '-f1', 'val2', '--' ]
|
15
|
+
|
16
|
+
assert_equal 'val1|-f1|val2|--', argv.join('|')
|
17
|
+
|
15
18
|
argv_c = argv.dup
|
16
19
|
args = CLASP::Arguments.new argv, aliases
|
17
20
|
|
21
|
+
assert_equal 'val1|-f1|val2|--', argv.join('|')
|
22
|
+
|
23
|
+
|
18
24
|
assert_equal 1, args.flags.size
|
19
25
|
assert_equal Hash.new, args.flags[0].extras
|
20
26
|
|
@@ -23,9 +29,9 @@ class Test_Arguments_4 < Test::Unit::TestCase
|
|
23
29
|
assert_equal 2, args.values.size
|
24
30
|
|
25
31
|
assert_same argv, args.argv
|
26
|
-
assert_equal
|
32
|
+
assert_equal 4, argv.size
|
27
33
|
assert_equal 'val1', argv[0]
|
28
|
-
assert_equal 'val2', argv[
|
34
|
+
assert_equal 'val2', argv[2]
|
29
35
|
|
30
36
|
assert_equal args.argv_original_copy, argv_c
|
31
37
|
assert_equal argv_c, args.argv_original_copy
|
data/test/unit/tc_arguments_1.rb
CHANGED
@@ -294,6 +294,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
294
294
|
def test_double_hyphen_3
|
295
295
|
|
296
296
|
aliases = [
|
297
|
+
|
297
298
|
CLASP.Option('--password', alias: '-p', extras: 'extra'),
|
298
299
|
]
|
299
300
|
args = CLASP::Arguments.new [ '-f1', 'value1', '-p', '--', 'value2' ], aliases
|
@@ -359,6 +360,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
359
360
|
def test_flag_aliases_2
|
360
361
|
|
361
362
|
aliases = [
|
363
|
+
|
362
364
|
CLASP.Flag('--expand', alias: '-x')
|
363
365
|
]
|
364
366
|
args = CLASP::Arguments.new [ '-f1', 'value1', '-x', '--delete' ], aliases
|
@@ -395,6 +397,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
395
397
|
def test_flag_aliases_3
|
396
398
|
|
397
399
|
aliases = [
|
400
|
+
|
398
401
|
CLASP.Flag('--expand', aliases: [ '-x', '--x' ], extras: %w{ e x t r a })
|
399
402
|
]
|
400
403
|
args = CLASP::Arguments.new [ '-f1', 'value1', '-x', '--delete', '--x' ], aliases
|
@@ -465,6 +468,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
465
468
|
def test_option_aliases_2
|
466
469
|
|
467
470
|
aliases = [
|
471
|
+
|
468
472
|
CLASP.Option('--option', aliases: [ '-o' ])
|
469
473
|
]
|
470
474
|
args = CLASP::Arguments.new [ '-f1', 'value1', '-o=value' ], aliases
|
@@ -493,6 +497,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
493
497
|
def test_option_aliases_3
|
494
498
|
|
495
499
|
aliases = [
|
500
|
+
|
496
501
|
CLASP.Option('--option', aliases: [ '-o' ])
|
497
502
|
]
|
498
503
|
args = CLASP::Arguments.new [ '-f1', 'value1', '-o', 'value' ], aliases
|
@@ -521,6 +526,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
521
526
|
def test_option_default_aliases_1
|
522
527
|
|
523
528
|
aliases = [
|
529
|
+
|
524
530
|
CLASP.Option('--option', aliases: [ '-o' ]),
|
525
531
|
CLASP.Option('--option=special-value', alias: '-s')
|
526
532
|
]
|
@@ -564,6 +570,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
564
570
|
def test_option_default_aliases_2
|
565
571
|
|
566
572
|
aliases = [
|
573
|
+
|
567
574
|
CLASP.Option('--option', aliases: [ '-o' ]),
|
568
575
|
CLASP.Flag('--option=special-value', alias: '-s'),
|
569
576
|
CLASP.Flag('--verbose', alias: '-v'),
|
@@ -624,6 +631,7 @@ class Test_Arguments < Test::Unit::TestCase
|
|
624
631
|
def test_option_default_aliases_3
|
625
632
|
|
626
633
|
aliases = [
|
634
|
+
|
627
635
|
CLASP.Option('--option', aliases: [ '-o' ]),
|
628
636
|
CLASP.Flag('--option=special-value', alias: '-s'),
|
629
637
|
CLASP.Flag('-v'),
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
|
4
|
+
|
5
|
+
require 'clasp'
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
|
9
|
+
class Test_Arguments_inspect_and_to_s < Test::Unit::TestCase
|
10
|
+
|
11
|
+
include CLASP
|
12
|
+
|
13
|
+
def test_no_arguments
|
14
|
+
|
15
|
+
argv = []
|
16
|
+
args = Arguments.new argv
|
17
|
+
|
18
|
+
assert_equal %Q<[]>, args.flags.to_s
|
19
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.flags.inspect
|
20
|
+
|
21
|
+
assert_equal %Q<[]>, args.options.to_s
|
22
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.options.inspect
|
23
|
+
|
24
|
+
assert_equal %Q<[]>, args.values.to_s
|
25
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.values.inspect
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_one_value
|
29
|
+
|
30
|
+
argv = [ 'val1' ]
|
31
|
+
args = Arguments.new argv
|
32
|
+
|
33
|
+
assert_equal %Q<[]>, args.flags.to_s
|
34
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.flags.inspect
|
35
|
+
|
36
|
+
assert_equal %Q<[]>, args.options.to_s
|
37
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.options.inspect
|
38
|
+
|
39
|
+
assert_equal %Q<["val1"]>, args.values.to_s
|
40
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\["val1"\].*>/, args.values.inspect
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_three_values
|
44
|
+
|
45
|
+
argv = [ 'val1', 'val2', 'val3' ]
|
46
|
+
args = Arguments.new argv
|
47
|
+
|
48
|
+
assert_equal %Q<[]>, args.flags.to_s
|
49
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.flags.inspect
|
50
|
+
|
51
|
+
assert_equal %Q<[]>, args.options.to_s
|
52
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.options.inspect
|
53
|
+
|
54
|
+
assert_equal %Q<["val1", "val2", "val3"]>, args.values.to_s
|
55
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\["val1", "val2", "val3"\].*>/, args.values.inspect
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_one_flag
|
59
|
+
|
60
|
+
argv = [ '-f' ]
|
61
|
+
args = Arguments.new argv
|
62
|
+
|
63
|
+
assert_equal %Q<["-f"]>, args.flags.to_s
|
64
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[#<CLASP::Arguments::FlagArgument:0x.*-f.*>\].*>/, args.flags.inspect
|
65
|
+
|
66
|
+
assert_equal %Q<[]>, args.options.to_s
|
67
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.options.inspect
|
68
|
+
|
69
|
+
assert_equal %Q<[]>, args.values.to_s
|
70
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.values.inspect
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_two_flags
|
74
|
+
|
75
|
+
argv = [ '-f', '-g' ]
|
76
|
+
args = Arguments.new argv
|
77
|
+
|
78
|
+
assert_equal %Q<["-f", "-g"]>, args.flags.to_s
|
79
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[#<CLASP::Arguments::FlagArgument:0x.*-f.*-g.*>\].*>/, args.flags.inspect
|
80
|
+
|
81
|
+
assert_equal %Q<[]>, args.options.to_s
|
82
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.options.inspect
|
83
|
+
|
84
|
+
assert_equal %Q<[]>, args.values.to_s
|
85
|
+
assert_match /#<Array:0x[0-9a-fA-Z]+\s+\[\].*>/, args.values.inspect
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
data/test/unit/tc_cli.rb
CHANGED
@@ -14,9 +14,9 @@ class Test_CLI < Test::Unit::TestCase
|
|
14
14
|
|
15
15
|
def test_invalid_aliases_types
|
16
16
|
|
17
|
-
assert_raise_with_message(::TypeError, 'each element in aliases array must be one of the types CLASP::
|
17
|
+
assert_raise_with_message(::TypeError, 'each element in aliases array must be one of the types CLASP::FlagAlias, CLASP::OptionAlias, or CLASP::Alias') { CLASP.show_usage([ 'abc', :def ]) }
|
18
18
|
|
19
|
-
assert_raise_with_message(::TypeError, 'each element in aliases array must be one of the types CLASP::
|
19
|
+
assert_raise_with_message(::TypeError, 'each element in aliases array must be one of the types CLASP::FlagAlias, CLASP::OptionAlias, or CLASP::Alias') { CLASP.show_version([ 'abc', :def ]) }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
data/test/unit/tc_usage.rb
CHANGED
@@ -38,7 +38,7 @@ class Test_Usage < Test::Unit::TestCase
|
|
38
38
|
|
39
39
|
stream = StringIO.new
|
40
40
|
|
41
|
-
info = 'myprog version'
|
41
|
+
info = 'myprog version'.freeze
|
42
42
|
|
43
43
|
CLASP.show_usage aliases, stream: stream, program_name: 'myprog', flags_and_options: '', info_lines: info
|
44
44
|
|
@@ -55,7 +55,7 @@ class Test_Usage < Test::Unit::TestCase
|
|
55
55
|
|
56
56
|
'Synesis Software My Program',
|
57
57
|
'version 1',
|
58
|
-
]
|
58
|
+
].freeze
|
59
59
|
|
60
60
|
CLASP.show_usage aliases, stream: stream, program_name: 'myprog', flags_and_options: '', info_lines: info_lines
|
61
61
|
|
@@ -72,7 +72,7 @@ class Test_Usage < Test::Unit::TestCase
|
|
72
72
|
|
73
73
|
'Synesis Software My Program',
|
74
74
|
:version
|
75
|
-
]
|
75
|
+
].freeze
|
76
76
|
|
77
77
|
CLASP.show_usage aliases, stream: stream, program_name: 'myprog', flags_and_options: '', info_lines: info_lines, version: [ 1, 0, 1], version_prefix: 'v'
|
78
78
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clasp-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.15.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Wilson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: xqsr3
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.30'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.30'
|
27
27
|
description: |
|
28
28
|
Command-Line Argument Sorting and Parsing library that provides a powerful
|
29
29
|
abstraction of command-line interpretation facilities. CLASP.Ruby is a Ruby port of the popular CLASP (C/C++) library, and provides declarative specification of command-line flags and options, aliasing, flag combination, UNIX de-facto standard flag processing, and a number of utility functions for expressing usage and version information.
|
@@ -43,7 +43,6 @@ files:
|
|
43
43
|
- lib/clasp/cli.rb
|
44
44
|
- lib/clasp/doc_.rb
|
45
45
|
- lib/clasp/old_module.rb
|
46
|
-
- lib/clasp/util/immutable_array.rb
|
47
46
|
- lib/clasp/version.rb
|
48
47
|
- test/scratch/test_aliases.rb
|
49
48
|
- test/scratch/test_list_command_line.rb
|
@@ -54,14 +53,13 @@ files:
|
|
54
53
|
- test/unit/tc_arguments_1.rb
|
55
54
|
- test/unit/tc_arguments_2.rb
|
56
55
|
- test/unit/tc_arguments_3.rb
|
56
|
+
- test/unit/tc_arguments_inspect.rb
|
57
57
|
- test/unit/tc_cli.rb
|
58
58
|
- test/unit/tc_defaults_1.rb
|
59
59
|
- test/unit/tc_examples_Arguments.rb
|
60
60
|
- test/unit/tc_extras.rb
|
61
|
-
- test/unit/tc_immutable_array.rb
|
62
61
|
- test/unit/tc_option_required.rb
|
63
62
|
- test/unit/tc_usage.rb
|
64
|
-
- test/unit/tc_util_immutable_array.rb
|
65
63
|
- test/unit/ts_all.rb
|
66
64
|
homepage: http://github.com/synesissoftware/CLASP.Ruby
|
67
65
|
licenses:
|
@@ -73,9 +71,9 @@ require_paths:
|
|
73
71
|
- lib
|
74
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
75
73
|
requirements:
|
76
|
-
- - "
|
74
|
+
- - "~>"
|
77
75
|
- !ruby/object:Gem::Version
|
78
|
-
version: '0'
|
76
|
+
version: '2.0'
|
79
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
78
|
requirements:
|
81
79
|
- - ">="
|
@@ -83,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
81
|
version: '0'
|
84
82
|
requirements: []
|
85
83
|
rubyforge_project:
|
86
|
-
rubygems_version: 2.
|
84
|
+
rubygems_version: 2.4.5.5
|
87
85
|
signing_key:
|
88
86
|
specification_version: 4
|
89
87
|
summary: CLASP.Ruby
|
@@ -1,169 +0,0 @@
|
|
1
|
-
|
2
|
-
# ######################################################################## #
|
3
|
-
# File: clasp/util/immutable_array.rb
|
4
|
-
#
|
5
|
-
# Purpose: Definition of the CLASP::Util::ImmutableArray class
|
6
|
-
#
|
7
|
-
# Created: 14th February 2014
|
8
|
-
# Updated: 16th July 2016
|
9
|
-
#
|
10
|
-
# Home: http://github.com/synesissoftware/CLASP.Ruby
|
11
|
-
#
|
12
|
-
# Author: Matthew Wilson
|
13
|
-
#
|
14
|
-
# Copyright (c) 2014-2016, Matthew Wilson and Synesis Software
|
15
|
-
# All rights reserved.
|
16
|
-
#
|
17
|
-
# Redistribution and use in source and binary forms, with or without
|
18
|
-
# modification, are permitted provided that the following conditions are
|
19
|
-
# met:
|
20
|
-
#
|
21
|
-
# * Redistributions of source code must retain the above copyright
|
22
|
-
# notice, this list of conditions and the following disclaimer.
|
23
|
-
#
|
24
|
-
# * Redistributions in binary form must reproduce the above copyright
|
25
|
-
# notice, this list of conditions and the following disclaimer in the
|
26
|
-
# documentation and/or other materials provided with the distribution.
|
27
|
-
#
|
28
|
-
# * Neither the names of the copyright holder nor the names of its
|
29
|
-
# contributors may be used to endorse or promote products derived from
|
30
|
-
# this software without specific prior written permission.
|
31
|
-
#
|
32
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
33
|
-
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
34
|
-
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
35
|
-
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
36
|
-
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
37
|
-
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
38
|
-
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
39
|
-
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
40
|
-
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
41
|
-
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
42
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
43
|
-
#
|
44
|
-
# ######################################################################## #
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
=begin
|
50
|
-
=end
|
51
|
-
|
52
|
-
module CLASP
|
53
|
-
module Util
|
54
|
-
|
55
|
-
# ######################################################################## #
|
56
|
-
# classes
|
57
|
-
|
58
|
-
# An immutable array
|
59
|
-
class ImmutableArray
|
60
|
-
|
61
|
-
include Enumerable
|
62
|
-
|
63
|
-
#:nodoc:
|
64
|
-
def initialize(a)
|
65
|
-
|
66
|
-
raise ArgumentError, "must supply array" if a.nil?
|
67
|
-
raise TypeError, "must supply instance of #{::Array}; #{a.class} given" unless a.is_a? ::Array
|
68
|
-
|
69
|
-
@a = a
|
70
|
-
end
|
71
|
-
|
72
|
-
# Calls the block once for each element in the array
|
73
|
-
def each
|
74
|
-
|
75
|
-
return @a.each unless block_given?
|
76
|
-
|
77
|
-
@a.each { |i| yield i }
|
78
|
-
end
|
79
|
-
|
80
|
-
# Alias for +length+
|
81
|
-
def size
|
82
|
-
|
83
|
-
@a.size
|
84
|
-
end
|
85
|
-
|
86
|
-
# Indicates whether the immutable array has no elements
|
87
|
-
def empty?
|
88
|
-
|
89
|
-
@a.empty?
|
90
|
-
end
|
91
|
-
|
92
|
-
# Same semantics as +Enumerable#find+ for the underlying array
|
93
|
-
def find ifnone = nil
|
94
|
-
|
95
|
-
return @a.find(ifnone) { |o| yield o } if block_given?
|
96
|
-
|
97
|
-
@a.find ifnone
|
98
|
-
end
|
99
|
-
|
100
|
-
# The number of elements in the immutable array
|
101
|
-
def length
|
102
|
-
|
103
|
-
@a.length
|
104
|
-
end
|
105
|
-
|
106
|
-
# Same semantics as +Array#slice+
|
107
|
-
def slice *args
|
108
|
-
|
109
|
-
case args.length
|
110
|
-
when 1
|
111
|
-
case args[0]
|
112
|
-
when ::Integer
|
113
|
-
return @a.slice args[0]
|
114
|
-
end
|
115
|
-
when 2
|
116
|
-
else
|
117
|
-
end
|
118
|
-
|
119
|
-
self.class.new @a.slice(*args)
|
120
|
-
end
|
121
|
-
|
122
|
-
# Same semantics as +Array#[]+
|
123
|
-
def [] *args
|
124
|
-
|
125
|
-
slice(*args)
|
126
|
-
end
|
127
|
-
|
128
|
-
# Determines whether +rhs+ is each to the receiver
|
129
|
-
def == rhs
|
130
|
-
|
131
|
-
return rhs == @a if rhs.is_a? self.class
|
132
|
-
|
133
|
-
@a == rhs
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
# ######################################################################## #
|
138
|
-
# module
|
139
|
-
|
140
|
-
end # module Util
|
141
|
-
end # module CLASP
|
142
|
-
|
143
|
-
# ######################################################################## #
|
144
|
-
# extensions
|
145
|
-
|
146
|
-
#:nodoc:
|
147
|
-
class Array
|
148
|
-
|
149
|
-
# Monkey-patched Array#== in order to handle comparison with
|
150
|
-
# ImmutableArray
|
151
|
-
#
|
152
|
-
# NOTE: do not do so for +eql?+
|
153
|
-
|
154
|
-
#:nodoc:
|
155
|
-
alias_method :old_equal, :==
|
156
|
-
|
157
|
-
undef :==
|
158
|
-
|
159
|
-
#:nodoc:
|
160
|
-
def ==(rhs)
|
161
|
-
|
162
|
-
return rhs == self if rhs.is_a? CLASP::Arguments::ImmutableArray
|
163
|
-
|
164
|
-
old_equal rhs
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
# ############################## end of file ############################# #
|
169
|
-
|
@@ -1,72 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
$:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
|
4
|
-
|
5
|
-
require 'clasp'
|
6
|
-
|
7
|
-
require 'test/unit'
|
8
|
-
|
9
|
-
class Test_ImmutableArray < Test::Unit::TestCase
|
10
|
-
|
11
|
-
ImmutableArray = ::CLASP::Arguments::ImmutableArray
|
12
|
-
|
13
|
-
def test_empty
|
14
|
-
|
15
|
-
ia = ImmutableArray.new []
|
16
|
-
|
17
|
-
assert ia.empty?
|
18
|
-
assert_equal 0, ia.size
|
19
|
-
assert_equal 0, ia.length
|
20
|
-
assert_equal 0, ia.count
|
21
|
-
assert_equal 0, ia.count(:abc)
|
22
|
-
assert_equal 0, ia.count { |o| !o.nil? }
|
23
|
-
assert (ia.find { |o| !o.nil? }).nil?
|
24
|
-
assert_equal ImmutableArray.new([]), ia
|
25
|
-
assert_not_equal ImmutableArray.new([ :def ]), ia
|
26
|
-
assert_not_equal ImmutableArray.new([ :abc, :def ]), ia
|
27
|
-
assert ia[0].nil?
|
28
|
-
assert ia[1].nil?
|
29
|
-
assert_equal ImmutableArray.new([]), ia[0..1]
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_one_item
|
33
|
-
|
34
|
-
ia = ImmutableArray.new [ :def ]
|
35
|
-
|
36
|
-
assert !ia.empty?
|
37
|
-
assert_equal 1, ia.size
|
38
|
-
assert_equal 1, ia.length
|
39
|
-
assert_equal 1, ia.count
|
40
|
-
assert_equal 0, ia.count(:abc)
|
41
|
-
assert_equal 1, ia.count { |o| !o.nil? }
|
42
|
-
assert !(ia.find { |o| !o.nil? }).nil?
|
43
|
-
assert_not_equal ImmutableArray.new([]), ia
|
44
|
-
assert_equal ImmutableArray.new([ :def ]), ia
|
45
|
-
assert_not_equal ImmutableArray.new([ :abc, :def ]), ia
|
46
|
-
assert_equal :def, ia[0]
|
47
|
-
assert ia[1].nil?
|
48
|
-
assert_equal ImmutableArray.new([ :def ]), ia[0..1]
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_two_items
|
52
|
-
|
53
|
-
ia = ImmutableArray.new [ :abc, :def ]
|
54
|
-
|
55
|
-
assert !ia.empty?
|
56
|
-
assert_equal 2, ia.size
|
57
|
-
assert_equal 2, ia.length
|
58
|
-
assert_equal 2, ia.count
|
59
|
-
assert_equal 1, ia.count(:abc)
|
60
|
-
assert_equal 2, ia.count { |o| !o.nil? }
|
61
|
-
assert !(ia.find { |o| !o.nil? }).nil?
|
62
|
-
assert_not_equal ImmutableArray.new([]), ia
|
63
|
-
assert_not_equal ImmutableArray.new([ :def ]), ia
|
64
|
-
assert_equal ImmutableArray.new([ :abc, :def ]), ia
|
65
|
-
assert_equal :abc, ia[0]
|
66
|
-
assert_equal :def, ia[1]
|
67
|
-
assert_equal ImmutableArray.new([ :abc, :def ]), ia[0..1]
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
# ############################## end of file ############################# #
|
72
|
-
|
@@ -1,72 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
$:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
|
4
|
-
|
5
|
-
require 'clasp'
|
6
|
-
|
7
|
-
require 'test/unit'
|
8
|
-
|
9
|
-
class Test_Util_ImmutableArray < Test::Unit::TestCase
|
10
|
-
|
11
|
-
include ::CLASP::Util
|
12
|
-
|
13
|
-
def test_empty
|
14
|
-
|
15
|
-
ia = ImmutableArray.new []
|
16
|
-
|
17
|
-
assert ia.empty?
|
18
|
-
assert_equal 0, ia.size
|
19
|
-
assert_equal 0, ia.length
|
20
|
-
assert_equal 0, ia.count
|
21
|
-
assert_equal 0, ia.count(:abc)
|
22
|
-
assert_equal 0, ia.count { |o| !o.nil? }
|
23
|
-
assert (ia.find { |o| !o.nil? }).nil?
|
24
|
-
assert_equal ImmutableArray.new([]), ia
|
25
|
-
assert_not_equal ImmutableArray.new([ :def ]), ia
|
26
|
-
assert_not_equal ImmutableArray.new([ :abc, :def ]), ia
|
27
|
-
assert ia[0].nil?
|
28
|
-
assert ia[1].nil?
|
29
|
-
assert_equal ImmutableArray.new([]), ia[0..1]
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_one_item
|
33
|
-
|
34
|
-
ia = ImmutableArray.new [ :def ]
|
35
|
-
|
36
|
-
assert !ia.empty?
|
37
|
-
assert_equal 1, ia.size
|
38
|
-
assert_equal 1, ia.length
|
39
|
-
assert_equal 1, ia.count
|
40
|
-
assert_equal 0, ia.count(:abc)
|
41
|
-
assert_equal 1, ia.count { |o| !o.nil? }
|
42
|
-
assert !(ia.find { |o| !o.nil? }).nil?
|
43
|
-
assert_not_equal ImmutableArray.new([]), ia
|
44
|
-
assert_equal ImmutableArray.new([ :def ]), ia
|
45
|
-
assert_not_equal ImmutableArray.new([ :abc, :def ]), ia
|
46
|
-
assert_equal :def, ia[0]
|
47
|
-
assert ia[1].nil?
|
48
|
-
assert_equal ImmutableArray.new([ :def ]), ia[0..1]
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_two_items
|
52
|
-
|
53
|
-
ia = ImmutableArray.new [ :abc, :def ]
|
54
|
-
|
55
|
-
assert !ia.empty?
|
56
|
-
assert_equal 2, ia.size
|
57
|
-
assert_equal 2, ia.length
|
58
|
-
assert_equal 2, ia.count
|
59
|
-
assert_equal 1, ia.count(:abc)
|
60
|
-
assert_equal 2, ia.count { |o| !o.nil? }
|
61
|
-
assert !(ia.find { |o| !o.nil? }).nil?
|
62
|
-
assert_not_equal ImmutableArray.new([]), ia
|
63
|
-
assert_not_equal ImmutableArray.new([ :def ]), ia
|
64
|
-
assert_equal ImmutableArray.new([ :abc, :def ]), ia
|
65
|
-
assert_equal :abc, ia[0]
|
66
|
-
assert_equal :def, ia[1]
|
67
|
-
assert_equal ImmutableArray.new([ :abc, :def ]), ia[0..1]
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
# ############################## end of file ############################# #
|
72
|
-
|