clasp-ruby 0.10.2

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.
@@ -0,0 +1,54 @@
1
+
2
+ # ######################################################################## #
3
+ # File: clasp/clasp.rb
4
+ #
5
+ # Purpose: Common 'require file' for CLASP.Ruby library
6
+ #
7
+ # Created: 14th February 2014
8
+ # Updated: 16th April 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
+ require 'clasp/arguments'
49
+ require 'clasp/aliases'
50
+ require 'clasp/cli'
51
+ require 'clasp/version'
52
+
53
+ # ############################## end of file ############################# #
54
+
data/lib/clasp/cli.rb ADDED
@@ -0,0 +1,264 @@
1
+
2
+ # ######################################################################## #
3
+ # File: clasp/cli.rb
4
+ #
5
+ # Purpose: Command-line interface
6
+ #
7
+ # Created: 27th July 2015
8
+ # Updated: 11th June 2016
9
+ #
10
+ # Home: http://github.com/synesissoftware/CLASP.Ruby
11
+ #
12
+ # Author: Matthew Wilson
13
+ #
14
+ # Copyright (c) 2015-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
+ # module
50
+
51
+ =begin
52
+ =end
53
+
54
+ module CLASP
55
+
56
+ # ######################################################################## #
57
+ # helpers
58
+
59
+ =begin
60
+ =end
61
+
62
+ # :stopdoc:
63
+ module CLI_helpers_
64
+
65
+ # :nodoc:
66
+ def self.generate_version_string_ options
67
+
68
+ program_name = options[:program_name] || File.basename($0)
69
+
70
+ version_prefix = options[:version_prefix]
71
+
72
+ if options[:version]
73
+
74
+ case options[:version]
75
+ when ::Array
76
+ version = options[:version].join('.')
77
+ else
78
+ version = options[:version]
79
+ end
80
+ else
81
+
82
+ version_major = options[:version_major] or raise ArgumentError, "options must specify :version or :version_major [ + :version_minor [ + :version_revision [ + :version_build ]]]"
83
+ version_minor = options[:version_minor]
84
+ version_rev = options[:version_revision]
85
+ version_build = options[:version_build]
86
+
87
+ version = version_major.to_s
88
+ version += ".#{version_minor}" if version_minor
89
+ version += ".#{version_rev}" if version_rev
90
+ version += ".#{version_build}" if version_build
91
+ end
92
+
93
+ "#{program_name} #{version_prefix}#{version}"
94
+ end
95
+
96
+ end # module CLI_helpers_
97
+
98
+ # ######################################################################## #
99
+ # methods
100
+
101
+ =begin
102
+ =end
103
+
104
+ # :startdoc:
105
+
106
+ # Displays usage for the program according to the given aliases and options
107
+ #
108
+ # === Signature
109
+ #
110
+ # * *Parameters*:
111
+ # - +aliases+:: (+Array+) The arguments array. May not be +nil+. Defaults to +ARGV+.
112
+ # - +options+:: An options hash, containing any of the following options.
113
+ #
114
+ # * *Options*:
115
+ # - +:exit+:: a program exit code; <tt>exit()</tt> not called if not specified (or +nil+).
116
+ # - +:program_name+:: program name; inferred from <tt>$0</tt> if not specified.
117
+ # - +:stream+:: output stream; <tt>$stdout</tt> if not specified.
118
+ # - +:suppress_blank_lines_between_options+:: does exactly what it says on the tin.
119
+ # - +:values+:: appends this string to USAGE line if specified.
120
+ # - +:flags_and_options+:: inserts a custom string instead of the default string <tt>'[ ... flags and options ... ]'</tt>.
121
+ # - +:info_lines+:: inserts 0+ information lines prior to the usage.
122
+ def self.show_usage aliases, options={}
123
+
124
+ options ||= {}
125
+
126
+ raise ArgumentError, "aliases may not be nil" if aliases.nil?
127
+ raise TypeError, "aliases must be an array or must respond to each, reject and select" unless ::Array === aliases || (aliases.respond_to?(:each) && aliases.respond_to?(:reject) && aliases.respond_to?(:select))
128
+
129
+ aliases.each { |a| raise TypeError, "each element in aliases must be a Flag or an Option" unless a.is_a?(::CLASP::Flag) || a.is_a?(::CLASP::Option) }
130
+
131
+ alias_dups = {}
132
+ aliases.each { |a| a.aliases.each { |aa| warn "WARNING: alias '#{aa}' is already used for alias '#{a}'" if alias_dups.has_key? aa; alias_dups[aa] = a; } }
133
+
134
+ suppress_blanks = options[:suppress_blank_lines_between_options] || ENV['SUPPRESS_BLANK_LINES_BETWEEN_OPTIONS']
135
+
136
+ stream = options[:stream] || $stdout
137
+ program_name = options[:program_name] || File.basename($0)
138
+
139
+ info_lines = options[:info_lines]
140
+ case info_lines
141
+ when ::Array
142
+ ;
143
+ when ::NilClass
144
+ info_lines = []
145
+ else
146
+ info_lines = [ info_lines ] unless [ :each, :empty? ].all? { |m| info_lines.respond_to? m }
147
+ end
148
+ info_lines.map! do |line|
149
+
150
+ case line
151
+ when :version
152
+
153
+ CLI_helpers_.generate_version_string_ options
154
+ else
155
+
156
+ line
157
+ end
158
+ end
159
+
160
+ values = options[:values] || ''
161
+ values = " #{values}" if !values.empty? && ' ' != values[0]
162
+
163
+ flags_and_options = options[:flags_and_options] || ' [ ... flags and options ... ]'
164
+ flags_and_options = " #{flags_and_options}" if !flags_and_options.empty? && ' ' != flags_and_options[0]
165
+
166
+ # sift the aliases to sort out which are value-option aliases (VOAs)
167
+
168
+ voas = {}
169
+
170
+ aliases.select { |a| a.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ }.each do |a|
171
+
172
+ a.name =~ /^(-+[a-zA-Z0-3_-]+)[=:](.+)$/
173
+
174
+ voas[$1] = [] unless voas.has_key? $1
175
+ voas[$1] << [ a, $2 ]
176
+ end
177
+
178
+ aliases = aliases.reject { |a| a.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ }
179
+
180
+ info_lines.each { |info_line| stream.puts info_line } unless info_lines.empty?
181
+
182
+ stream.puts "USAGE: #{program_name}#{flags_and_options}#{values}"
183
+ stream.puts
184
+
185
+ unless aliases.empty?
186
+ stream.puts "flags/options:"
187
+ stream.puts
188
+ aliases.each do |a|
189
+
190
+ case a
191
+ when Flag
192
+
193
+ a.aliases.each { |al| stream.puts "\t#{al}" }
194
+ stream.puts "\t#{a.name}"
195
+ stream.puts "\t\t#{a.help}"
196
+ when Option
197
+
198
+ if voas.has_key? a.name
199
+
200
+ voas[a.name].each do |ar|
201
+
202
+ ar[0].aliases.each { |al| stream.puts "\t#{al} #{ar[0].name}" }
203
+ end
204
+ end
205
+ a.aliases.each { |al| stream.puts "\t#{al} <value>" }
206
+ stream.puts "\t#{a.name}=<value>"
207
+ stream.puts "\t\t#{a.help}"
208
+ unless a.values_range.empty?
209
+
210
+ stream.puts "\t\twhere <value> one of:"
211
+ a.values_range.each { |v| stream.puts "\t\t\t#{v}" }
212
+ end
213
+ end
214
+ stream.puts unless suppress_blanks
215
+ end
216
+ end
217
+
218
+ exit options[:exit] if options[:exit]
219
+ end
220
+
221
+ # Displays version for the program according to the given aliases and options
222
+ #
223
+ # === Signature
224
+ #
225
+ # * *Parameters*:
226
+ # - +aliases+:: (+Array+) The arguments array. May not be +nil+. Defaults to +ARGV+.
227
+ # - +options+:: An options hash, containing any of the following options.
228
+ #
229
+ # * *Options*:
230
+ # - +:exit+:: a program exit code; <tt>exit()</tt> not called if not specified (or +nil+).
231
+ # - +:program_name+:: program name; inferred from <tt>$0</tt> if not specified.
232
+ # - +:stream+:: output stream; <tt>$stdout</tt> if not specified.
233
+ # - +:version+:: an array (of N elements, each of which will be separated by a period '.'), or a string. Must be specified if not +:version_major+.
234
+ # - +:version_major+:: a number or string. Only considered and must be specified if +:version+ is not.
235
+ # - +:version_minor+:: a number or string. Only considered if +:version+ is not.
236
+ # - +:version_revision+:: a number or string. Only considered if +:version+ is not.
237
+ # - +:version_build+:: a number or string. Only considered if +:version+ is not.
238
+ # - +:version_prefix+:: optional string to prefix the version number(s).
239
+ def self.show_version aliases, options = {}
240
+
241
+ options ||= {}
242
+
243
+ raise ArgumentError, "aliases may not be nil" if aliases.nil?
244
+ raise TypeError, "aliases must be an array or must respond to each, reject and select" unless ::Array === aliases || (aliases.respond_to?(:each) && aliases.respond_to?(:reject) && aliases.respond_to?(:select))
245
+
246
+ aliases.each { |a| raise TypeError, "each element in aliases must be a Flag or an Option" unless a.is_a?(::CLASP::Flag) || a.is_a?(::CLASP::Option) }
247
+
248
+ stream = options[:stream] || $stdout
249
+
250
+ version_string = CLI_helpers_.generate_version_string_ options
251
+
252
+ stream.puts version_string
253
+
254
+ exit options[:exit] if options[:exit]
255
+ end
256
+
257
+ # ######################################################################## #
258
+ # module
259
+
260
+ end # module CLASP
261
+
262
+ # ############################## end of file ############################# #
263
+
264
+
data/lib/clasp/doc_.rb ADDED
@@ -0,0 +1,151 @@
1
+
2
+ # ######################################################################## #
3
+ # File: lib/xqsr3/doc_.rb
4
+ #
5
+ # Purpose: Documentation of the CLASP.Ruby modules
6
+ #
7
+ # Created: 11th June 2016
8
+ # Updated: 11th June 2016
9
+ #
10
+ # Home: http://github.com/synesissoftware/xqsr3
11
+ #
12
+ # Author: Matthew Wilson
13
+ #
14
+ # Copyright (c) 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 notice,
22
+ # 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
+ =begin
48
+ =end
49
+
50
+ # Main module for CLASP.Ruby library
51
+ #
52
+ # === Examples
53
+ #
54
+ # ==== Simple command-line, no aliases
55
+ #
56
+ # argv = %w{ --show-all=true infile -c outfile }
57
+ #
58
+ # args = CLASP::Arguments.new(argv)
59
+ #
60
+ # puts args.flags.size # => 1
61
+ # puts args.flags[0] # => "#<CLASP::Arguments::Flag:0x007fd23aa3aa98 @arg="-c", @given_index=1, @given_name="-c", @argument_alias=nil, @given_hyphens=1, @given_label="c", @name="-c", @extras={}>"
62
+ # puts args.options.size # => 1
63
+ # puts args.options[0] # => "#<CLASP::Arguments::Option: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
+ # puts args.options[0].value # => "true"
65
+ # puts args.values.size # => 2
66
+ # puts args.values[0] # => "infile"
67
+ # puts args.values[1] # => "outfile"
68
+ #
69
+ # ==== Use of the special double-slash flag to treat all subsequent arguments as values
70
+ #
71
+ # argv = %w{ --show-all=true -- infile outfile -c }
72
+ #
73
+ # args = CLASP::Arguments.new(argv)
74
+ #
75
+ # puts args.flags.size # => 0
76
+ # puts args.options.size # => 1
77
+ # puts args.options[0] # => "#<CLASP::Arguments::Option: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
+ # puts args.values.size # => 3
79
+ # puts args.values[0] # => "infile"
80
+ # puts args.values[1] # => "outfile"
81
+ # puts args.values[2] # => "-c"
82
+ #
83
+ # ==== Use of flag short forms
84
+ #
85
+ # aliases = [
86
+ #
87
+ # CLASP.Flag('--verbose', alias: '-v'),
88
+ # CLASP.Flag('--trace-output', aliases: [ '-t', '--trace' ]),
89
+ # ]
90
+ #
91
+ # argv = %w{ -trace -v }
92
+ #
93
+ # args = CLASP::Arguments.new(argv, aliases)
94
+ #
95
+ # puts args.flags.size # => 2
96
+ # puts args.flags[0].name # => "--trace-output"
97
+ # puts args.flags[1].name # => "--verbose"
98
+ # puts args.options.size # => 0
99
+ # puts args.values.size # => 0
100
+ #
101
+ # ==== Use of flag single short forms combined
102
+ #
103
+ # aliases = [
104
+ #
105
+ # CLASP.Flag('--expand', alias: '-x'),
106
+ # CLASP.Flag('--verbose', alias: '-v'),
107
+ # CLASP.Flag('--trace-output', aliases: [ '-t', '--trace' ]),
108
+ # ]
109
+ #
110
+ # argv = %w{ -tvx }
111
+ #
112
+ # args = CLASP::Arguments.new(argv, aliases)
113
+ #
114
+ # puts args.flags.size # => 3
115
+ # puts args.options.size # => 0
116
+ # puts args.values.size # => 0
117
+ #
118
+ # ==== Use of option short form
119
+ #
120
+ # aliases = [
121
+ #
122
+ # CLASP.Option('--show-all', alias: '-a'),
123
+ # ]
124
+ #
125
+ # argv = %w{ -c -a true infile outfile }
126
+ #
127
+ # args = CLASP::Arguments.new(argv, aliases)
128
+ #
129
+ # puts args.flags.size # => 1
130
+ # puts args.flags[0] # => "#<CLASP::Arguments::Flag:0x007f8593b0ddd8 @arg="-c", @given_index=0, @given_name="-c", @argument_alias=nil, @given_hyphens=1, @given_label="c", @name="-c", @extras={}>"
131
+ # puts args.options.size # => 1
132
+ # puts args.options[0] # => "#<CLASP::Arguments::Option:0x007f8593b0db80 @arg="-a", @given_index=1, @given_name="-a", @argument_alias=#<CLASP::Option: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
+ # puts args.values.size # => 2
134
+ # puts args.values[0] # => "infile"
135
+ # puts args.values[1] # => "outfile"
136
+ #
137
+ # === Classes of interest
138
+ # * ::CLASP::Arguments
139
+ # * ::CLASP::Flag
140
+ # * ::CLASP::Option
141
+ #
142
+ # === Functions of interest
143
+ # * ::CLASP.show_version()
144
+ # * ::CLASP#show_version()
145
+ # * ::CLASP::show_version()
146
+ module CLASP
147
+
148
+ end # module CLASP
149
+
150
+ # ############################## end of file ############################# #
151
+