cli 1.1.0 → 1.1.1
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/VERSION +1 -1
- data/cli.gemspec +9 -8
- data/lib/cli.rb +25 -15
- data/lib/cli/arguments.rb +2 -2
- data/lib/cli/dsl.rb +12 -4
- data/lib/cli/options.rb +6 -6
- data/spec/option_spec.rb +15 -0
- metadata +32 -20
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.1
|
data/cli.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "1.1.
|
7
|
+
s.name = %q{cli}
|
8
|
+
s.version = "1.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jakub Pastuszek"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = %q{2012-02-01}
|
13
|
+
s.description = %q{Command Line Interface gem allows you to quickly specify command argument parser that will automatically generate usage, handle stdin, switches, options and arguments with default values and value casting}
|
14
|
+
s.email = %q{jpastuszek@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
17
|
"README.md"
|
@@ -48,13 +48,14 @@ Gem::Specification.new do |s|
|
|
48
48
|
"spec/switch_spec.rb",
|
49
49
|
"spec/usage_spec.rb"
|
50
50
|
]
|
51
|
-
s.homepage =
|
51
|
+
s.homepage = %q{http://github.com/jpastuszek/cli}
|
52
52
|
s.licenses = ["MIT"]
|
53
53
|
s.require_paths = ["lib"]
|
54
|
-
s.rubygems_version =
|
55
|
-
s.summary =
|
54
|
+
s.rubygems_version = %q{1.3.7}
|
55
|
+
s.summary = %q{Command line argument parser with stdin handling and usage generator}
|
56
56
|
|
57
57
|
if s.respond_to? :specification_version then
|
58
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
58
59
|
s.specification_version = 3
|
59
60
|
|
60
61
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
data/lib/cli.rb
CHANGED
@@ -98,7 +98,7 @@ class CLI
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def append(argument, value)
|
101
|
-
v = (
|
101
|
+
v = (get(argument) or [])
|
102
102
|
v << value
|
103
103
|
send((argument.name.to_s + '=').to_sym, v)
|
104
104
|
end
|
@@ -106,6 +106,10 @@ class CLI
|
|
106
106
|
def set(argument)
|
107
107
|
value(argument, true)
|
108
108
|
end
|
109
|
+
|
110
|
+
def get(argument)
|
111
|
+
send(argument.name.to_s)
|
112
|
+
end
|
109
113
|
end
|
110
114
|
|
111
115
|
def initialize(&block)
|
@@ -146,7 +150,7 @@ class CLI
|
|
146
150
|
|
147
151
|
@arguments << arguments_dsl
|
148
152
|
|
149
|
-
raise ParserError::MultipleArgumentsSpecifierError.new(@arguments.
|
153
|
+
raise ParserError::MultipleArgumentsSpecifierError.new(@arguments.multiary) if @arguments.multiary.length > 1
|
150
154
|
end
|
151
155
|
|
152
156
|
def switch(name, options = {})
|
@@ -196,15 +200,10 @@ class CLI
|
|
196
200
|
end
|
197
201
|
|
198
202
|
# initialize multi options
|
199
|
-
@options.
|
203
|
+
@options.multiary.each do |o|
|
200
204
|
values.value(o, [])
|
201
205
|
end
|
202
206
|
|
203
|
-
# set defaults
|
204
|
-
@options.defaults.each do |o|
|
205
|
-
values.value(o, o.cast(o.default))
|
206
|
-
end
|
207
|
-
|
208
207
|
# process switches
|
209
208
|
mandatory_options = @options.mandatory.dup
|
210
209
|
|
@@ -215,7 +214,7 @@ class CLI
|
|
215
214
|
values.set(switch)
|
216
215
|
elsif option = @options.find(arg)
|
217
216
|
value = argv.shift or raise ParsingError::MissingOptionValueError.new(option)
|
218
|
-
if option.
|
217
|
+
if option.multiary?
|
219
218
|
values.append(option, option.cast(value))
|
220
219
|
else
|
221
220
|
values.value(option, option.cast(value))
|
@@ -226,18 +225,29 @@ class CLI
|
|
226
225
|
end
|
227
226
|
end
|
228
227
|
|
229
|
-
|
228
|
+
# set defaults
|
229
|
+
@options.unarry.each do |o|
|
230
|
+
next unless o.has_default?
|
231
|
+
values.value(o, o.default_cast) if values.get(o) == nil
|
232
|
+
end
|
233
|
+
|
234
|
+
@options.multiary.each do |o|
|
235
|
+
next unless o.has_default?
|
236
|
+
values.value(o, o.default_cast) if values.get(o) == []
|
237
|
+
end
|
230
238
|
|
231
239
|
# check mandatory options
|
232
240
|
raise ParsingError::MandatoryOptionsNotSpecifiedError.new(mandatory_options) unless mandatory_options.empty?
|
233
241
|
|
242
|
+
argv.shift if argv.first == '--'
|
243
|
+
|
234
244
|
# process arguments
|
235
245
|
arguments = @arguments.dup
|
236
246
|
while argument = arguments.shift
|
237
247
|
value = if arguments.mandatory.length < argv.length
|
238
248
|
arg = argv.shift
|
239
249
|
|
240
|
-
if argument.
|
250
|
+
if argument.multiary?
|
241
251
|
v = [arg]
|
242
252
|
while argv.length > arguments.length
|
243
253
|
v << argv.shift
|
@@ -250,7 +260,7 @@ class CLI
|
|
250
260
|
if argument.has_default?
|
251
261
|
argument.default
|
252
262
|
elsif not argument.mandatory?
|
253
|
-
argument.
|
263
|
+
argument.multiary? ? [] : nil
|
254
264
|
else
|
255
265
|
raise ParsingError::MandatoryArgumentNotSpecifiedError.new(argument) if argv.empty?
|
256
266
|
end
|
@@ -309,7 +319,7 @@ class CLI
|
|
309
319
|
out.print(@arguments.map do |a|
|
310
320
|
v = ''
|
311
321
|
v += '[' unless a.mandatory?
|
312
|
-
v += a.
|
322
|
+
v += a.multiary? ? a.to_s + '*': a.to_s
|
313
323
|
v += ']' unless a.mandatory?
|
314
324
|
v
|
315
325
|
end.join(' '))
|
@@ -337,7 +347,7 @@ class CLI
|
|
337
347
|
unless @options.empty?
|
338
348
|
out.puts "Options:"
|
339
349
|
@options.each do |o|
|
340
|
-
unless o.
|
350
|
+
unless o.multiary?
|
341
351
|
out.print " #{o.switch}"
|
342
352
|
else
|
343
353
|
out.print " #{o.switch}*"
|
@@ -353,7 +363,7 @@ class CLI
|
|
353
363
|
unless @arguments.empty?
|
354
364
|
out.puts "Arguments:"
|
355
365
|
@arguments.each do |a|
|
356
|
-
unless a.
|
366
|
+
unless a.multiary?
|
357
367
|
out.print " #{a}"
|
358
368
|
else
|
359
369
|
out.print " #{a}*"
|
data/lib/cli/arguments.rb
CHANGED
data/lib/cli/dsl.rb
CHANGED
@@ -57,6 +57,10 @@ class CLI
|
|
57
57
|
@options[:default].to_s
|
58
58
|
end
|
59
59
|
|
60
|
+
def default_cast
|
61
|
+
cast(default)
|
62
|
+
end
|
63
|
+
|
60
64
|
def has_default?
|
61
65
|
@options.member? :default
|
62
66
|
end
|
@@ -71,6 +75,10 @@ class CLI
|
|
71
75
|
value = @options[:default]
|
72
76
|
value.is_a?(Array) ? value.map{|v| v.to_s} : [value.to_s]
|
73
77
|
end
|
78
|
+
|
79
|
+
def default_cast
|
80
|
+
default.map{|d| cast(d)}
|
81
|
+
end
|
74
82
|
end
|
75
83
|
|
76
84
|
class Input < DSL::Base
|
@@ -96,7 +104,7 @@ class CLI
|
|
96
104
|
name.to_s.tr('_', '-')
|
97
105
|
end
|
98
106
|
|
99
|
-
def
|
107
|
+
def multiary?
|
100
108
|
false
|
101
109
|
end
|
102
110
|
end
|
@@ -112,7 +120,7 @@ class CLI
|
|
112
120
|
out
|
113
121
|
end
|
114
122
|
|
115
|
-
def
|
123
|
+
def multiary?
|
116
124
|
true
|
117
125
|
end
|
118
126
|
end
|
@@ -153,7 +161,7 @@ class CLI
|
|
153
161
|
include DSL::Value
|
154
162
|
include DSL::Cast
|
155
163
|
|
156
|
-
def
|
164
|
+
def multiary?
|
157
165
|
false
|
158
166
|
end
|
159
167
|
end
|
@@ -161,7 +169,7 @@ class CLI
|
|
161
169
|
class Options < Option
|
162
170
|
include DSL::MultiDefault
|
163
171
|
|
164
|
-
def
|
172
|
+
def multiary?
|
165
173
|
true
|
166
174
|
end
|
167
175
|
end
|
data/lib/cli/options.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'cli/switches'
|
2
2
|
|
3
3
|
class CLI::Options < CLI::Switches
|
4
|
-
def defaults
|
5
|
-
select{|o| o.has_default?}
|
6
|
-
end
|
7
|
-
|
8
4
|
def mandatory
|
9
5
|
select{|o| o.mandatory?}
|
10
6
|
end
|
@@ -13,8 +9,12 @@ class CLI::Options < CLI::Switches
|
|
13
9
|
select{|o| not o.mandatory?}
|
14
10
|
end
|
15
11
|
|
16
|
-
def
|
17
|
-
select{|a| a.
|
12
|
+
def unarry
|
13
|
+
select{|a| not a.multiary?}
|
14
|
+
end
|
15
|
+
|
16
|
+
def multiary
|
17
|
+
select{|a| a.multiary?}
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
data/spec/option_spec.rb
CHANGED
@@ -34,6 +34,21 @@ describe CLI do
|
|
34
34
|
ps.size.should == [24, 10]
|
35
35
|
end
|
36
36
|
|
37
|
+
it "should support casting of multiple options with default" do
|
38
|
+
ps = CLI.new do
|
39
|
+
options :log_file, :cast => Pathname, :default => 'test.log'
|
40
|
+
end.parse(['--log-file', 'server.log', '--log-file', 'error.log'])
|
41
|
+
|
42
|
+
ps.log_file.should be_a Array
|
43
|
+
ps.log_file.length.should == 2
|
44
|
+
|
45
|
+
ps.log_file.first.should be_a Pathname
|
46
|
+
ps.log_file.first.to_s.should == 'server.log'
|
47
|
+
|
48
|
+
ps.log_file.last.should be_a Pathname
|
49
|
+
ps.log_file.last.to_s.should == 'error.log'
|
50
|
+
end
|
51
|
+
|
37
52
|
it "should support casting with lambda" do
|
38
53
|
ps = CLI.new do
|
39
54
|
option :size, :cast => lambda{|v| v.to_i + 2}
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 17
|
5
|
+
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
|
9
|
+
- 1
|
10
|
+
segments_generated: true
|
11
|
+
version: 1.1.1
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- Jakub Pastuszek
|
@@ -15,10 +16,10 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2012-01
|
19
|
+
date: 2012-02-01 00:00:00 +01:00
|
20
|
+
default_executable:
|
19
21
|
dependencies:
|
20
22
|
- !ruby/object:Gem::Dependency
|
21
|
-
type: :development
|
22
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
23
24
|
none: false
|
24
25
|
requirements:
|
@@ -29,12 +30,13 @@ dependencies:
|
|
29
30
|
- 2
|
30
31
|
- 3
|
31
32
|
- 0
|
33
|
+
segments_generated: true
|
32
34
|
version: 2.3.0
|
33
|
-
|
35
|
+
type: :development
|
34
36
|
name: rspec
|
37
|
+
prerelease: false
|
35
38
|
version_requirements: *id001
|
36
39
|
- !ruby/object:Gem::Dependency
|
37
|
-
type: :development
|
38
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
41
|
none: false
|
40
42
|
requirements:
|
@@ -43,12 +45,13 @@ dependencies:
|
|
43
45
|
hash: 3
|
44
46
|
segments:
|
45
47
|
- 0
|
48
|
+
segments_generated: true
|
46
49
|
version: "0"
|
47
|
-
|
50
|
+
type: :development
|
48
51
|
name: cucumber
|
52
|
+
prerelease: false
|
49
53
|
version_requirements: *id002
|
50
54
|
- !ruby/object:Gem::Dependency
|
51
|
-
type: :development
|
52
55
|
requirement: &id003 !ruby/object:Gem::Requirement
|
53
56
|
none: false
|
54
57
|
requirements:
|
@@ -59,12 +62,13 @@ dependencies:
|
|
59
62
|
- 1
|
60
63
|
- 0
|
61
64
|
- 0
|
65
|
+
segments_generated: true
|
62
66
|
version: 1.0.0
|
63
|
-
|
67
|
+
type: :development
|
64
68
|
name: bundler
|
69
|
+
prerelease: false
|
65
70
|
version_requirements: *id003
|
66
71
|
- !ruby/object:Gem::Dependency
|
67
|
-
type: :development
|
68
72
|
requirement: &id004 !ruby/object:Gem::Requirement
|
69
73
|
none: false
|
70
74
|
requirements:
|
@@ -75,12 +79,13 @@ dependencies:
|
|
75
79
|
- 1
|
76
80
|
- 6
|
77
81
|
- 4
|
82
|
+
segments_generated: true
|
78
83
|
version: 1.6.4
|
79
|
-
|
84
|
+
type: :development
|
80
85
|
name: jeweler
|
86
|
+
prerelease: false
|
81
87
|
version_requirements: *id004
|
82
88
|
- !ruby/object:Gem::Dependency
|
83
|
-
type: :development
|
84
89
|
requirement: &id005 !ruby/object:Gem::Requirement
|
85
90
|
none: false
|
86
91
|
requirements:
|
@@ -89,12 +94,13 @@ dependencies:
|
|
89
94
|
hash: 3
|
90
95
|
segments:
|
91
96
|
- 0
|
97
|
+
segments_generated: true
|
92
98
|
version: "0"
|
93
|
-
|
99
|
+
type: :development
|
94
100
|
name: rcov
|
101
|
+
prerelease: false
|
95
102
|
version_requirements: *id005
|
96
103
|
- !ruby/object:Gem::Dependency
|
97
|
-
type: :development
|
98
104
|
requirement: &id006 !ruby/object:Gem::Requirement
|
99
105
|
none: false
|
100
106
|
requirements:
|
@@ -104,12 +110,13 @@ dependencies:
|
|
104
110
|
segments:
|
105
111
|
- 3
|
106
112
|
- 9
|
113
|
+
segments_generated: true
|
107
114
|
version: "3.9"
|
108
|
-
|
115
|
+
type: :development
|
109
116
|
name: rdoc
|
117
|
+
prerelease: false
|
110
118
|
version_requirements: *id006
|
111
119
|
- !ruby/object:Gem::Dependency
|
112
|
-
type: :development
|
113
120
|
requirement: &id007 !ruby/object:Gem::Requirement
|
114
121
|
none: false
|
115
122
|
requirements:
|
@@ -119,9 +126,11 @@ dependencies:
|
|
119
126
|
segments:
|
120
127
|
- 0
|
121
128
|
- 9
|
129
|
+
segments_generated: true
|
122
130
|
version: "0.9"
|
123
|
-
|
131
|
+
type: :development
|
124
132
|
name: ruby-ip
|
133
|
+
prerelease: false
|
125
134
|
version_requirements: *id007
|
126
135
|
description: Command Line Interface gem allows you to quickly specify command argument parser that will automatically generate usage, handle stdin, switches, options and arguments with default values and value casting
|
127
136
|
email: jpastuszek@gmail.com
|
@@ -163,6 +172,7 @@ files:
|
|
163
172
|
- spec/stdin_spec.rb
|
164
173
|
- spec/switch_spec.rb
|
165
174
|
- spec/usage_spec.rb
|
175
|
+
has_rdoc: true
|
166
176
|
homepage: http://github.com/jpastuszek/cli
|
167
177
|
licenses:
|
168
178
|
- MIT
|
@@ -179,6 +189,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
179
189
|
hash: 3
|
180
190
|
segments:
|
181
191
|
- 0
|
192
|
+
segments_generated: true
|
182
193
|
version: "0"
|
183
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
184
195
|
none: false
|
@@ -188,11 +199,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
188
199
|
hash: 3
|
189
200
|
segments:
|
190
201
|
- 0
|
202
|
+
segments_generated: true
|
191
203
|
version: "0"
|
192
204
|
requirements: []
|
193
205
|
|
194
206
|
rubyforge_project:
|
195
|
-
rubygems_version: 1.
|
207
|
+
rubygems_version: 1.3.7
|
196
208
|
signing_key:
|
197
209
|
specification_version: 3
|
198
210
|
summary: Command line argument parser with stdin handling and usage generator
|