clasp-ruby 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +30 -0
- data/README.md +186 -0
- data/examples/cr-example.rb +63 -0
- data/lib/clasp-ruby.rb +51 -0
- data/lib/clasp.rb +51 -0
- data/lib/clasp/aliases.rb +256 -0
- data/lib/clasp/arguments.rb +467 -0
- data/lib/clasp/clasp.rb +54 -0
- data/lib/clasp/cli.rb +264 -0
- data/lib/clasp/doc_.rb +151 -0
- data/lib/clasp/old_module.rb +54 -0
- data/lib/clasp/version.rb +69 -0
- data/test/scratch/test_aliases.rb +38 -0
- data/test/scratch/test_list_command_line.rb +37 -0
- data/test/scratch/test_usage.rb +32 -0
- data/test/scratch/test_usage_with_duplicate_aliases.rb +30 -0
- data/test/unit/tc_ARGV_rewrite.rb +61 -0
- data/test/unit/tc_aliases.rb +75 -0
- data/test/unit/tc_arguments_1.rb +684 -0
- data/test/unit/tc_arguments_2.rb +65 -0
- data/test/unit/tc_arguments_3.rb +89 -0
- data/test/unit/tc_defaults_1.rb +73 -0
- data/test/unit/tc_examples_Arguments.rb +149 -0
- data/test/unit/tc_usage.rb +110 -0
- data/test/unit/ts_all.rb +13 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d272de686d9352ca5f798e3235b78a1b2711516e
|
4
|
+
data.tar.gz: 5b40f0eb1d2ec82edd4c04ae06457b09d5e7bce1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d61b6ece12b65f7fd2fe310f092468006ed8af5dca6ccc2db06ce6d3fc1e5205869ab53379d0ef270bf65b6291e870905bab19773e1e8b363472acf2e8e73d72
|
7
|
+
data.tar.gz: 67c1a12d8fd6389712d03e4ca48360bf6bbf6b3d445e54792112d9ddb0bb8915b6c951b2fa1c679b58ebcd87501eb70dbf3c8dff7a795d82f2c7bcc8e8e1ff9b
|
data/LICENSE
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
CLASP.Ruby
|
2
|
+
|
3
|
+
Copyright (c) 2008-2016, Matthew Wilson and Synesis Software
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
10
|
+
* list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
14
|
+
* and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
* Neither the names of the copyright holder nor the names of its contributors
|
17
|
+
* may be used to endorse or promote products derived from this software without
|
18
|
+
* specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
21
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
data/README.md
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
# CLASP.Ruby
|
2
|
+
|
3
|
+
## Installation & usage
|
4
|
+
|
5
|
+
Install using `gem install clasp-ruby` or add it to your `Gemfile`.
|
6
|
+
|
7
|
+
## Description
|
8
|
+
|
9
|
+
**CLASP** stands for **C**\ommand-**L**\ine **A**\rgument **S**\orting and
|
10
|
+
**P**\arsing. The first \CLASP library was a C library with a C++ wrapper. There
|
11
|
+
have been several implementations in other languages. **CLASP.Ruby** is the
|
12
|
+
Ruby version.
|
13
|
+
|
14
|
+
All \CLASP libraries provide the following facilities to **C**\ommand **L**\ine
|
15
|
+
**I**\nterface (**CLI**) programs:
|
16
|
+
|
17
|
+
### Command-line parsing
|
18
|
+
|
19
|
+
All **CLASP** libraries discriminate between three types of command-line arguments:
|
20
|
+
|
21
|
+
* *flags* are hyphen-prefixed arguments that are either present or absent, and hence have a boolean nature;
|
22
|
+
* *options* are hyphen-prefixed arguments that are given values; and
|
23
|
+
* *values* are non-hyphen-prefixed arguments that represent values.
|
24
|
+
|
25
|
+
For example, in the command line
|
26
|
+
|
27
|
+
`myprog --all -c --opt1=val1 infile outfile`
|
28
|
+
|
29
|
+
there are:
|
30
|
+
|
31
|
+
* two *flags*, `--all` and `-c`;
|
32
|
+
* one *option* called `--opt1`, which has the value `val1`; and
|
33
|
+
* two *values* `infile` and `outfile`.
|
34
|
+
|
35
|
+
*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:
|
36
|
+
|
37
|
+
`myprog -a -c -o val1 infile outfile`
|
38
|
+
|
39
|
+
One-letter *flags* may be combined. Hence, the following command-line is exactly equivalent to the previous ones:
|
40
|
+
|
41
|
+
`myprog -ac -o val1 infile outfile`
|
42
|
+
|
43
|
+
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:
|
44
|
+
|
45
|
+
`myprog -ac -v1 infile outfile`
|
46
|
+
|
47
|
+
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:
|
48
|
+
|
49
|
+
`myprog -acv infile outfile`
|
50
|
+
|
51
|
+
UNIX standard arguments confer specific meanings:
|
52
|
+
|
53
|
+
* `--help` means that the program should show the usage/help information and terminate;
|
54
|
+
* `--version` means that the program should show the version information and terminate;
|
55
|
+
* `--` means that all subsequent arguments should be treated as values, regardless of any hyphen-prefixes or embedded `=` signs.
|
56
|
+
|
57
|
+
### Declarative specification of the flags and options for a CLI
|
58
|
+
|
59
|
+
To support such above special processing, \CLASP libraries provide facilities
|
60
|
+
for declarative specification of command-line *flags* and *options*, and
|
61
|
+
aliases thereof. For the previous example, the **CLASP.Ruby** code would look
|
62
|
+
like the following:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
|
66
|
+
# file: cr-example.rb
|
67
|
+
|
68
|
+
PROGRAM_VERSION = '0.1.2'
|
69
|
+
|
70
|
+
Aliases = [
|
71
|
+
|
72
|
+
CLASP.Flag('--all', alias: '-a', help: 'processes all item types'),
|
73
|
+
CLASP.Flag('-c', help: 'count the processed items'),
|
74
|
+
CLASP.Option('--opt1', alias: '-o', help: 'an option of some kind', values_range: %w{ val1, val2 }),
|
75
|
+
CLASP.Option('--opt1=val1', alias: '-v'),
|
76
|
+
|
77
|
+
# see next section for why these two are here
|
78
|
+
CLASP::Flag.Help,
|
79
|
+
CLASP::Flag.Version,
|
80
|
+
]
|
81
|
+
|
82
|
+
# assuming the command-line `myprog -acv infile outfile`
|
83
|
+
Args = CLASP::Arguments.new(ARGV, Aliases)
|
84
|
+
|
85
|
+
puts Args.flags.size # => 2
|
86
|
+
puts Args.flags[0].name # => "--all"
|
87
|
+
puts Args.flags[1].name # => "-c"
|
88
|
+
|
89
|
+
puts Args.options.size # => 1
|
90
|
+
puts Args.options[0].name # => "--opt1"
|
91
|
+
puts Args.options[0].value # => "val1"
|
92
|
+
|
93
|
+
puts Args.values.size # => 2
|
94
|
+
puts Args.values[0] # => "infile"
|
95
|
+
puts Args.values[1] # => "outfile"
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
### Utility functions for displaying usage and version information
|
100
|
+
|
101
|
+
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:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
|
105
|
+
Args.flags.each do |f|
|
106
|
+
|
107
|
+
case f.name
|
108
|
+
when CLASP::Flag.Help.name
|
109
|
+
|
110
|
+
CLASP.show_usage(Aliases, exit: 0, values: '<input-file> <output-file>')
|
111
|
+
when CLASP::Flag.Version.name
|
112
|
+
|
113
|
+
CLASP.show_version(Aliases, exit: 0, version: PROGRAM_VERSION)
|
114
|
+
when '--all'
|
115
|
+
|
116
|
+
# do something appropriate to `--all`
|
117
|
+
|
118
|
+
. . .
|
119
|
+
|
120
|
+
```
|
121
|
+
|
122
|
+
Given the command
|
123
|
+
|
124
|
+
`./cr-example.rb --help`
|
125
|
+
|
126
|
+
then the program will output the following
|
127
|
+
|
128
|
+
```
|
129
|
+
USAGE: cr-example.rb [ ... flags and options ... ] <input-file> <output-file>
|
130
|
+
|
131
|
+
flags/options:
|
132
|
+
|
133
|
+
-a
|
134
|
+
--all
|
135
|
+
processes all item types
|
136
|
+
|
137
|
+
-c
|
138
|
+
count the processed items
|
139
|
+
|
140
|
+
-v --opt1=val1
|
141
|
+
-o <value>
|
142
|
+
--opt1=<value>
|
143
|
+
an option of some kind where <value> one of:
|
144
|
+
val1,
|
145
|
+
val2
|
146
|
+
|
147
|
+
--help
|
148
|
+
shows this help and terminates
|
149
|
+
|
150
|
+
--version
|
151
|
+
shows version and terminates
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
and given the command
|
156
|
+
|
157
|
+
`./cr-example.rb --version`
|
158
|
+
|
159
|
+
then the program will output the following
|
160
|
+
|
161
|
+
```
|
162
|
+
cr-example.rb 0.1.2
|
163
|
+
```
|
164
|
+
|
165
|
+
## Where to get help
|
166
|
+
|
167
|
+
[GitHub Page](https://github.com/synesissoftware/CLASP.Ruby "GitHub Page")
|
168
|
+
|
169
|
+
## Contribution guidelines
|
170
|
+
|
171
|
+
Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/CLASP.Ruby.
|
172
|
+
|
173
|
+
## Related projects
|
174
|
+
|
175
|
+
**CLASP.Ruby** is inspired by the [C/C++ CLASP library](https://github.com/synesissoftware/CLASP), which is documented in the articles:
|
176
|
+
|
177
|
+
* _An Introduction to \CLASP_, Matthew Wilson, [CVu](http://accu.org/index.php/journals/c77/), January 2012;
|
178
|
+
* _[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
|
179
|
+
* _[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.
|
180
|
+
|
181
|
+
**CLASP.Ruby** is used in the **[libCLImate.Ruby](https://github.com/synesissoftware/libCLImate.Ruby)** library.
|
182
|
+
|
183
|
+
## License
|
184
|
+
|
185
|
+
**CLASP.Ruby** is released under the 3-clause BSD license. See LICENSE for details.
|
186
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
#############################################################################
|
4
|
+
# File: examples/cr-example.rb
|
5
|
+
#
|
6
|
+
# Purpose: COMPLETE_ME
|
7
|
+
#
|
8
|
+
# Created: 11 06 2016
|
9
|
+
# Updated: 11 06 2016
|
10
|
+
#
|
11
|
+
# Author: Matthew Wilson
|
12
|
+
#
|
13
|
+
#############################################################################
|
14
|
+
|
15
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
16
|
+
|
17
|
+
require 'clasp'
|
18
|
+
|
19
|
+
PROGRAM_VERSION = '0.1.2'
|
20
|
+
|
21
|
+
Aliases = [
|
22
|
+
|
23
|
+
CLASP.Flag('--all', alias: '-a', help: 'processes all item types'),
|
24
|
+
CLASP.Flag('-c', help: 'count the processed items'),
|
25
|
+
CLASP.Option('--opt1', alias: '-o', help: 'an option of some kind', values_range: %w{ val1, val2 }),
|
26
|
+
CLASP.Option('--opt1=val1', alias: '-v'),
|
27
|
+
|
28
|
+
# see next section for why these two are here
|
29
|
+
CLASP::Flag.Help,
|
30
|
+
CLASP::Flag.Version,
|
31
|
+
]
|
32
|
+
|
33
|
+
Args = CLASP::Arguments.new(ARGV, Aliases)
|
34
|
+
|
35
|
+
Args.flags.each do |f|
|
36
|
+
|
37
|
+
case f.name
|
38
|
+
when CLASP::Flag.Help.name
|
39
|
+
|
40
|
+
CLASP.show_usage(Aliases, exit: 0, values: '<input-file> <output-file>')
|
41
|
+
when CLASP::Flag.Version.name
|
42
|
+
|
43
|
+
CLASP.show_version(Aliases, exit: 0, version: PROGRAM_VERSION)
|
44
|
+
when '--all'
|
45
|
+
|
46
|
+
;
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
puts Args.flags.size
|
51
|
+
puts Args.flags[0].name
|
52
|
+
puts Args.flags[1].name
|
53
|
+
puts
|
54
|
+
|
55
|
+
puts Args.options.size
|
56
|
+
puts Args.options[0].name
|
57
|
+
puts Args.options[0].value
|
58
|
+
puts
|
59
|
+
|
60
|
+
puts Args.values.size
|
61
|
+
puts Args.values[0]
|
62
|
+
puts Args.values[1]
|
63
|
+
puts
|
data/lib/clasp-ruby.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
|
2
|
+
# ######################################################################## #
|
3
|
+
# File: clasp-ruby.rb
|
4
|
+
#
|
5
|
+
# Purpose: Top-level source for CLASP.ruby library [alternate]
|
6
|
+
#
|
7
|
+
# Created: 13th October 2014
|
8
|
+
# Updated: 10th June 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
|
+
# clasp-ruby.rb -> clasp/clasp.rb
|
48
|
+
require 'clasp/clasp'
|
49
|
+
|
50
|
+
# ############################## end of file ############################# #
|
51
|
+
|
data/lib/clasp.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
|
2
|
+
# ######################################################################## #
|
3
|
+
# File: clasp.rb
|
4
|
+
#
|
5
|
+
# Purpose: Top-level source for CLASP.ruby library
|
6
|
+
#
|
7
|
+
# Created: 13th October 2014
|
8
|
+
# Updated: 10th June 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
|
+
# clasp.rb -> clasp/clasp.rb
|
48
|
+
require 'clasp/clasp'
|
49
|
+
|
50
|
+
# ############################## end of file ############################# #
|
51
|
+
|
@@ -0,0 +1,256 @@
|
|
1
|
+
|
2
|
+
# ######################################################################## #
|
3
|
+
# File: clasp/aliases.rb
|
4
|
+
#
|
5
|
+
# Purpose: Alias classes
|
6
|
+
#
|
7
|
+
# Created: 25th October 2014
|
8
|
+
# Updated: 10th June 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
|
+
# ######################################################################## #
|
50
|
+
# module
|
51
|
+
|
52
|
+
=begin
|
53
|
+
=end
|
54
|
+
|
55
|
+
module CLASP
|
56
|
+
|
57
|
+
# ######################################################################## #
|
58
|
+
# classes
|
59
|
+
|
60
|
+
# A class that represents the specification for a command-line flag
|
61
|
+
class Flag
|
62
|
+
|
63
|
+
# Creates a Flag instance from the given name, aliases, and help
|
64
|
+
#
|
65
|
+
# === Signature
|
66
|
+
#
|
67
|
+
# * *Parameters*
|
68
|
+
# - +name+:: (+String+) The name, or long-form, of the flag.
|
69
|
+
# - +aliases+:: (+Array+) 0 or more strings specifying short-form or option-value aliases.
|
70
|
+
# - +help+:: (+String+) The help string, which may be +nil+.
|
71
|
+
# - +extras+:: An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+.
|
72
|
+
def initialize(name, aliases, help, extras = nil)
|
73
|
+
|
74
|
+
@name = name
|
75
|
+
@aliases = (aliases || []).select { |a| a and not a.empty? }
|
76
|
+
@help = help
|
77
|
+
@extras = extras || {}
|
78
|
+
end
|
79
|
+
|
80
|
+
# The flag's name string
|
81
|
+
attr_reader :name
|
82
|
+
# The flag's aliases array
|
83
|
+
attr_reader :aliases
|
84
|
+
# The flag's help string
|
85
|
+
attr_reader :help
|
86
|
+
# The flag's extras
|
87
|
+
attr_reader :extras
|
88
|
+
|
89
|
+
# String form of the flag
|
90
|
+
def to_s
|
91
|
+
|
92
|
+
"{#{name}; aliases=#{aliases.join(', ')}; help='#{help}'; extras=#{extras}}"
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
@@Help_ = self.new('--help', [], 'shows this help and terminates')
|
97
|
+
@@Version_ = self.new('--version', [], 'shows version and terminates')
|
98
|
+
public
|
99
|
+
# An instance of Flag that provides default '--help' information
|
100
|
+
def self.Help(extras = nil)
|
101
|
+
|
102
|
+
h = @@Help_
|
103
|
+
|
104
|
+
return self.new(h.name, h.aliases, h.help, extras) if extras
|
105
|
+
|
106
|
+
h
|
107
|
+
end
|
108
|
+
|
109
|
+
# An instance of Flag that provides default '--version' information
|
110
|
+
def self.Version(extras = nil)
|
111
|
+
|
112
|
+
h = @@Version_
|
113
|
+
|
114
|
+
return self.new(h.name, h.aliases, h.help, extras) if extras
|
115
|
+
|
116
|
+
h
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# A class that represents the specification for a command-line option
|
121
|
+
class Option
|
122
|
+
|
123
|
+
# Creates an Option instance from the given name, aliases, help,
|
124
|
+
# values_range, and default_value
|
125
|
+
#
|
126
|
+
# === Signature
|
127
|
+
#
|
128
|
+
# * *Parameters*
|
129
|
+
# - +name+:: (+String+) The name, or long-form, of the option.
|
130
|
+
# - +aliases+:: (+Array+) 0 or more strings specifying short-form or option-value aliases.
|
131
|
+
# - +help+:: (+String+) The help string, which may be +nil+.
|
132
|
+
# - +values_range+:: (+Array+) 0 or more strings specifying values supported by the option.
|
133
|
+
# - +default_value+:: (+String+) The default value of the option. May be +nil+.
|
134
|
+
# - +extras+:: An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+.
|
135
|
+
def initialize(name, aliases, help, values_range, default_value, extras = nil)
|
136
|
+
|
137
|
+
@name = name
|
138
|
+
@aliases = (aliases || []).select { |a| a and not a.empty? }
|
139
|
+
@help = help
|
140
|
+
@values_range = values_range || []
|
141
|
+
@default_value = default_value
|
142
|
+
@extras = extras || {}
|
143
|
+
end
|
144
|
+
|
145
|
+
# The option's name string
|
146
|
+
attr_reader :name
|
147
|
+
# The option's aliases array
|
148
|
+
attr_reader :aliases
|
149
|
+
# The option's help string
|
150
|
+
attr_reader :help
|
151
|
+
# The range of values supported by the option
|
152
|
+
attr_reader :values_range
|
153
|
+
# The default value of the option
|
154
|
+
attr_reader :default_value
|
155
|
+
# The flag's extras
|
156
|
+
attr_reader :extras
|
157
|
+
|
158
|
+
# String form of the option
|
159
|
+
def to_s
|
160
|
+
|
161
|
+
"{#{name}; aliases=#{aliases.join(', ')}; values_range=[ #{values_range.join(', ')} ]; default_value='#{default_value}'; help='#{help}'; extras=#{extras}}"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# ######################################################################## #
|
166
|
+
# functions
|
167
|
+
|
168
|
+
# Generator method that obtains a CLASP::Flag according to the given parameters
|
169
|
+
def CLASP.Flag(name, options = {})
|
170
|
+
|
171
|
+
aliases = nil
|
172
|
+
help = nil
|
173
|
+
extras = nil
|
174
|
+
|
175
|
+
options.each do |k, v|
|
176
|
+
|
177
|
+
case k
|
178
|
+
when Symbol
|
179
|
+
case k
|
180
|
+
when :alias
|
181
|
+
|
182
|
+
aliases = [ v ]
|
183
|
+
when :aliases
|
184
|
+
|
185
|
+
aliases = v
|
186
|
+
when :help
|
187
|
+
|
188
|
+
help = v
|
189
|
+
when :extras
|
190
|
+
|
191
|
+
extras = v
|
192
|
+
else
|
193
|
+
|
194
|
+
raise ArgumentError, "invalid option for flag: '#{k}' => '#{v}'"
|
195
|
+
end
|
196
|
+
else
|
197
|
+
|
198
|
+
raise ArgumentError, "invalid option type for flag: '#{k}' (#{k.class}) => '#{v}'"
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
CLASP::Flag.new(name, aliases, help, extras)
|
203
|
+
end
|
204
|
+
|
205
|
+
# Generator method that obtains a CLASP::Option according to the given parameters
|
206
|
+
def CLASP.Option(name, options = {})
|
207
|
+
|
208
|
+
aliases = nil
|
209
|
+
help = nil
|
210
|
+
values_range = nil
|
211
|
+
default_value = nil
|
212
|
+
extras = nil
|
213
|
+
|
214
|
+
options.each do |k, v|
|
215
|
+
|
216
|
+
case k
|
217
|
+
when Symbol
|
218
|
+
case k
|
219
|
+
when :alias
|
220
|
+
|
221
|
+
aliases = [ v ]
|
222
|
+
when :aliases
|
223
|
+
|
224
|
+
aliases = v
|
225
|
+
when :help
|
226
|
+
|
227
|
+
help = v
|
228
|
+
when :values_range, :values
|
229
|
+
|
230
|
+
values_range = v
|
231
|
+
when :default_value, :default
|
232
|
+
|
233
|
+
default_value = v
|
234
|
+
when :extras
|
235
|
+
|
236
|
+
extras = v
|
237
|
+
else
|
238
|
+
|
239
|
+
raise ArgumentError, "invalid option for flag: '#{k}' => '#{v}'"
|
240
|
+
end
|
241
|
+
else
|
242
|
+
|
243
|
+
raise ArgumentError, "invalid option type for flag: '#{k}' (#{k.class}) => '#{v}'"
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
CLASP::Option.new(name, aliases, help, values_range, default_value, extras)
|
248
|
+
end
|
249
|
+
|
250
|
+
# ######################################################################## #
|
251
|
+
# module
|
252
|
+
|
253
|
+
end # module CLASP
|
254
|
+
|
255
|
+
# ############################## end of file ############################# #
|
256
|
+
|