libclimate-ruby 0.2.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02794e536b229ccb6f52ec08d7899389dd749dab
4
+ data.tar.gz: 28d0b8f7d6243ed083c8cfd85e1f7536bca6b7c5
5
+ SHA512:
6
+ metadata.gz: f6778014e31bfa9ce3024a4e09fdc59197091fd65ef85c75928ecda9cc0b4f5d67cd681b91f1e801f4d9a7a5f2dcbcd5807bfafc2bd4f39bf218ee8af6290d0c
7
+ data.tar.gz: 3d165c6a9f529123f3816bd719ce5dcd1bf9da583cdb9cc81c51aab89cbfef4e503e4de8611919e7e2cd8ef2131a66aa18808175e833307a1afc58ee26a5bfa3
data/LICENSE ADDED
@@ -0,0 +1,30 @@
1
+ libCLImate.Ruby
2
+
3
+ Copyright (c) 2015-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 libCLImate or libCLImate.Ruby nor the names of the
17
+ copyright holder nor the names of its contributors may be used to endorse or
18
+ promote products derived from this software without specific prior written
19
+ permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # libCLImate.Ruby
2
+ libCLImate for Ruby
3
+
4
+ ## Installation & usage
5
+
6
+ Install using `gem install libclimate-ruby` or add it to your `Gemfile`.
7
+
8
+ ## Description
9
+
10
+ TODO
11
+
12
+ ## Where to get help
13
+
14
+ [GitHub Page](https://github.com/synesissoftware/libCLImate.Ruby "GitHub Page")
15
+
16
+ ## Contribution guidelines
17
+
18
+ Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/libCLImate.Ruby.
19
+
20
+ ## Related projects
21
+
22
+ **libCLImate.Ruby** uses the **[clasp.Ruby](https://github.com/synesissoftware/clasp.Ruby)** and **[xqsr3](https://github.com/synesissoftware/xqsr3)** libraries.
23
+
24
+ ## License
25
+
26
+ **libCLImate.Ruby** is released under the 3-clause BSD license. See LICENSE for details.
data/lib/libclimate.rb ADDED
@@ -0,0 +1,43 @@
1
+ # ######################################################################## #
2
+ # File: libclimate.rb
3
+ #
4
+ # Purpose: Top-level include for libclimate.Ruby library
5
+ #
6
+ # Created: 13th July 2015
7
+ # Updated: 14th May 2016
8
+ #
9
+ # Home: http://github.com/synesissoftware/libCLImate.Ruby
10
+ #
11
+ # Copyright (c) 2015-2016, Matthew Wilson and Synesis Software
12
+ # All rights reserved.
13
+ #
14
+ # Redistribution and use in source and binary forms, with or without
15
+ # modification, are permitted provided that the following conditions are
16
+ # met:
17
+ #
18
+ # * Redistributions of source code must retain the above copyright notice,
19
+ # this list of conditions and the following disclaimer.
20
+ #
21
+ # * Redistributions in binary form must reproduce the above copyright
22
+ # notice, this list of conditions and the following disclaimer in the
23
+ # documentation and/or other materials provided with the distribution.
24
+ #
25
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
29
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
+ #
37
+ # ######################################################################## #
38
+
39
+
40
+ require 'libclimate/libclimate'
41
+
42
+ # ############################## end of file ############################# #
43
+
@@ -0,0 +1,251 @@
1
+
2
+ # ######################################################################## #
3
+ # File: lib/libclimate/climate.rb
4
+ #
5
+ # Purpose: Definition of the ::LibCLImate::Climate class
6
+ #
7
+ # Created: 13th July 2015
8
+ # Updated: 11th June 2016
9
+ #
10
+ # Home: http://github.com/synesissoftware/libCLImate.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 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
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
29
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
+ #
40
+ # ######################################################################## #
41
+
42
+
43
+ require 'clasp'
44
+ require 'xqsr3/extensions/io'
45
+
46
+ if !defined? Colcon
47
+
48
+ begin
49
+
50
+ require 'colcon'
51
+ rescue LoadError => x
52
+
53
+ warn "could not load colcon library" if $DEBUG
54
+ end
55
+ end
56
+
57
+ module LibCLImate
58
+
59
+ class Climate
60
+
61
+ private
62
+ def show_usage_
63
+
64
+ options = {}
65
+ options.merge! stream: stdout, program_name: program_name, version: version, exit: exit_on_usage ? 0 : nil
66
+ options[:info_lines] = info_lines if info_lines
67
+ options[:values] = usage_values if usage_values
68
+
69
+ CLASP.show_usage aliases, options
70
+ end
71
+
72
+ def show_version_
73
+
74
+ CLASP.show_version aliases, stream: stdout, program_name: program_name, version: version, exit: exit_on_usage ? 0 : nil
75
+ end
76
+
77
+ public
78
+ def initialize(options={})
79
+
80
+ options ||= {}
81
+
82
+ program_name = File.basename($0)
83
+ program_name = (program_name =~ /\.rb$/) ? "#$`(#$&)" : program_name
84
+
85
+ if defined? Colcon
86
+
87
+ program_name = "#{::Colcon::Decorations::Bold}#{program_name}#{::Colcon::Decorations::Unbold}"
88
+ end
89
+
90
+ @aliases = []
91
+ @exit_on_unknown = true
92
+ @exit_on_usage = true
93
+ @info_lines = nil
94
+ @program_name = program_name
95
+ @stdout = $stdout
96
+ @stderr = $stderr
97
+ @usage_values = usage_values
98
+ @version = []
99
+
100
+ @aliases << CLASP::Flag.Help(handle: proc { show_usage_ }) unless options[:no_help_flag]
101
+ @aliases << CLASP::Flag.Version(handle: proc { show_version_ }) unless options[:no_version_flag]
102
+
103
+ raise ArgumentError, "block is required" unless block_given?
104
+
105
+ yield self
106
+ end
107
+
108
+ attr_accessor :aliases
109
+ attr_accessor :exit_on_unknown
110
+ attr_accessor :exit_on_usage
111
+ attr_accessor :info_lines
112
+ attr_accessor :program_name
113
+ attr_accessor :stdout
114
+ attr_accessor :stderr
115
+ attr_accessor :usage_values
116
+ attr_accessor :version
117
+
118
+ def run argv = ARGV
119
+
120
+ raise ArgumentError, "argv may not be nil" if argv.nil?
121
+
122
+ arguments = CLASP::Arguments.new argv, aliases
123
+ flags = arguments.flags
124
+ options = arguments.options
125
+ values = arguments.values.to_a
126
+
127
+ results = {
128
+
129
+ flags: {
130
+
131
+ given: flags,
132
+ handled: [],
133
+ unhandled: [],
134
+ unknown: []
135
+ },
136
+
137
+ options: {
138
+
139
+ given: options,
140
+ handled: [],
141
+ unhandled: [],
142
+ unknown: []
143
+ },
144
+
145
+ values: values
146
+ }
147
+
148
+ flags.each do |f|
149
+
150
+ al = aliases.detect do |a|
151
+
152
+ a.kind_of?(::CLASP::Flag) && f.name == a.name
153
+ end
154
+
155
+ if al
156
+
157
+ selector = :unhandled
158
+
159
+ ex = f.extras
160
+
161
+ case f.extras
162
+ when ::Hash
163
+ if f.extras.has_key? :handle
164
+
165
+ f.extras[:handle].call(f, al)
166
+
167
+ selector = :handled
168
+ end
169
+ end
170
+
171
+ results[:flags][selector] << f
172
+ else
173
+
174
+ message = "#{program_name}: unrecognised flag '#{f}'; use --help for usage"
175
+
176
+ if exit_on_unknown
177
+
178
+ abort message
179
+ else
180
+
181
+ stderr.puts message
182
+ end
183
+
184
+ results[:flags][:unknown] << f
185
+ end
186
+ end
187
+
188
+ options.each do |o|
189
+
190
+ al = aliases.detect do |a|
191
+
192
+ a.kind_of?(::CLASP::Option) && o.name == a.name
193
+ end
194
+
195
+ if al
196
+
197
+ selector = :unhandled
198
+
199
+ ex = al.extras
200
+
201
+ case ex
202
+ when ::Hash
203
+ if ex.has_key? :handle
204
+
205
+ ex[:handle].call(o, al)
206
+
207
+ selector = :handled
208
+ end
209
+ end
210
+
211
+ results[:options][selector] << o
212
+ else
213
+
214
+ message = "#{program_name}: unrecognised option '#{o}'; use --help for usage"
215
+
216
+ if exit_on_unknown
217
+
218
+ abort message
219
+ else
220
+
221
+ stderr.puts message
222
+ end
223
+
224
+ results[:options][:unknown] << o
225
+ end
226
+ end
227
+
228
+ def results.flags
229
+
230
+ self[:flags]
231
+ end
232
+
233
+ def results.options
234
+
235
+ self[:options]
236
+ end
237
+
238
+ def results.values
239
+
240
+ self[:values]
241
+ end
242
+
243
+ results
244
+ end
245
+ end # class Climate
246
+
247
+ end # module LibCLImate
248
+
249
+ # ############################## end of file ############################# #
250
+
251
+
@@ -0,0 +1,50 @@
1
+ # ######################################################################## #
2
+ # File: libclimate/libclimate.rb
3
+ #
4
+ # Purpose: Main file for libclimate.Ruby library
5
+ #
6
+ # Created: 13th July 2015
7
+ # Updated: 14th May 2016
8
+ #
9
+ # Home: http://github.com/synesissoftware/libCLImate.Ruby
10
+ #
11
+ # Copyright (c) 2015-2016, Matthew Wilson and Synesis Software
12
+ # All rights reserved.
13
+ #
14
+ # Redistribution and use in source and binary forms, with or without
15
+ # modification, are permitted provided that the following conditions are
16
+ # met:
17
+ #
18
+ # * Redistributions of source code must retain the above copyright notice,
19
+ # this list of conditions and the following disclaimer.
20
+ #
21
+ # * Redistributions in binary form must reproduce the above copyright
22
+ # notice, this list of conditions and the following disclaimer in the
23
+ # documentation and/or other materials provided with the distribution.
24
+ #
25
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
29
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
+ #
37
+ # ######################################################################## #
38
+
39
+
40
+ require 'libclimate/climate'
41
+ require 'libclimate/version'
42
+
43
+ module LibCLImate
44
+
45
+
46
+ end # module LibCLImate
47
+
48
+ # ############################## end of file ############################# #
49
+
50
+
@@ -0,0 +1,61 @@
1
+ # ######################################################################## #
2
+ # File: libclimate/version.rb
3
+ #
4
+ # Purpose: Version for libclimate.Ruby library
5
+ #
6
+ # Created: 13th July 2015
7
+ # Updated: 11th June 2016
8
+ #
9
+ # Home: http://github.com/synesissoftware/libCLImate.Ruby
10
+ #
11
+ # Copyright (c) 2015-2016, Matthew Wilson and Synesis Software
12
+ # All rights reserved.
13
+ #
14
+ # Redistribution and use in source and binary forms, with or without
15
+ # modification, are permitted provided that the following conditions are
16
+ # met:
17
+ #
18
+ # * Redistributions of source code must retain the above copyright notice,
19
+ # this list of conditions and the following disclaimer.
20
+ #
21
+ # * Redistributions in binary form must reproduce the above copyright
22
+ # notice, this list of conditions and the following disclaimer in the
23
+ # documentation and/or other materials provided with the distribution.
24
+ #
25
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
29
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
+ #
37
+ # ######################################################################## #
38
+
39
+
40
+ # Main module for libCLImate.Ruby library
41
+ module LibCLImate
42
+
43
+ # Current version of the libCLImate.Ruby library
44
+ VERSION = '0.2.3'
45
+
46
+ private
47
+ VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
48
+ public
49
+ # Major version of the libCLImate.Ruby library
50
+ VERSION_MAJOR = VERSION_PARTS_[0] # :nodoc:
51
+ # # Minor version of the libCLImate.Ruby library
52
+ VERSION_MINOR = VERSION_PARTS_[1] # :nodoc:
53
+ # # Revision version of the libCLImate.Ruby library
54
+ VERSION_REVISION = VERSION_PARTS_[2] # :nodoc:
55
+ #
56
+
57
+ end # module LibCLImate
58
+
59
+ # ############################## end of file ############################# #
60
+
61
+
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/ruby
2
+
3
+ #############################################################################
4
+ # File: test/scratch/blankzeroes.rb
5
+ #
6
+ # Purpose: COMPLETE_ME
7
+ #
8
+ # Created: 14 05 2016
9
+ # Updated: 14 05 2016
10
+ #
11
+ # Author: Matthew Wilson
12
+ #
13
+ # Copyright: <<TBD>>
14
+ #
15
+ #############################################################################
16
+
17
+ $:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
18
+
19
+
20
+ require 'libclimate'
21
+
22
+ # ##########################################################
23
+ # constants
24
+
25
+ PROGRAM_VER_MAJOR = 0
26
+ PROGRAM_VER_MINOR = 1
27
+ PROGRAM_VER_REVISION = 2
28
+
29
+ # ##########################################################
30
+ # command-line parsing
31
+
32
+ LibCLImate::Climate.new do |climate|
33
+
34
+ climate.version = [ PROGRAM_VER_MAJOR, PROGRAM_VER_MINOR, PROGRAM_VER_REVISION ]
35
+ end.run
36
+
37
+ # ##########################################################
38
+ # main
39
+
40
+ $<.each_line do |line|
41
+
42
+ puts line.split(/\t/).map { |s| '0' == s ? '' : s }.join("\t")
43
+ end
44
+
45
+ # ############################## end of file ############################# #
46
+
@@ -0,0 +1,363 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # test minimal uses
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
6
+
7
+
8
+ require 'libclimate'
9
+
10
+ require 'xqsr3/extensions/test/unit'
11
+
12
+ require 'test/unit'
13
+
14
+ require 'stringio'
15
+
16
+ class Test_Climate_minimal < Test::Unit::TestCase
17
+
18
+ def test_no_arguments_no_mods
19
+
20
+ climate = LibCLImate::Climate.new do |climate|
21
+
22
+ ;
23
+ end
24
+
25
+ assert $stdout.equal? climate.stdout
26
+ assert $stderr.equal? climate.stderr
27
+ end
28
+
29
+ def test_no_arguments_set_streams
30
+
31
+ climate = LibCLImate::Climate.new do |climate|
32
+
33
+ climate.stdout = $stdout
34
+ climate.stderr = $stderr
35
+ end
36
+
37
+ assert $stdout.equal? climate.stdout
38
+ assert $stderr.equal? climate.stderr
39
+ end
40
+
41
+ def test_help_to_string
42
+
43
+ str = StringIO.new
44
+
45
+ climate = LibCLImate::Climate.new do |climate|
46
+
47
+ climate.program_name = 'program'
48
+ climate.stdout = str
49
+ climate.exit_on_usage = false
50
+ end
51
+
52
+ argv = %w{ --help }
53
+
54
+ r = climate.run argv
55
+
56
+ assert_not_nil r
57
+ assert_kind_of ::Hash, r
58
+ assert_equal 3, r.size
59
+ assert_not_nil r[:flags]
60
+ assert_not_nil r[:options]
61
+ assert_not_nil r[:values]
62
+ assert_equal 4, r[:flags].size
63
+ assert_equal 4, r.flags.size
64
+ assert_equal 1, r[:flags][:given].size
65
+ assert_equal 1, r[:flags][:handled].size
66
+ assert_equal 0, r[:flags][:unhandled].size
67
+ assert_equal 0, r[:flags][:unknown].size
68
+ assert_equal 4, r[:options].size
69
+ assert_equal 0, r[:options][:given].size
70
+ assert_equal 0, r[:options][:handled].size
71
+ assert_equal 0, r[:options][:unhandled].size
72
+ assert_equal 0, r[:options][:unknown].size
73
+ assert_equal 0, r[:values].size
74
+
75
+ lines = str.string.split(/\n/)
76
+ lines = lines.reject { |line| line.chomp.strip.empty? }
77
+ lines = lines.map { |line| line.chomp.strip }
78
+
79
+ assert_equal 6, lines.size
80
+ assert_equal "USAGE: program [ ... flags and options ... ]", lines[0]
81
+ assert_equal "flags/options:", lines[1]
82
+ assert_equal "--help", lines[2]
83
+ assert_equal "shows this help and terminates", lines[3]
84
+ assert_equal "--version", lines[4]
85
+ assert_equal "shows version and terminates", lines[5]
86
+ end
87
+
88
+ def test_help_to_string_with_info_line
89
+
90
+ str = StringIO.new
91
+
92
+ climate = LibCLImate::Climate.new do |climate|
93
+
94
+ climate.program_name = 'program'
95
+ climate.info_lines = 'Synesis Software Open Source'
96
+ climate.stdout = str
97
+ climate.exit_on_usage = false
98
+ end
99
+
100
+ argv = %w{ --help }
101
+
102
+ r = climate.run argv
103
+
104
+ assert_not_nil r
105
+ assert_kind_of ::Hash, r
106
+ assert_equal 3, r.size
107
+ assert_not_nil r[:flags]
108
+ assert_not_nil r[:options]
109
+ assert_not_nil r[:values]
110
+ assert_equal 4, r[:flags].size
111
+ assert_equal 4, r.flags.size
112
+ assert_equal 1, r[:flags][:given].size
113
+ assert_equal 1, r[:flags][:handled].size
114
+ assert_equal 0, r[:flags][:unhandled].size
115
+ assert_equal 0, r[:flags][:unknown].size
116
+ assert_equal 4, r[:options].size
117
+ assert_equal 0, r[:options][:given].size
118
+ assert_equal 0, r[:options][:handled].size
119
+ assert_equal 0, r[:options][:unhandled].size
120
+ assert_equal 0, r[:options][:unknown].size
121
+ assert_equal 0, r[:values].size
122
+
123
+ lines = str.string.split(/\n/)
124
+ lines = lines.reject { |line| line.chomp.strip.empty? }
125
+ lines = lines.map { |line| line.chomp.strip }
126
+
127
+ assert_equal 7, lines.size
128
+ index = -1
129
+ assert_equal 'Synesis Software Open Source', lines[index += 1]
130
+ assert_equal "USAGE: program [ ... flags and options ... ]", lines[index += 1]
131
+ assert_equal "flags/options:", lines[index += 1]
132
+ assert_equal "--help", lines[index += 1]
133
+ assert_equal "shows this help and terminates", lines[index += 1]
134
+ assert_equal "--version", lines[index += 1]
135
+ assert_equal "shows version and terminates", lines[index += 1]
136
+ end
137
+
138
+ def test_version_to_string
139
+
140
+ str = StringIO.new
141
+
142
+ climate = LibCLImate::Climate.new do |climate|
143
+
144
+ climate.program_name = 'program'
145
+ climate.version = [ 1, 2, 3, 4 ]
146
+ climate.stdout = str
147
+ climate.exit_on_usage = false
148
+ end
149
+
150
+ argv = %w{ --version }
151
+
152
+ climate.run argv
153
+
154
+ lines = str.string.split(/\n/)
155
+ lines = lines.reject { |line| line.chomp.strip.empty? }
156
+ lines = lines.map { |line| line.chomp.strip }
157
+
158
+ assert_equal 1, lines.size
159
+ assert_equal "program 1.2.3.4", lines[0]
160
+ end
161
+
162
+ def test_unrecognised_flag
163
+
164
+ strout = StringIO.new
165
+ strerr = StringIO.new
166
+
167
+ climate = LibCLImate::Climate.new do |climate|
168
+
169
+ climate.program_name = 'program'
170
+ climate.stdout = strout
171
+ climate.stderr = strerr
172
+ climate.exit_on_unknown = false
173
+ end
174
+
175
+ argv = %w{ --unknown }
176
+
177
+ climate.run argv
178
+
179
+ lines_out = strout.string.split /\n/
180
+ lines_err = strerr.string.split /\n/
181
+
182
+ assert_equal 0, lines_out.size
183
+ assert_equal 1, lines_err.size
184
+ assert_equal "program: unrecognised flag '--unknown'; use --help for usage", lines_err[0]
185
+ end
186
+
187
+ def test_unrecognised_option
188
+
189
+ strout = StringIO.new
190
+ strerr = StringIO.new
191
+
192
+ climate = LibCLImate::Climate.new do |climate|
193
+
194
+ climate.program_name = 'program'
195
+ climate.stdout = strout
196
+ climate.stderr = strerr
197
+ climate.exit_on_unknown = false
198
+ end
199
+
200
+ argv = %w{ --unknown=10 }
201
+
202
+ climate.run argv
203
+
204
+ lines_out = strout.string.split /\n/
205
+ lines_err = strerr.string.split /\n/
206
+
207
+ assert_equal 0, lines_out.size
208
+ assert_equal 1, lines_err.size
209
+ assert_equal "program: unrecognised option '--unknown=10'; use --help for usage", lines_err[0]
210
+ end
211
+
212
+ def test_one_custom_flag_help_to_string
213
+
214
+ str = StringIO.new
215
+
216
+ is_verbose = false
217
+
218
+ climate = LibCLImate::Climate.new do |climate|
219
+
220
+ climate.program_name = 'program'
221
+ climate.stdout = str
222
+ climate.exit_on_usage = false
223
+
224
+ bl = false#proc { is_verbose = true }
225
+
226
+ climate.aliases << CLASP.Flag('--succinct', alias: '-s', help: 'operates succinctly')
227
+ climate.aliases << CLASP.Flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { :handle => proc { is_verbose = true }})
228
+ end
229
+
230
+ argv = %w{ --help --verbose --succinct }
231
+
232
+ r = climate.run argv
233
+
234
+ assert_not_nil r
235
+ assert_kind_of ::Hash, r
236
+ assert_equal 3, r.size
237
+ assert_not_nil r[:flags]
238
+ assert_not_nil r[:options]
239
+ assert_not_nil r[:values]
240
+ assert_equal 4, r[:flags].size
241
+ assert_equal 3, r[:flags][:given].size
242
+ assert_equal 2, r[:flags][:handled].size
243
+ assert_equal 1, r[:flags][:unhandled].size
244
+ assert_equal 0, r[:flags][:unknown].size
245
+ assert_equal 4, r[:options].size
246
+ assert_equal 0, r[:options][:given].size
247
+ assert_equal 0, r[:options][:handled].size
248
+ assert_equal 0, r[:options][:unhandled].size
249
+ assert_equal 0, r[:options][:unknown].size
250
+ assert_equal 0, r[:values].size
251
+ assert_equal 0, r.values.size
252
+ lines = str.string.split(/\n/)
253
+ lines = lines.reject { |line| line.chomp.strip.empty? }
254
+ lines = lines.map { |line| line.chomp.strip }
255
+
256
+ assert_equal 12, lines.size
257
+ index = -1
258
+ assert_equal "USAGE: program [ ... flags and options ... ]", lines[index += 1]
259
+ assert_equal "flags/options:", lines[index += 1]
260
+ assert_equal "--help", lines[index += 1]
261
+ assert_equal "shows this help and terminates", lines[index += 1]
262
+ assert_equal "--version", lines[index += 1]
263
+ assert_equal "shows version and terminates", lines[index += 1]
264
+ assert_equal "-s", lines[index += 1]
265
+ assert_equal "--succinct", lines[index += 1]
266
+ assert_equal "operates succinctly", lines[index += 1]
267
+ assert_equal "-v", lines[index += 1]
268
+ assert_equal "--verbose", lines[index += 1]
269
+ assert_equal "operates verbosely", lines[index += 1]
270
+ end
271
+
272
+ def test_one_custom_flag_with_select
273
+
274
+ str = StringIO.new
275
+
276
+ is_verbose = false
277
+
278
+ climate = LibCLImate::Climate.new do |climate|
279
+
280
+ climate.program_name = 'program'
281
+ climate.stdout = str
282
+ climate.exit_on_usage = false
283
+
284
+ climate.aliases << CLASP.Flag('--verbose', alias: '-v', help: 'operates verbosely', extras: { handle: proc { is_verbose = true }})
285
+ end
286
+
287
+ argv = %w{ --verbose }
288
+
289
+ r = climate.run argv
290
+
291
+ assert_not_nil r
292
+ assert_kind_of ::Hash, r
293
+ assert_equal 3, r.size
294
+ assert_not_nil r[:flags]
295
+ assert_not_nil r[:options]
296
+ assert_not_nil r[:values]
297
+ assert_equal 4, r[:flags].size
298
+ assert_equal 1, r[:flags][:given].size
299
+ assert_equal 1, r[:flags][:handled].size
300
+ assert_equal 0, r[:flags][:unhandled].size
301
+ assert_equal 0, r[:flags][:unknown].size
302
+ assert_equal 4, r[:options].size
303
+ assert_equal 0, r[:options][:given].size
304
+ assert_equal 0, r[:options][:handled].size
305
+ assert_equal 0, r[:options][:unhandled].size
306
+ assert_equal 0, r[:options][:unknown].size
307
+ assert_equal 0, r[:values].size
308
+ lines = str.string.split(/\n/)
309
+ lines = lines.reject { |line| line.chomp.strip.empty? }
310
+ lines = lines.map { |line| line.chomp.strip }
311
+
312
+ assert_equal 0, lines.size
313
+ assert is_verbose, "is_verbose not altered"
314
+ end
315
+
316
+ def test_one_custom_option_with_select
317
+
318
+ str = StringIO.new
319
+
320
+ verbosity = 1
321
+
322
+ climate = LibCLImate::Climate.new do |climate|
323
+
324
+ climate.program_name = 'program'
325
+ climate.stdout = str
326
+ climate.exit_on_usage = false
327
+
328
+ climate.aliases << CLASP.Option('--verbosity', alias: '-v', help: 'determines level of verbose operation', extras: { handle: proc { |o| verbosity = o.value }})
329
+ end
330
+
331
+ argv = %w{ -v 2 }
332
+
333
+ r = climate.run argv
334
+
335
+ assert_not_nil r
336
+ assert_kind_of ::Hash, r
337
+ assert_equal 3, r.size
338
+ assert_not_nil r[:flags]
339
+ assert_not_nil r[:options]
340
+ assert_not_nil r[:values]
341
+ assert_equal 4, r[:flags].size
342
+ assert_equal 0, r[:flags][:given].size
343
+ assert_equal 0, r[:flags][:handled].size
344
+ assert_equal 0, r[:flags][:unhandled].size
345
+ assert_equal 0, r[:flags][:unknown].size
346
+ assert_equal 4, r[:options].size
347
+ assert_equal 1, r[:options][:given].size
348
+ assert_equal 1, r[:options][:handled].size
349
+ assert_equal 0, r[:options][:unhandled].size
350
+ assert_equal 0, r[:options][:unknown].size
351
+ assert_equal 0, r[:values].size
352
+ lines = str.string.split(/\n/)
353
+ lines = lines.reject { |line| line.chomp.strip.empty? }
354
+ lines = lines.map { |line| line.chomp.strip }
355
+
356
+ assert_equal 0, lines.size
357
+ assert_equal '2', verbosity
358
+ end
359
+ end
360
+
361
+ # ############################## end of file ############################# #
362
+
363
+
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # test complex scenarios
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
6
+
7
+
8
+ require 'libclimate'
9
+
10
+ require 'xqsr3/extensions/test/unit'
11
+
12
+ require 'test/unit'
13
+
14
+ require 'stringio'
15
+
16
+ class Test_Climate_minimal < Test::Unit::TestCase
17
+
18
+ def test_option_with_flag_aliases
19
+
20
+ options = {}
21
+
22
+ climate = LibCLImate::Climate.new do |climate|
23
+
24
+ climate.aliases << CLASP.Flag('--action=list', alias: '-l')
25
+ climate.aliases << CLASP.Flag('--action=change', alias: '-c')
26
+ climate.aliases << CLASP.Option('--action', alias: '-a', extras: { handle: Proc.new { |o, a| options[:action] = o.value } })
27
+ end
28
+
29
+ # invoke via option
30
+ begin
31
+ options = {}
32
+
33
+ argv = %w{ --action=action1 }
34
+
35
+ r = climate.run argv
36
+
37
+ assert_not_nil r
38
+ assert_kind_of ::Hash, r
39
+ assert_equal 3, r.size
40
+ assert_equal 0, r.flags[:given].size
41
+
42
+ assert_equal 1, options.size
43
+ assert_not_nil options[:action]
44
+ assert_equal 'action1', options[:action]
45
+ end
46
+
47
+ # invoke via option alias
48
+ begin
49
+ options = {}
50
+
51
+ argv = %w{ -a action2 }
52
+
53
+ r = climate.run argv
54
+
55
+ assert_equal 1, options.size
56
+ assert_not_nil options[:action]
57
+ assert_equal 'action2', options[:action]
58
+ end
59
+
60
+ # invoke via flag alias
61
+ begin
62
+ options = {}
63
+
64
+ argv = %w{ -c }
65
+
66
+ r = climate.run argv
67
+
68
+ assert_equal 1, options.size
69
+ assert_not_nil options[:action]
70
+ assert_equal 'change', options[:action]
71
+ end
72
+
73
+ # invoke via flag alias
74
+ begin
75
+ options = {}
76
+
77
+ argv = %w{ -l }
78
+
79
+ r = climate.run argv
80
+
81
+ assert_equal 1, options.size
82
+ assert_not_nil options[:action]
83
+ assert_equal 'list', options[:action]
84
+ end
85
+ end
86
+ end
87
+
88
+ # ############################## end of file ############################# #
89
+
90
+
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # executes all other tests
4
+
5
+ this_file = File.expand_path(__FILE__)
6
+ this_dir = File.expand_path(File.dirname(__FILE__))
7
+
8
+ # all tc_*rb in current directory
9
+ Dir[File.join(this_dir, 'tc_*rb')].each { |file| require file }
10
+
11
+ # all ts_*rb in immediate sub-directories
12
+ Dir[File.join(this_dir, '*', 'ts_*rb')].each { |file| require file }
13
+
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libclimate-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Matt Wilson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: clasp-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.2
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '1.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.10.2
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: xqsr3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: 0.8.3
40
+ - - <
41
+ - !ruby/object:Gem::Version
42
+ version: '1.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 0.8.3
50
+ - - <
51
+ - !ruby/object:Gem::Version
52
+ version: '1.0'
53
+ description: |2+
54
+
55
+ email: matthew@synesis.com.au
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - LICENSE
61
+ - README.md
62
+ - lib/libclimate.rb
63
+ - lib/libclimate/climate.rb
64
+ - lib/libclimate/libclimate.rb
65
+ - lib/libclimate/version.rb
66
+ - test/scratch/blankzeroes.rb
67
+ - test/unit/tc_minimal.rb
68
+ - test/unit/tc_test_aliases.rb
69
+ - test/unit/ts_all.rb
70
+ homepage: http://www.libclimate.org/
71
+ licenses:
72
+ - Modified BSD
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.2
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: libCLImate.Ruby
94
+ test_files: []