cli 0.0.4 → 0.1.0
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.
- data/README.md +151 -0
- data/VERSION +1 -1
- data/cli.gemspec +14 -5
- data/examples/processor +17 -0
- data/examples/sinatra +41 -0
- data/lib/cli.rb +40 -8
- data/lib/cli/arguments.rb +4 -0
- data/lib/cli/dsl.rb +23 -16
- data/lib/cli/options.rb +4 -11
- data/spec/argument_spec.rb +133 -0
- data/spec/cli_spec.rb +17 -557
- data/spec/conflict_reporting_spec.rb +69 -0
- data/spec/option_spec.rb +155 -0
- data/spec/separator_spec.rb +57 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/stdin_spec.rb +74 -0
- data/spec/switch_spec.rb +62 -0
- data/spec/usage_spec.rb +303 -0
- metadata +15 -6
- data/README.rdoc +0 -19
data/lib/cli/options.rb
CHANGED
@@ -1,19 +1,12 @@
|
|
1
1
|
require 'cli/switches'
|
2
2
|
|
3
3
|
class CLI::Options < CLI::Switches
|
4
|
-
def
|
5
|
-
|
6
|
-
@defaults = []
|
7
|
-
@mandatory = []
|
4
|
+
def defaults
|
5
|
+
select{|o| o.has_default?}
|
8
6
|
end
|
9
7
|
|
10
|
-
def
|
11
|
-
|
12
|
-
@defaults << option_dsl if option_dsl.has_default?
|
13
|
-
@mandatory << option_dsl unless option_dsl.optional?
|
8
|
+
def mandatory
|
9
|
+
select{|o| o.mandatory?}
|
14
10
|
end
|
15
|
-
|
16
|
-
attr_reader :defaults
|
17
|
-
attr_reader :mandatory
|
18
11
|
end
|
19
12
|
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe CLI do
|
4
|
+
describe 'argument handling' do
|
5
|
+
it "should handle single argument" do
|
6
|
+
ps = CLI.new do
|
7
|
+
argument :log
|
8
|
+
end.parse(['/tmp'])
|
9
|
+
ps.log.should be_a String
|
10
|
+
ps.log.should == '/tmp'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "non empty, non optional with class casting" do
|
14
|
+
ps = CLI.new do
|
15
|
+
argument :log, :cast => Pathname
|
16
|
+
end.parse(['/tmp'])
|
17
|
+
ps.log.should be_a Pathname
|
18
|
+
ps.log.to_s.should == '/tmp'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "non empty, non optional with builtin class casting" do
|
22
|
+
ps = CLI.new do
|
23
|
+
argument :number, :cast => Integer
|
24
|
+
end.parse(['123'])
|
25
|
+
ps.number.should be_a Integer
|
26
|
+
ps.number.should == 123
|
27
|
+
|
28
|
+
ps = CLI.new do
|
29
|
+
argument :number, :cast => Float
|
30
|
+
end.parse(['123'])
|
31
|
+
ps.number.should be_a Float
|
32
|
+
ps.number.should == 123.0
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should handle multiple arguments" do
|
36
|
+
ps = CLI.new do
|
37
|
+
argument :log, :cast => Pathname
|
38
|
+
argument :test
|
39
|
+
end.parse(['/tmp', 'hello'])
|
40
|
+
ps.log.should be_a Pathname
|
41
|
+
ps.log.to_s.should == '/tmp'
|
42
|
+
ps.test.should be_a String
|
43
|
+
ps.test.should == 'hello'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should raise error if not symbol and optional hash is passed" do
|
47
|
+
lambda {
|
48
|
+
ps = CLI.new do
|
49
|
+
argument 'number'
|
50
|
+
end
|
51
|
+
}.should raise_error CLI::ParserError::NameArgumetNotSymbolError, "argument name has to be of type Symbol, got String"
|
52
|
+
|
53
|
+
lambda {
|
54
|
+
ps = CLI.new do
|
55
|
+
argument :number, :test
|
56
|
+
end
|
57
|
+
}.should raise_error CLI::ParserError::OptionsArgumentNotHashError, "argument options has to be of type Hash, got Symbol"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should raise error if artument name is specified twice" do
|
61
|
+
lambda {
|
62
|
+
ps = CLI.new do
|
63
|
+
argument :number
|
64
|
+
argument :number
|
65
|
+
end
|
66
|
+
}.should raise_error CLI::ParserError::ArgumentNameSpecifiedTwice, 'argument number specified twice'
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should be required by default and raise error if not given" do
|
70
|
+
lambda {
|
71
|
+
ps = CLI.new do
|
72
|
+
argument :log
|
73
|
+
end.parse([])
|
74
|
+
}.should raise_error CLI::ParsingError::MandatoryArgumentNotSpecifiedError, 'mandatory argument log not given'
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should raise error if casting fail" do
|
78
|
+
require 'ip'
|
79
|
+
lambda {
|
80
|
+
ps = CLI.new do
|
81
|
+
argument :log, :cast => IP
|
82
|
+
end.parse(['abc'])
|
83
|
+
}.should raise_error CLI::ParsingError::CastError, 'failed to cast: log to type: IP: invalid address'
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "with defaults" do
|
87
|
+
it "when not enought arguments given it should fill required arguments only with defaults" do
|
88
|
+
ps = CLI.new do
|
89
|
+
argument :log, :cast => Pathname, :default => '/tmp'
|
90
|
+
argument :test
|
91
|
+
end.parse(['hello'])
|
92
|
+
ps.log.should be_a Pathname
|
93
|
+
ps.log.to_s.should == '/tmp'
|
94
|
+
ps.test.should be_a String
|
95
|
+
ps.test.should == 'hello'
|
96
|
+
|
97
|
+
ps = CLI.new do
|
98
|
+
argument :log, :cast => Pathname
|
99
|
+
argument :test, :default => 'hello'
|
100
|
+
end.parse(['/tmp'])
|
101
|
+
ps.log.should be_a Pathname
|
102
|
+
ps.log.to_s.should == '/tmp'
|
103
|
+
ps.test.should be_a String
|
104
|
+
ps.test.should == 'hello'
|
105
|
+
|
106
|
+
ps = CLI.new do
|
107
|
+
argument :log, :cast => Pathname
|
108
|
+
argument :magick, :default => 'word'
|
109
|
+
argument :test
|
110
|
+
argument :code, :cast => Integer, :default => '123'
|
111
|
+
end.parse(['/tmp', 'hello'])
|
112
|
+
ps.log.to_s.should == '/tmp'
|
113
|
+
ps.magick.should == 'word'
|
114
|
+
ps.test.should == 'hello'
|
115
|
+
ps.code.should == 123
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should fill defaults form the beginning if more than required arguments are given" do
|
119
|
+
ps = CLI.new do
|
120
|
+
argument :log, :cast => Pathname
|
121
|
+
argument :magick, :default => 'word'
|
122
|
+
argument :test
|
123
|
+
argument :code, :cast => Integer, :default => '123'
|
124
|
+
end.parse(['/tmp', 'hello', 'world'])
|
125
|
+
ps.log.to_s.should == '/tmp'
|
126
|
+
ps.magick.should == 'hello'
|
127
|
+
ps.test.should == 'world'
|
128
|
+
ps.code.should == 123
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
data/spec/cli_spec.rb
CHANGED
@@ -1,335 +1,6 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
require 'cli'
|
3
|
-
|
4
|
-
def stdin_write(data)
|
5
|
-
r, w = IO.pipe
|
6
|
-
old_stdin = STDIN.reopen r
|
7
|
-
Thread.new do
|
8
|
-
w.write data
|
9
|
-
w.close
|
10
|
-
end
|
11
|
-
begin
|
12
|
-
yield
|
13
|
-
ensure
|
14
|
-
STDIN.reopen old_stdin
|
15
|
-
end
|
16
|
-
end
|
17
2
|
|
18
3
|
describe CLI do
|
19
|
-
describe 'STDIN handling' do
|
20
|
-
before :all do
|
21
|
-
@yaml = <<EOF
|
22
|
-
---
|
23
|
-
:parser:
|
24
|
-
:successes: 41
|
25
|
-
:failures: 0
|
26
|
-
EOF
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should be nil if not specified" do
|
30
|
-
ps = CLI.new.parse
|
31
|
-
ps.stdin.should be_nil
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should return IO if stdin is defined" do
|
35
|
-
ps = CLI.new do
|
36
|
-
stdin
|
37
|
-
end.parse
|
38
|
-
ps.stdin.should be_a IO
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should return YAML document if stdin is casted to YAML" do
|
42
|
-
ps = nil
|
43
|
-
ss = CLI.new do
|
44
|
-
stdin :log_data, :cast => YAML, :description => 'log statistic data in YAML format'
|
45
|
-
end
|
46
|
-
|
47
|
-
stdin_write(@yaml) do
|
48
|
-
ps = ss.parse
|
49
|
-
end
|
50
|
-
|
51
|
-
ps.stdin.should == {:parser=>{:successes=>41, :failures=>0}}
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe 'argument handling' do
|
56
|
-
it "should handle single argument" do
|
57
|
-
ps = CLI.new do
|
58
|
-
argument :log
|
59
|
-
end.parse(['/tmp'])
|
60
|
-
ps.log.should be_a String
|
61
|
-
ps.log.should == '/tmp'
|
62
|
-
end
|
63
|
-
|
64
|
-
it "non empty, non optional with class casting" do
|
65
|
-
ps = CLI.new do
|
66
|
-
argument :log, :cast => Pathname
|
67
|
-
end.parse(['/tmp'])
|
68
|
-
ps.log.should be_a Pathname
|
69
|
-
ps.log.to_s.should == '/tmp'
|
70
|
-
end
|
71
|
-
|
72
|
-
it "non empty, non optional with builtin class casting" do
|
73
|
-
ps = CLI.new do
|
74
|
-
argument :number, :cast => Integer
|
75
|
-
end.parse(['123'])
|
76
|
-
ps.number.should be_a Integer
|
77
|
-
ps.number.should == 123
|
78
|
-
|
79
|
-
ps = CLI.new do
|
80
|
-
argument :number, :cast => Float
|
81
|
-
end.parse(['123'])
|
82
|
-
ps.number.should be_a Float
|
83
|
-
ps.number.should == 123.0
|
84
|
-
end
|
85
|
-
|
86
|
-
it "should handle multiple arguments" do
|
87
|
-
ps = CLI.new do
|
88
|
-
argument :log, :cast => Pathname
|
89
|
-
argument :test
|
90
|
-
end.parse(['/tmp', 'hello'])
|
91
|
-
ps.log.should be_a Pathname
|
92
|
-
ps.log.to_s.should == '/tmp'
|
93
|
-
ps.test.should be_a String
|
94
|
-
ps.test.should == 'hello'
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should raise error if not symbol and optional hash is passed" do
|
98
|
-
lambda {
|
99
|
-
ps = CLI.new do
|
100
|
-
argument 'number'
|
101
|
-
end
|
102
|
-
}.should raise_error CLI::ParserError::NameArgumetNotSymbolError, "argument name has to be of type Symbol, got String"
|
103
|
-
|
104
|
-
lambda {
|
105
|
-
ps = CLI.new do
|
106
|
-
argument :number, :test
|
107
|
-
end
|
108
|
-
}.should raise_error CLI::ParserError::OptionsArgumentNotHashError, "argument options has to be of type Hash, got Symbol"
|
109
|
-
end
|
110
|
-
|
111
|
-
it "should raise error if artument name is specified twice" do
|
112
|
-
lambda {
|
113
|
-
ps = CLI.new do
|
114
|
-
argument :number
|
115
|
-
argument :number
|
116
|
-
end
|
117
|
-
}.should raise_error CLI::ParserError::ArgumentNameSpecifiedTwice, 'argument number specified twice'
|
118
|
-
end
|
119
|
-
|
120
|
-
it "should raise error if not given" do
|
121
|
-
lambda {
|
122
|
-
ps = CLI.new do
|
123
|
-
argument :log
|
124
|
-
end.parse([])
|
125
|
-
}.should raise_error CLI::ParsingError::MandatoryArgumentNotSpecifiedError, 'mandatory argument log not given'
|
126
|
-
end
|
127
|
-
|
128
|
-
it "should raise error if casting fail" do
|
129
|
-
require 'ip'
|
130
|
-
lambda {
|
131
|
-
ps = CLI.new do
|
132
|
-
argument :log, :cast => IP
|
133
|
-
end.parse(['abc'])
|
134
|
-
}.should raise_error CLI::ParsingError::CastError, 'failed to cast: log to type: IP: invalid address'
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "with defaults" do
|
138
|
-
it "should use default first argument" do
|
139
|
-
ps = CLI.new do
|
140
|
-
argument :log, :cast => Pathname, :default => '/tmp'
|
141
|
-
argument :test
|
142
|
-
end.parse(['hello'])
|
143
|
-
ps.log.should be_a Pathname
|
144
|
-
ps.log.to_s.should == '/tmp'
|
145
|
-
ps.test.should be_a String
|
146
|
-
ps.test.should == 'hello'
|
147
|
-
end
|
148
|
-
|
149
|
-
it "should use default second argument" do
|
150
|
-
ps = CLI.new do
|
151
|
-
argument :log, :cast => Pathname
|
152
|
-
argument :test, :default => 'hello'
|
153
|
-
end.parse(['/tmp'])
|
154
|
-
ps.log.should be_a Pathname
|
155
|
-
ps.log.to_s.should == '/tmp'
|
156
|
-
ps.test.should be_a String
|
157
|
-
ps.test.should == 'hello'
|
158
|
-
end
|
159
|
-
|
160
|
-
it "should use default second argument" do
|
161
|
-
ps = CLI.new do
|
162
|
-
argument :log, :cast => Pathname
|
163
|
-
argument :magick, :default => 'word'
|
164
|
-
argument :test
|
165
|
-
argument :code, :cast => Integer, :default => '123'
|
166
|
-
end.parse(['/tmp', 'hello'])
|
167
|
-
ps.log.to_s.should == '/tmp'
|
168
|
-
ps.magick.should == 'word'
|
169
|
-
ps.test.should == 'hello'
|
170
|
-
ps.code.should == 123
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
describe 'switch handling' do
|
176
|
-
it "should handle long switch names" do
|
177
|
-
ps = CLI.new do
|
178
|
-
switch :location
|
179
|
-
switch :unset
|
180
|
-
end.parse(['--location'])
|
181
|
-
ps.location.should be_true
|
182
|
-
ps.unset.should be_nil
|
183
|
-
end
|
184
|
-
|
185
|
-
it "should handle short switch names" do
|
186
|
-
ps = CLI.new do
|
187
|
-
switch :location, :short => :l
|
188
|
-
switch :unset, :short => :u
|
189
|
-
end.parse(['-l'])
|
190
|
-
ps.location.should be_true
|
191
|
-
ps.unset.should be_nil
|
192
|
-
end
|
193
|
-
|
194
|
-
it "should raise error if not symbol and optional hash is passed" do
|
195
|
-
lambda {
|
196
|
-
ps = CLI.new do
|
197
|
-
switch 'number'
|
198
|
-
end.parse([])
|
199
|
-
}.should raise_error CLI::ParserError::NameArgumetNotSymbolError, "switch name has to be of type Symbol, got String"
|
200
|
-
|
201
|
-
lambda {
|
202
|
-
ps = CLI.new do
|
203
|
-
switch :number, :test
|
204
|
-
end
|
205
|
-
}.should raise_error CLI::ParserError::OptionsArgumentNotHashError, "switch options has to be of type Hash, got Symbol"
|
206
|
-
end
|
207
|
-
|
208
|
-
it "shoud raise error is short name is invalid" do
|
209
|
-
lambda {
|
210
|
-
ps = CLI.new do
|
211
|
-
switch :location, :short => "l"
|
212
|
-
end
|
213
|
-
}.should raise_error CLI::ParserError::ShortNameNotSymbolError, 'short name has to be of type Symbol, got String'
|
214
|
-
|
215
|
-
lambda {
|
216
|
-
ps = CLI.new do
|
217
|
-
switch :location, :short => :abc
|
218
|
-
end
|
219
|
-
}.should raise_error CLI::ParserError::ShortNameIsInvalidError, 'short name has to be one letter symbol, got abc'
|
220
|
-
end
|
221
|
-
|
222
|
-
it "should raise error on unrecognized switch" do
|
223
|
-
ps = CLI.new do
|
224
|
-
option :location
|
225
|
-
end
|
226
|
-
|
227
|
-
lambda {
|
228
|
-
ps.parse(['--xxx'])
|
229
|
-
}.should raise_error CLI::ParsingError::UnknownSwitchError, 'unknown switch --xxx'
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
describe 'option handling' do
|
234
|
-
it "should handle long option names" do
|
235
|
-
ps = CLI.new do
|
236
|
-
option :location
|
237
|
-
end.parse(['--location', 'singapore'])
|
238
|
-
ps.location.should be_a String
|
239
|
-
ps.location.should == 'singapore'
|
240
|
-
end
|
241
|
-
|
242
|
-
it "should handle short option names" do
|
243
|
-
ps = CLI.new do
|
244
|
-
option :location, :short => :l
|
245
|
-
end.parse(['-l', 'singapore'])
|
246
|
-
ps.location.should be_a String
|
247
|
-
ps.location.should == 'singapore'
|
248
|
-
end
|
249
|
-
|
250
|
-
it "should handle default values" do
|
251
|
-
ps = CLI.new do
|
252
|
-
option :location, :default => 'singapore'
|
253
|
-
option :size, :cast => Integer, :default => 23
|
254
|
-
end.parse([])
|
255
|
-
ps.location.should be_a String
|
256
|
-
ps.location.should == 'singapore'
|
257
|
-
ps.size.should be_a Integer
|
258
|
-
ps.size.should == 23
|
259
|
-
end
|
260
|
-
|
261
|
-
it "should support casting" do
|
262
|
-
ps = CLI.new do
|
263
|
-
option :size, :cast => Integer
|
264
|
-
end.parse(['--size', '24'])
|
265
|
-
ps.size.should be_a Integer
|
266
|
-
ps.size.should == 24
|
267
|
-
end
|
268
|
-
|
269
|
-
it "not given and not defined options should be nil" do
|
270
|
-
ps = CLI.new do
|
271
|
-
option :size, :cast => Integer
|
272
|
-
end.parse([])
|
273
|
-
ps.size.should be_nil
|
274
|
-
ps.gold.should be_nil
|
275
|
-
end
|
276
|
-
|
277
|
-
it "should handle multiple long and short intermixed options" do
|
278
|
-
ps = CLI.new do
|
279
|
-
option :location, :short => :l
|
280
|
-
option :group, :default => 'red'
|
281
|
-
option :power_up, :short => :p
|
282
|
-
option :speed, :short => :s, :cast => Integer
|
283
|
-
option :not_given
|
284
|
-
option :size
|
285
|
-
end.parse(['-l', 'singapore', '--power-up', 'yes', '-s', '24', '--size', 'XXXL'])
|
286
|
-
ps.group.should == 'red'
|
287
|
-
ps.power_up.should == 'yes'
|
288
|
-
ps.speed.should == 24
|
289
|
-
ps.not_given.should be_nil
|
290
|
-
ps.size.should == 'XXXL'
|
291
|
-
ps.gold.should be_nil
|
292
|
-
end
|
293
|
-
|
294
|
-
it "should raise error if not symbol and optional hash is passed" do
|
295
|
-
lambda {
|
296
|
-
ps = CLI.new do
|
297
|
-
option 'number'
|
298
|
-
end
|
299
|
-
}.should raise_error CLI::ParserError::NameArgumetNotSymbolError, "option name has to be of type Symbol, got String"
|
300
|
-
|
301
|
-
lambda {
|
302
|
-
ps = CLI.new do
|
303
|
-
option :number, :test
|
304
|
-
end
|
305
|
-
}.should raise_error CLI::ParserError::OptionsArgumentNotHashError, "option options has to be of type Hash, got Symbol"
|
306
|
-
end
|
307
|
-
|
308
|
-
it "should raise error on missing option argument" do
|
309
|
-
ps = CLI.new do
|
310
|
-
option :location
|
311
|
-
end
|
312
|
-
|
313
|
-
lambda {
|
314
|
-
ps.parse(['--location'])
|
315
|
-
}.should raise_error CLI::ParsingError::MissingOptionValueError, 'missing value for option --location'
|
316
|
-
end
|
317
|
-
|
318
|
-
it "should raise error on missing mandatory option" do
|
319
|
-
ps = CLI.new do
|
320
|
-
option :location
|
321
|
-
option :weight, :required => true
|
322
|
-
option :size, :required => true
|
323
|
-
option :group, :default => 'red'
|
324
|
-
option :speed, :short => :s, :cast => Integer
|
325
|
-
end
|
326
|
-
|
327
|
-
lambda {
|
328
|
-
ps.parse(['--location', 'singapore'])
|
329
|
-
}.should raise_error CLI::ParsingError::MandatoryOptionsNotSpecifiedError, "mandatory options not specified: --size, --weight"
|
330
|
-
end
|
331
|
-
end
|
332
|
-
|
333
4
|
it "should handle options, switches and then arguments" do
|
334
5
|
ps = CLI.new do
|
335
6
|
option :location, :short => :l
|
@@ -357,240 +28,29 @@ EOF
|
|
357
28
|
ps.debug.should be_true
|
358
29
|
end
|
359
30
|
|
360
|
-
describe "
|
361
|
-
it "
|
362
|
-
|
363
|
-
ps = CLI.new do
|
364
|
-
switch :location
|
365
|
-
switch :location
|
366
|
-
end
|
367
|
-
}.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch location specified twice'
|
368
|
-
|
369
|
-
lambda {
|
370
|
-
ps = CLI.new do
|
371
|
-
option :location
|
372
|
-
option :location
|
373
|
-
end
|
374
|
-
}.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option location specified twice'
|
375
|
-
|
376
|
-
lambda {
|
377
|
-
ps = CLI.new do
|
378
|
-
switch :location
|
379
|
-
option :location
|
380
|
-
end
|
381
|
-
}.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch and option location specified twice'
|
382
|
-
|
383
|
-
lambda {
|
384
|
-
ps = CLI.new do
|
385
|
-
option :location
|
386
|
-
switch :location
|
387
|
-
end
|
388
|
-
}.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option and switch location specified twice'
|
389
|
-
end
|
390
|
-
end
|
391
|
-
|
392
|
-
describe "short name conflict reporting" do
|
393
|
-
it "raise error when short names configlict" do
|
394
|
-
lambda {
|
395
|
-
ps = CLI.new do
|
396
|
-
switch :location, :short => :l
|
397
|
-
switch :location2, :short => :l
|
398
|
-
end
|
399
|
-
}.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch l specified twice'
|
400
|
-
|
401
|
-
lambda {
|
402
|
-
ps = CLI.new do
|
403
|
-
option :location, :short => :l
|
404
|
-
option :location2, :short => :l
|
405
|
-
end
|
406
|
-
}.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short option l specified twice'
|
407
|
-
|
408
|
-
lambda {
|
409
|
-
ps = CLI.new do
|
410
|
-
switch :location, :short => :l
|
411
|
-
option :location2, :short => :l
|
412
|
-
end
|
413
|
-
}.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch and option l specified twice'
|
414
|
-
|
415
|
-
lambda {
|
416
|
-
ps = CLI.new do
|
417
|
-
option :location2, :short => :l
|
418
|
-
switch :location, :short => :l
|
419
|
-
end
|
420
|
-
}.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short option and switch l specified twice'
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
describe "usage and description" do
|
425
|
-
it "parse should set help variable if -h or --help specified in the argument list and not parse the input" do
|
426
|
-
ss = CLI.new do
|
31
|
+
describe "parse!" do
|
32
|
+
it "should return value structure with all the values on successful parsing" do
|
33
|
+
ps = CLI.new do
|
427
34
|
option :location, :short => :l
|
428
|
-
option :group, :default => 'red'
|
429
|
-
option :power_up, :short => :p
|
430
|
-
option :speed, :short => :s, :cast => Integer
|
431
|
-
option :size
|
432
35
|
switch :debug
|
36
|
+
argument :code
|
37
|
+
end.parse!(['-l', 'singapore', '--debug', 'hello'])
|
433
38
|
|
434
|
-
argument :log, :cast => Pathname
|
435
|
-
argument :magick, :default => 'word'
|
436
|
-
argument :test
|
437
|
-
argument :code, :cast => Integer, :default => '123'
|
438
|
-
end
|
439
|
-
|
440
|
-
ps = ss.parse(['-l', 'singapore', '--power-up', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
441
|
-
ps.help.should be_nil
|
442
|
-
ps.location.should == 'singapore'
|
443
|
-
|
444
|
-
ps = ss.parse(['-h', '-l', 'singapore', '--power-up'])
|
445
|
-
ps.help.should be_a String
|
446
|
-
ps.location.should be_nil
|
447
|
-
|
448
|
-
ps = ss.parse(['-l', 'singapore', '--power-up', '-h', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
449
|
-
ps.help.should be_a String
|
450
|
-
ps.location.should be_nil
|
451
|
-
|
452
|
-
ps = ss.parse(['-l', 'singapore', '--power-up', '--help'])
|
453
|
-
ps.help.should be_a String
|
454
|
-
ps.location.should be_nil
|
455
|
-
|
456
|
-
ps = ss.parse(['--help', '-l', 'singapore', '--power-up', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
457
|
-
ps.help.should be_a String
|
458
|
-
ps.location.should be_nil
|
459
|
-
|
460
|
-
ps = ss.parse(['-l', 'singapore', '--power-up', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
461
|
-
ps.help.should be_nil
|
462
39
|
ps.location.should == 'singapore'
|
40
|
+
ps.debug.should be_true
|
41
|
+
ps.code.should == 'hello'
|
463
42
|
end
|
464
43
|
|
465
|
-
it "should
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
ss = CLI.new do
|
476
|
-
option :location, :short => :l, :description => "place where server is located"
|
477
|
-
option :group, :default => 'red'
|
478
|
-
end
|
479
|
-
|
480
|
-
ss.usage.should include("place where server is located")
|
481
|
-
end
|
482
|
-
|
483
|
-
it "should allow describing arguments" do
|
484
|
-
ss = CLI.new do
|
485
|
-
option :group, :default => 'red'
|
486
|
-
argument :log, :cast => Pathname, :description => "log file to process"
|
487
|
-
end
|
488
|
-
|
489
|
-
ss.usage.should include("log file to process")
|
490
|
-
end
|
491
|
-
|
492
|
-
it "should suggest that switches can be used in usage line" do
|
493
|
-
ss = CLI.new do
|
494
|
-
switch :location, :short => :l
|
495
|
-
end
|
496
|
-
|
497
|
-
ss.usage.first.should == "Usage: rspec [switches]\n"
|
498
|
-
end
|
499
|
-
|
500
|
-
it "should suggest that options can be used in usage line" do
|
501
|
-
ss = CLI.new do
|
502
|
-
option :location, :short => :l
|
503
|
-
end
|
504
|
-
|
505
|
-
ss.usage.first.should == "Usage: rspec [options]\n"
|
506
|
-
end
|
507
|
-
|
508
|
-
it "should suggest that switches or options can be used in usage line" do
|
509
|
-
ss = CLI.new do
|
510
|
-
switch :location, :short => :l
|
511
|
-
option :size, :short => :s
|
512
|
-
end
|
513
|
-
|
514
|
-
ss.usage.first.should == "Usage: rspec [switches|options]\n"
|
515
|
-
end
|
516
|
-
|
517
|
-
it "should allow describing whole script" do
|
518
|
-
ss = CLI.new do
|
519
|
-
description 'Log file processor'
|
520
|
-
option :group, :default => 'red'
|
521
|
-
argument :log, :cast => Pathname
|
522
|
-
end
|
523
|
-
|
524
|
-
ss.usage.should include("Log file processor")
|
525
|
-
end
|
526
|
-
|
527
|
-
it "should provide stdin usage information" do
|
528
|
-
CLI.new do
|
529
|
-
stdin
|
530
|
-
end.usage.should include(" < data")
|
531
|
-
|
532
|
-
CLI.new do
|
533
|
-
stdin :log_file
|
534
|
-
end.usage.should include(" < log-file")
|
535
|
-
|
536
|
-
u = CLI.new do
|
537
|
-
stdin :log_file, :description => 'log file to process'
|
538
|
-
end.usage
|
539
|
-
u.should include(" < log-file")
|
540
|
-
u.should include("log file to process")
|
541
|
-
|
542
|
-
u = CLI.new do
|
543
|
-
stdin :log_data, :cast => YAML, :description => 'log data to process'
|
544
|
-
end.usage
|
545
|
-
u.should include(" < log-data")
|
546
|
-
u.should include("log data to process")
|
547
|
-
end
|
548
|
-
|
549
|
-
it "should provide formated usage with optional message" do
|
550
|
-
u = CLI.new do
|
551
|
-
description 'Log file processor'
|
552
|
-
stdin :log_data, :cast => YAML, :description => "YAML formatted log data"
|
553
|
-
switch :debug, :short => :d, :description => "enable debugging"
|
554
|
-
switch :logging, :short => :l
|
555
|
-
switch :run
|
556
|
-
option :location, :short => :r, :description => "place where server is located"
|
557
|
-
option :group, :default => 'red'
|
558
|
-
option :power_up, :short => :p
|
559
|
-
option :speed, :short => :s, :cast => Integer
|
560
|
-
option :the_number_of_the_beast, :short => :b, :cast => Integer, :default => 666, :description => "The number of the beast"
|
561
|
-
option :size
|
562
|
-
|
563
|
-
argument :log, :cast => Pathname, :description => "log file to process"
|
564
|
-
argument :magick, :default => 'word'
|
565
|
-
argument :string
|
566
|
-
argument :number, :cast => Integer
|
567
|
-
argument :code, :cast => Integer, :default => '123', :description => "secret code"
|
568
|
-
argument :illegal_prime, :cast => Integer, :description => "prime number that represents information that it is forbidden to possess or distribute"
|
569
|
-
end.usage
|
570
|
-
|
571
|
-
#puts u
|
572
|
-
|
573
|
-
u.should == <<EOS
|
574
|
-
Usage: rspec [switches|options] log magick string number code illegal-prime < log-data
|
575
|
-
Log file processor
|
576
|
-
Input:
|
577
|
-
log-data - YAML formatted log data
|
578
|
-
Switches:
|
579
|
-
--debug (-d) - enable debugging
|
580
|
-
--logging (-l)
|
581
|
-
--run
|
582
|
-
Options:
|
583
|
-
--location (-r) - place where server is located
|
584
|
-
--group [red]
|
585
|
-
--power-up (-p)
|
586
|
-
--speed (-s)
|
587
|
-
--the-number-of-the-beast (-b) [666] - The number of the beast
|
588
|
-
--size
|
589
|
-
Arguments:
|
590
|
-
log - log file to process
|
591
|
-
code - secret code
|
592
|
-
illegal-prime - prime number that represents information that it is forbidden to possess or distribute
|
593
|
-
EOS
|
44
|
+
it "should exit displaying usage and error message on standard error on usage error" do
|
45
|
+
out = stderr_read do
|
46
|
+
lambda {
|
47
|
+
ps = CLI.new do
|
48
|
+
option :weight, :required => true
|
49
|
+
end.parse!([])
|
50
|
+
}.should raise_error SystemExit
|
51
|
+
end
|
52
|
+
out.should include('Error: mandatory options not specified: --weight')
|
53
|
+
out.should include('Usage:')
|
594
54
|
end
|
595
55
|
end
|
596
56
|
end
|