cli 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- 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/spec/usage_spec.rb
ADDED
@@ -0,0 +1,303 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe CLI do
|
4
|
+
describe "usage and description" do
|
5
|
+
describe "help switch" do
|
6
|
+
it "parse should set help variable if -h or --help specified in the argument list and not parse the input" do
|
7
|
+
ss = CLI.new do
|
8
|
+
option :location, :short => :l
|
9
|
+
option :group, :default => 'red'
|
10
|
+
option :power_up, :short => :p
|
11
|
+
option :speed, :short => :s, :cast => Integer
|
12
|
+
option :size
|
13
|
+
switch :debug
|
14
|
+
|
15
|
+
argument :log, :cast => Pathname
|
16
|
+
argument :magick, :default => 'word'
|
17
|
+
argument :test
|
18
|
+
argument :code, :cast => Integer, :default => '123'
|
19
|
+
end
|
20
|
+
|
21
|
+
ps = ss.parse(['-l', 'singapore', '--power-up', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
22
|
+
ps.help.should be_nil
|
23
|
+
ps.location.should == 'singapore'
|
24
|
+
|
25
|
+
ps = ss.parse(['-h', '-l', 'singapore', '--power-up'])
|
26
|
+
ps.help.should be_a String
|
27
|
+
ps.location.should be_nil
|
28
|
+
|
29
|
+
ps = ss.parse(['-l', 'singapore', '--power-up', '-h', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
30
|
+
ps.help.should be_a String
|
31
|
+
ps.location.should be_nil
|
32
|
+
|
33
|
+
ps = ss.parse(['-l', 'singapore', '--power-up', '--help'])
|
34
|
+
ps.help.should be_a String
|
35
|
+
ps.location.should be_nil
|
36
|
+
|
37
|
+
ps = ss.parse(['--help', '-l', 'singapore', '--power-up', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
38
|
+
ps.help.should be_a String
|
39
|
+
ps.location.should be_nil
|
40
|
+
|
41
|
+
ps = ss.parse(['-l', 'singapore', '--power-up', 'yes', '-s', '24', '--size', 'XXXL', '/tmp', 'hello'])
|
42
|
+
ps.help.should be_nil
|
43
|
+
ps.location.should == 'singapore'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "parse! should cause program to exit and display help message on --help or -h switch" do
|
47
|
+
stdout_read do
|
48
|
+
lambda {
|
49
|
+
ps = CLI.new do
|
50
|
+
end.parse!(['--help'])
|
51
|
+
}.should raise_error SystemExit
|
52
|
+
end.should include('Usage:')
|
53
|
+
|
54
|
+
stdout_read do
|
55
|
+
lambda {
|
56
|
+
ps = CLI.new do
|
57
|
+
end.parse!(['-h'])
|
58
|
+
}.should raise_error SystemExit
|
59
|
+
end.should include('Usage:')
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should reserve --help and -h switches" do
|
63
|
+
lambda {
|
64
|
+
ps = CLI.new do
|
65
|
+
switch :help
|
66
|
+
end
|
67
|
+
}.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch help specified twice'
|
68
|
+
|
69
|
+
lambda {
|
70
|
+
ps = CLI.new do
|
71
|
+
switch :help2, :short => :h
|
72
|
+
end
|
73
|
+
}.should raise_error CLI::ParserError::ShortNameSpecifiedTwiceError, 'short switch h specified twice'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should display help switch in the help message as the last entry" do
|
77
|
+
CLI.new do
|
78
|
+
switch :test
|
79
|
+
end.usage.should include("--test\n --help (-h) - display this help message")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "version" do
|
84
|
+
it "should allow specifing version number that will be accessibel via --version switch" do
|
85
|
+
ps = CLI.new do
|
86
|
+
version '1.0.2'
|
87
|
+
end.parse(['--version'])
|
88
|
+
ps.version.should == "rspec version \"1.0.2\"\n"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "parse! should cause program to exit displyaing version on --version switch" do
|
92
|
+
stdout_read do
|
93
|
+
lambda {
|
94
|
+
ps = CLI.new do
|
95
|
+
version "1.2.3"
|
96
|
+
end.parse!(['--version'])
|
97
|
+
}.should raise_error SystemExit
|
98
|
+
end.should == "rspec version \"1.2.3\"\n"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should display version switch in the help message as the last entry when version is specified" do
|
102
|
+
CLI.new do
|
103
|
+
end.usage.should_not include("--version")
|
104
|
+
|
105
|
+
CLI.new do
|
106
|
+
version '1.0.2'
|
107
|
+
end.usage.should include("--help (-h) - display this help message\n --version - display version string")
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should reserve --version switch" do
|
111
|
+
lambda {
|
112
|
+
ps = CLI.new do
|
113
|
+
switch :version
|
114
|
+
end
|
115
|
+
}.should_not raise_error
|
116
|
+
|
117
|
+
lambda {
|
118
|
+
ps = CLI.new do
|
119
|
+
version '1.0.2'
|
120
|
+
switch :version
|
121
|
+
end
|
122
|
+
}.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'switch version specified twice'
|
123
|
+
|
124
|
+
lambda {
|
125
|
+
ps = CLI.new do
|
126
|
+
version '1.0.2'
|
127
|
+
option :version
|
128
|
+
end
|
129
|
+
}.should raise_error CLI::ParserError::LongNameSpecifiedTwiceError, 'option and switch version specified twice'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should allow describing switches" do
|
134
|
+
ss = CLI.new do
|
135
|
+
switch :debug, :short => :d, :description => "enable debugging"
|
136
|
+
switch :logging
|
137
|
+
end
|
138
|
+
|
139
|
+
ss.usage.should include("enable debugging")
|
140
|
+
end
|
141
|
+
|
142
|
+
it "switch description should be casted to string" do
|
143
|
+
ss = CLI.new do
|
144
|
+
switch :debug, :short => :d, :description => 2 + 2
|
145
|
+
switch :logging
|
146
|
+
end
|
147
|
+
|
148
|
+
ss.usage.should include("4")
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should allow describing options" do
|
152
|
+
ss = CLI.new do
|
153
|
+
option :location, :short => :l, :description => "place where server is located"
|
154
|
+
option :group, :default => 'red'
|
155
|
+
end
|
156
|
+
|
157
|
+
ss.usage.should include("place where server is located")
|
158
|
+
end
|
159
|
+
|
160
|
+
it "option description should be casted to string" do
|
161
|
+
ss = CLI.new do
|
162
|
+
option :location, :short => :l, :description => 2 + 2
|
163
|
+
option :group, :default => 'red'
|
164
|
+
end
|
165
|
+
|
166
|
+
ss.usage.should include("4")
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should allow describing arguments" do
|
170
|
+
ss = CLI.new do
|
171
|
+
option :group, :default => 'red'
|
172
|
+
argument :log, :cast => Pathname, :description => "log file to process"
|
173
|
+
end
|
174
|
+
|
175
|
+
ss.usage.should include("log file to process")
|
176
|
+
end
|
177
|
+
|
178
|
+
it "argument description should be casted to string" do
|
179
|
+
ss = CLI.new do
|
180
|
+
option :group, :default => 'red'
|
181
|
+
argument :log, :cast => Pathname, :description => 2 + 2
|
182
|
+
end
|
183
|
+
|
184
|
+
ss.usage.should include("4")
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should suggest that arguments can be used in usage line" do
|
188
|
+
ss = CLI.new do
|
189
|
+
argument :location
|
190
|
+
end
|
191
|
+
|
192
|
+
# switches will always be there due to implicit --help switch
|
193
|
+
ss.usage.first.should == "Usage: rspec [switches] [--] location\n"
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should suggest that switches can be used in usage line" do
|
197
|
+
ss = CLI.new do
|
198
|
+
switch :location, :short => :l
|
199
|
+
end
|
200
|
+
|
201
|
+
ss.usage.first.should == "Usage: rspec [switches]\n"
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should suggest that options can be used in usage line" do
|
205
|
+
ss = CLI.new do
|
206
|
+
option :location, :short => :l
|
207
|
+
end
|
208
|
+
|
209
|
+
# switches will always be there due to implicit --help switch
|
210
|
+
ss.usage.first.should == "Usage: rspec [switches|options]\n"
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should suggest that switches or options can be used in usage line" do
|
214
|
+
ss = CLI.new do
|
215
|
+
switch :location, :short => :l
|
216
|
+
option :size, :short => :s
|
217
|
+
end
|
218
|
+
|
219
|
+
ss.usage.first.should == "Usage: rspec [switches|options]\n"
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should allow describing whole script" do
|
223
|
+
ss = CLI.new do
|
224
|
+
description 'Log file processor'
|
225
|
+
option :group, :default => 'red'
|
226
|
+
argument :log, :cast => Pathname
|
227
|
+
end
|
228
|
+
|
229
|
+
ss.usage.should include("Log file processor")
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should provide stdin usage information" do
|
233
|
+
CLI.new do
|
234
|
+
stdin
|
235
|
+
end.usage.should include(" < data")
|
236
|
+
|
237
|
+
CLI.new do
|
238
|
+
stdin :log_file
|
239
|
+
end.usage.should include(" < log-file")
|
240
|
+
|
241
|
+
u = CLI.new do
|
242
|
+
stdin :log_file, :description => 'log file to process'
|
243
|
+
end.usage
|
244
|
+
u.should include(" < log-file")
|
245
|
+
u.should include("log file to process")
|
246
|
+
|
247
|
+
u = CLI.new do
|
248
|
+
stdin :log_data, :cast => YAML, :description => 'log data to process'
|
249
|
+
end.usage
|
250
|
+
u.should include(" < log-data")
|
251
|
+
u.should include("log data to process")
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should provide formated usage with optional message" do
|
255
|
+
u = CLI.new do
|
256
|
+
description 'Log file processor'
|
257
|
+
version '1.0'
|
258
|
+
stdin :log_data, :cast => YAML, :description => "YAML formatted log data"
|
259
|
+
switch :debug, :short => :d, :description => "enable debugging"
|
260
|
+
switch :logging, :short => :l
|
261
|
+
switch :run
|
262
|
+
option :location, :short => :r, :description => "place where server is located"
|
263
|
+
option :group, :default => 'red'
|
264
|
+
option :power_up, :short => :p
|
265
|
+
option :speed, :short => :s, :cast => Integer
|
266
|
+
option :the_number_of_the_beast, :short => :b, :cast => Integer, :default => 666, :description => "The number of the beast"
|
267
|
+
option :size
|
268
|
+
|
269
|
+
argument :log, :cast => Pathname, :description => "log file to process"
|
270
|
+
argument :magick, :default => 'word'
|
271
|
+
argument :string
|
272
|
+
argument :number, :cast => Integer
|
273
|
+
argument :code, :cast => Integer, :default => '123', :description => "secret code"
|
274
|
+
argument :illegal_prime, :cast => Integer, :description => "prime number that represents information that it is forbidden to possess or distribute"
|
275
|
+
end.usage
|
276
|
+
|
277
|
+
u.should == <<EOS
|
278
|
+
Usage: rspec [switches|options] [--] log magick string number code illegal-prime < log-data
|
279
|
+
Log file processor
|
280
|
+
Input:
|
281
|
+
log-data - YAML formatted log data
|
282
|
+
Switches:
|
283
|
+
--debug (-d) - enable debugging
|
284
|
+
--logging (-l)
|
285
|
+
--run
|
286
|
+
--help (-h) - display this help message
|
287
|
+
--version - display version string
|
288
|
+
Options:
|
289
|
+
--location (-r) - place where server is located
|
290
|
+
--group [red]
|
291
|
+
--power-up (-p)
|
292
|
+
--speed (-s)
|
293
|
+
--the-number-of-the-beast (-b) [666] - The number of the beast
|
294
|
+
--size
|
295
|
+
Arguments:
|
296
|
+
log - log file to process
|
297
|
+
code - secret code
|
298
|
+
illegal-prime - prime number that represents information that it is forbidden to possess or distribute
|
299
|
+
EOS
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.4
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jakub Pastuszek
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-12-
|
18
|
+
date: 2011-12-16 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
type: :development
|
@@ -131,17 +131,19 @@ extensions: []
|
|
131
131
|
|
132
132
|
extra_rdoc_files:
|
133
133
|
- LICENSE.txt
|
134
|
-
- README.
|
134
|
+
- README.md
|
135
135
|
files:
|
136
136
|
- .document
|
137
137
|
- .rspec
|
138
138
|
- Gemfile
|
139
139
|
- Gemfile.lock
|
140
140
|
- LICENSE.txt
|
141
|
-
- README.
|
141
|
+
- README.md
|
142
142
|
- Rakefile
|
143
143
|
- VERSION
|
144
144
|
- cli.gemspec
|
145
|
+
- examples/processor
|
146
|
+
- examples/sinatra
|
145
147
|
- features/cli.feature
|
146
148
|
- features/step_definitions/cli_steps.rb
|
147
149
|
- features/support/env.rb
|
@@ -150,8 +152,15 @@ files:
|
|
150
152
|
- lib/cli/dsl.rb
|
151
153
|
- lib/cli/options.rb
|
152
154
|
- lib/cli/switches.rb
|
155
|
+
- spec/argument_spec.rb
|
153
156
|
- spec/cli_spec.rb
|
157
|
+
- spec/conflict_reporting_spec.rb
|
158
|
+
- spec/option_spec.rb
|
159
|
+
- spec/separator_spec.rb
|
154
160
|
- spec/spec_helper.rb
|
161
|
+
- spec/stdin_spec.rb
|
162
|
+
- spec/switch_spec.rb
|
163
|
+
- spec/usage_spec.rb
|
155
164
|
homepage: http://github.com/jpastuszek/cli
|
156
165
|
licenses:
|
157
166
|
- MIT
|
data/README.rdoc
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
= cli
|
2
|
-
|
3
|
-
Description goes here.
|
4
|
-
|
5
|
-
== Contributing to cli
|
6
|
-
|
7
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
-
* Fork the project
|
10
|
-
* Start a feature/bugfix branch
|
11
|
-
* Commit and push until you are happy with your contribution
|
12
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
-
|
15
|
-
== Copyright
|
16
|
-
|
17
|
-
Copyright (c) 2011 Jakub Pastuszek. See LICENSE.txt for
|
18
|
-
further details.
|
19
|
-
|