libclimate-ruby 0.14.0.1 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +43 -9
- data/examples/flag_and_option_specifications.from_DATA.md +4 -6
- data/examples/flag_and_option_specifications.from_DATA.rb +3 -3
- data/examples/flag_and_option_specifications.md +18 -4
- data/examples/flag_and_option_specifications.rb +12 -2
- data/examples/show_usage_and_version.md +4 -5
- data/examples/show_usage_and_version.rb +2 -2
- data/lib/libclimate/climate.rb +463 -188
- data/lib/libclimate/version.rb +1 -1
- data/test/unit/tc_minimal.rb +3 -3
- data/test/unit/tc_minimal_CLASP.rb +3 -3
- data/test/unit/tc_parse.rb +171 -0
- data/test/unit/tc_parse_and_verify.rb +157 -0
- data/test/unit/tc_values.rb +52 -0
- metadata +3 -1
data/lib/libclimate/version.rb
CHANGED
data/test/unit/tc_minimal.rb
CHANGED
@@ -250,7 +250,7 @@ class Test_Climate_minimal < Test::Unit::TestCase
|
|
250
250
|
bl = false#proc { is_verbose = true }
|
251
251
|
|
252
252
|
cl.add_flag('--succinct', alias: '-s', help: 'operates succinctly')
|
253
|
-
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely'
|
253
|
+
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely') { is_verbose = true }
|
254
254
|
end
|
255
255
|
|
256
256
|
argv = %w{ --help --verbose --succinct }
|
@@ -308,7 +308,7 @@ class Test_Climate_minimal < Test::Unit::TestCase
|
|
308
308
|
cl.stdout = str
|
309
309
|
cl.exit_on_usage = false
|
310
310
|
|
311
|
-
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely'
|
311
|
+
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely') { is_verbose = true }
|
312
312
|
end
|
313
313
|
|
314
314
|
argv = %w{ --verbose }
|
@@ -353,7 +353,7 @@ class Test_Climate_minimal < Test::Unit::TestCase
|
|
353
353
|
cl.stdout = str
|
354
354
|
cl.exit_on_usage = false
|
355
355
|
|
356
|
-
cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation'
|
356
|
+
cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation') { |o| verbosity = o.value }
|
357
357
|
end
|
358
358
|
|
359
359
|
argv = %w{ -v 2 }
|
@@ -222,7 +222,7 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
|
|
222
222
|
cl.exit_on_usage = false
|
223
223
|
|
224
224
|
cl.add_flag('--succinct', alias: '-s', help: 'operates succinctly')
|
225
|
-
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely'
|
225
|
+
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely') { is_verbose = true }
|
226
226
|
end
|
227
227
|
|
228
228
|
argv = %w{ --help --verbose --succinct }
|
@@ -279,7 +279,7 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
|
|
279
279
|
cl.stdout = str
|
280
280
|
cl.exit_on_usage = false
|
281
281
|
|
282
|
-
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely'
|
282
|
+
cl.add_flag('--verbose', alias: '-v', help: 'operates verbosely') { is_verbose = true }
|
283
283
|
end
|
284
284
|
|
285
285
|
argv = %w{ --verbose }
|
@@ -323,7 +323,7 @@ class Test_Climate_minimal_CLASP < Test::Unit::TestCase
|
|
323
323
|
cl.stdout = str
|
324
324
|
cl.exit_on_usage = false
|
325
325
|
|
326
|
-
cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation'
|
326
|
+
cl.add_option('--verbosity', alias: '-v', help: 'determines level of verbose operation') { |o| verbosity = o.value }
|
327
327
|
end
|
328
328
|
|
329
329
|
argv = %w{ -v 2 }
|
@@ -0,0 +1,171 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# test version inference
|
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_parse < Test::Unit::TestCase
|
17
|
+
|
18
|
+
class VerifyException < RuntimeError; end
|
19
|
+
|
20
|
+
class MissingRequiredException < VerifyException; end
|
21
|
+
class UnrecognisedArgumentException < VerifyException; end
|
22
|
+
class UnusedArgumentException < VerifyException; end
|
23
|
+
|
24
|
+
def test_empty_specs_empty_args
|
25
|
+
|
26
|
+
stdout = StringIO.new
|
27
|
+
stderr = StringIO.new
|
28
|
+
|
29
|
+
climate = LibCLImate::Climate.new do |cl|
|
30
|
+
|
31
|
+
cl.stdout = $stdout
|
32
|
+
cl.stderr = $stderr
|
33
|
+
end
|
34
|
+
|
35
|
+
assert $stdout.equal? climate.stdout
|
36
|
+
assert $stderr.equal? climate.stderr
|
37
|
+
|
38
|
+
argv = [
|
39
|
+
]
|
40
|
+
|
41
|
+
r = climate.parse argv
|
42
|
+
|
43
|
+
assert_eql climate, r.climate
|
44
|
+
assert_equal 0, r.flags.size
|
45
|
+
assert_equal 0, r.options.size
|
46
|
+
assert_equal 0, r.values.size
|
47
|
+
|
48
|
+
r.verify()
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_one_flag_with_block
|
52
|
+
|
53
|
+
stdout = StringIO.new
|
54
|
+
stderr = StringIO.new
|
55
|
+
|
56
|
+
debug = false
|
57
|
+
|
58
|
+
climate = LibCLImate::Climate.new do |cl|
|
59
|
+
|
60
|
+
cl.add_flag('--debug', alias: '-d') { debug = true }
|
61
|
+
|
62
|
+
cl.stdout = $stdout
|
63
|
+
cl.stderr = $stderr
|
64
|
+
end
|
65
|
+
|
66
|
+
assert $stdout.equal? climate.stdout
|
67
|
+
assert $stderr.equal? climate.stderr
|
68
|
+
|
69
|
+
argv = [
|
70
|
+
|
71
|
+
'-d',
|
72
|
+
]
|
73
|
+
|
74
|
+
r = climate.parse argv
|
75
|
+
|
76
|
+
assert_false debug
|
77
|
+
|
78
|
+
assert_eql climate, r.climate
|
79
|
+
assert_equal 1, r.flags.size
|
80
|
+
assert_equal 0, r.options.size
|
81
|
+
assert_equal 0, r.values.size
|
82
|
+
|
83
|
+
flag0 = r.flags[0]
|
84
|
+
|
85
|
+
assert_equal '-d', flag0.given_name
|
86
|
+
assert_equal '--debug', flag0.name
|
87
|
+
|
88
|
+
r.verify()
|
89
|
+
|
90
|
+
assert_true debug
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_one_option_with_block
|
94
|
+
|
95
|
+
stdout = StringIO.new
|
96
|
+
stderr = StringIO.new
|
97
|
+
|
98
|
+
verb = nil
|
99
|
+
|
100
|
+
climate = LibCLImate::Climate.new do |cl|
|
101
|
+
|
102
|
+
cl.add_option('--verbosity', alias: '-v') do |o, s|
|
103
|
+
|
104
|
+
verb = o.value
|
105
|
+
end
|
106
|
+
|
107
|
+
cl.stdout = $stdout
|
108
|
+
cl.stderr = $stderr
|
109
|
+
end
|
110
|
+
|
111
|
+
assert $stdout.equal? climate.stdout
|
112
|
+
assert $stderr.equal? climate.stderr
|
113
|
+
|
114
|
+
argv = [
|
115
|
+
|
116
|
+
'-v',
|
117
|
+
'chatty',
|
118
|
+
]
|
119
|
+
|
120
|
+
r = climate.parse argv
|
121
|
+
|
122
|
+
assert_nil verb
|
123
|
+
|
124
|
+
assert_eql climate, r.climate
|
125
|
+
assert_equal 0, r.flags.size
|
126
|
+
assert_equal 1, r.options.size
|
127
|
+
assert_equal 0, r.values.size
|
128
|
+
|
129
|
+
option0 = r.options[0]
|
130
|
+
|
131
|
+
assert_equal '-v', option0.given_name
|
132
|
+
assert_equal '--verbosity', option0.name
|
133
|
+
|
134
|
+
r.verify()
|
135
|
+
|
136
|
+
assert_equal 'chatty', verb
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_one_required_flag_that_is_missing
|
140
|
+
|
141
|
+
stdout = StringIO.new
|
142
|
+
stderr = StringIO.new
|
143
|
+
|
144
|
+
climate = LibCLImate::Climate.new do |cl|
|
145
|
+
|
146
|
+
cl.add_option('--verbosity', alias: '-v', required: true) do |o, s|
|
147
|
+
|
148
|
+
verb = o.value
|
149
|
+
end
|
150
|
+
|
151
|
+
cl.stdout = $stdout
|
152
|
+
cl.stderr = $stderr
|
153
|
+
end
|
154
|
+
|
155
|
+
assert $stdout.equal? climate.stdout
|
156
|
+
assert $stderr.equal? climate.stderr
|
157
|
+
|
158
|
+
argv = [
|
159
|
+
]
|
160
|
+
|
161
|
+
r = climate.parse argv
|
162
|
+
|
163
|
+
assert_eql climate, r.climate
|
164
|
+
assert_equal 0, r.flags.size
|
165
|
+
assert_equal 0, r.options.size
|
166
|
+
assert_equal 0, r.values.size
|
167
|
+
|
168
|
+
assert_raise_with_message(MissingRequiredException, /.*verbosity.*not specified/) { r.verify(raise_on_required: MissingRequiredException) }
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
@@ -0,0 +1,157 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# test version inference
|
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_parse_and_verify < Test::Unit::TestCase
|
17
|
+
|
18
|
+
class VerifyException < RuntimeError; end
|
19
|
+
|
20
|
+
class MissingRequiredException < VerifyException; end
|
21
|
+
class UnrecognisedArgumentException < VerifyException; end
|
22
|
+
class UnusedArgumentException < VerifyException; end
|
23
|
+
|
24
|
+
def test_empty_specs_empty_args
|
25
|
+
|
26
|
+
stdout = StringIO.new
|
27
|
+
stderr = StringIO.new
|
28
|
+
|
29
|
+
climate = LibCLImate::Climate.new do |cl|
|
30
|
+
|
31
|
+
cl.stdout = $stdout
|
32
|
+
cl.stderr = $stderr
|
33
|
+
end
|
34
|
+
|
35
|
+
assert $stdout.equal? climate.stdout
|
36
|
+
assert $stderr.equal? climate.stderr
|
37
|
+
|
38
|
+
argv = [
|
39
|
+
]
|
40
|
+
|
41
|
+
r = climate.parse_and_verify argv
|
42
|
+
|
43
|
+
assert_eql climate, r.climate
|
44
|
+
assert_equal 0, r.flags.size
|
45
|
+
assert_equal 0, r.options.size
|
46
|
+
assert_equal 0, r.values.size
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_one_flag_with_block
|
50
|
+
|
51
|
+
stdout = StringIO.new
|
52
|
+
stderr = StringIO.new
|
53
|
+
|
54
|
+
debug = false
|
55
|
+
|
56
|
+
climate = LibCLImate::Climate.new do |cl|
|
57
|
+
|
58
|
+
cl.add_flag('--debug', alias: '-d') { debug = true }
|
59
|
+
|
60
|
+
cl.stdout = $stdout
|
61
|
+
cl.stderr = $stderr
|
62
|
+
end
|
63
|
+
|
64
|
+
assert $stdout.equal? climate.stdout
|
65
|
+
assert $stderr.equal? climate.stderr
|
66
|
+
|
67
|
+
argv = [
|
68
|
+
|
69
|
+
'-d',
|
70
|
+
]
|
71
|
+
|
72
|
+
r = climate.parse_and_verify argv
|
73
|
+
|
74
|
+
assert_true debug
|
75
|
+
|
76
|
+
assert_eql climate, r.climate
|
77
|
+
assert_equal 1, r.flags.size
|
78
|
+
assert_equal 0, r.options.size
|
79
|
+
assert_equal 0, r.values.size
|
80
|
+
|
81
|
+
flag0 = r.flags[0]
|
82
|
+
|
83
|
+
assert_equal '-d', flag0.given_name
|
84
|
+
assert_equal '--debug', flag0.name
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_one_option_with_block
|
88
|
+
|
89
|
+
stdout = StringIO.new
|
90
|
+
stderr = StringIO.new
|
91
|
+
|
92
|
+
verb = nil
|
93
|
+
|
94
|
+
climate = LibCLImate::Climate.new do |cl|
|
95
|
+
|
96
|
+
cl.add_option('--verbosity', alias: '-v') do |o, s|
|
97
|
+
|
98
|
+
verb = o.value
|
99
|
+
end
|
100
|
+
|
101
|
+
cl.stdout = $stdout
|
102
|
+
cl.stderr = $stderr
|
103
|
+
end
|
104
|
+
|
105
|
+
assert $stdout.equal? climate.stdout
|
106
|
+
assert $stderr.equal? climate.stderr
|
107
|
+
|
108
|
+
argv = [
|
109
|
+
|
110
|
+
'-v',
|
111
|
+
'chatty',
|
112
|
+
]
|
113
|
+
|
114
|
+
r = climate.parse_and_verify argv
|
115
|
+
|
116
|
+
assert_equal 'chatty', verb
|
117
|
+
|
118
|
+
assert_eql climate, r.climate
|
119
|
+
assert_equal 0, r.flags.size
|
120
|
+
assert_equal 1, r.options.size
|
121
|
+
assert_equal 0, r.values.size
|
122
|
+
|
123
|
+
option0 = r.options[0]
|
124
|
+
|
125
|
+
assert_equal '-v', option0.given_name
|
126
|
+
assert_equal '--verbosity', option0.name
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_one_required_flag_that_is_missing
|
130
|
+
|
131
|
+
stdout = StringIO.new
|
132
|
+
stderr = StringIO.new
|
133
|
+
|
134
|
+
climate = LibCLImate::Climate.new do |cl|
|
135
|
+
|
136
|
+
cl.add_option('--verbosity', alias: '-v', required: true) do |o, s|
|
137
|
+
|
138
|
+
verb = o.value
|
139
|
+
end
|
140
|
+
|
141
|
+
cl.stdout = $stdout
|
142
|
+
cl.stderr = $stderr
|
143
|
+
end
|
144
|
+
|
145
|
+
assert $stdout.equal? climate.stdout
|
146
|
+
assert $stderr.equal? climate.stderr
|
147
|
+
|
148
|
+
argv = [
|
149
|
+
]
|
150
|
+
|
151
|
+
assert_raise_with_message(MissingRequiredException, /.*verbosity.*not specified/) do
|
152
|
+
|
153
|
+
climate.parse_and_verify argv, raise_on_required: MissingRequiredException
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
data/test/unit/tc_values.rb
CHANGED
@@ -87,6 +87,58 @@ class Test_Climate_values_constraints < Test::Unit::TestCase
|
|
87
87
|
assert_match /wrong number of values.*3 given.*2 required.*/, stderr.string
|
88
88
|
end
|
89
89
|
|
90
|
+
def test_constrain_with_integer_and_names
|
91
|
+
|
92
|
+
stdout = StringIO.new
|
93
|
+
stderr = StringIO.new
|
94
|
+
|
95
|
+
climate = LibCLImate::Climate.new do |cl|
|
96
|
+
|
97
|
+
cl.exit_on_missing = false
|
98
|
+
|
99
|
+
cl.stdout = stdout
|
100
|
+
cl.stderr = stderr
|
101
|
+
|
102
|
+
cl.constrain_values = 2
|
103
|
+
cl.value_names = [
|
104
|
+
|
105
|
+
'input-path',
|
106
|
+
'output-path',
|
107
|
+
]
|
108
|
+
end
|
109
|
+
|
110
|
+
stdout.string = ''
|
111
|
+
stderr.string = ''
|
112
|
+
r = climate.run [ ]
|
113
|
+
assert_equal 0, r.values.size
|
114
|
+
assert_empty stdout.string
|
115
|
+
assert_not_empty stderr.string
|
116
|
+
assert_match /input-path not specified.*#{climate.usage_help_suffix}/, stderr.string
|
117
|
+
|
118
|
+
stdout.string = ''
|
119
|
+
stderr.string = ''
|
120
|
+
r = climate.run [ 'value-1' ]
|
121
|
+
assert_equal 1, r.values.size
|
122
|
+
assert_empty stdout.string
|
123
|
+
assert_not_empty stderr.string
|
124
|
+
assert_match /output-path not specified.*#{climate.usage_help_suffix}/, stderr.string
|
125
|
+
|
126
|
+
stdout.string = ''
|
127
|
+
stderr.string = ''
|
128
|
+
r = climate.run [ 'value-1', 'value-2' ]
|
129
|
+
assert_equal 2, r.values.size
|
130
|
+
assert_empty stdout.string
|
131
|
+
assert_empty stderr.string
|
132
|
+
|
133
|
+
stdout.string = ''
|
134
|
+
stderr.string = ''
|
135
|
+
r = climate.run [ 'value-1', 'value-2', 'value-3' ]
|
136
|
+
assert_equal 3, r.values.size
|
137
|
+
assert_empty stdout.string
|
138
|
+
assert_not_empty stderr.string
|
139
|
+
assert_match /wrong number of values.*3 given.*2 required.*/, stderr.string
|
140
|
+
end
|
141
|
+
|
90
142
|
def test_constrain_with_simple_range
|
91
143
|
|
92
144
|
stdout = StringIO.new
|