choice 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/{README → README.rdoc} +0 -0
- data/lib/choice.rb +6 -0
- data/lib/choice/parser.rb +7 -3
- data/lib/choice/version.rb +1 -1
- data/test/test_parser.rb +52 -53
- metadata +18 -6
data/CHANGELOG
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
0.1.4:
|
2
|
+
- Monkeypatch to Hash for #index deprecation. Only applied on RUBY_VERSION >= "1.9"
|
3
|
+
- Fixed rake file for README filename change
|
4
|
+
- Fixed a broken test that's been broken since 2009.
|
5
|
+
|
1
6
|
0.1.3:
|
2
7
|
- Added args_of method to retrieve the arguments of an option
|
3
8
|
|
data/{README → README.rdoc}
RENAMED
File without changes
|
data/lib/choice.rb
CHANGED
@@ -4,6 +4,12 @@ require 'choice/parser'
|
|
4
4
|
require 'choice/writer'
|
5
5
|
require 'choice/lazyhash'
|
6
6
|
|
7
|
+
if RUBY_VERSION < "1.9"
|
8
|
+
class Hash
|
9
|
+
alias_method(:key, :index) unless method_defined?(:key)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
7
13
|
#
|
8
14
|
# Usage of this module is lovingly detailed in the README file.
|
9
15
|
#
|
data/lib/choice/parser.rb
CHANGED
@@ -105,7 +105,7 @@ module Choice
|
|
105
105
|
|
106
106
|
# Add this value to the choices hash with the key of the option's
|
107
107
|
# name. If we expect an array, tack this argument on.
|
108
|
-
name = hashes['shorts'].
|
108
|
+
name = hashes['shorts'].key(arg)
|
109
109
|
if arrayed[name]
|
110
110
|
choices[name] ||= []
|
111
111
|
choices[name] << value unless value.nil?
|
@@ -119,7 +119,7 @@ module Choice
|
|
119
119
|
|
120
120
|
# Grab values from --long=VALUE format
|
121
121
|
name, value = arg.split('=', 2)
|
122
|
-
name = longs.
|
122
|
+
name = longs.key(name)
|
123
123
|
|
124
124
|
if value.nil? && args.first !~ /^-/
|
125
125
|
# Grab value otherwise if not in --long=VALUE format. Assume --long VALUE.
|
@@ -178,7 +178,11 @@ module Choice
|
|
178
178
|
|
179
179
|
# Now that we've done all that, re-set the element of the choice hash
|
180
180
|
# with the (potentially) new value.
|
181
|
-
|
181
|
+
if arrayed[name] && choices[name].empty?
|
182
|
+
choices[name] = true
|
183
|
+
else
|
184
|
+
choices[name] = value
|
185
|
+
end
|
182
186
|
end
|
183
187
|
|
184
188
|
# Die if we're missing any required arguments
|
data/lib/choice/version.rb
CHANGED
data/test/test_parser.rb
CHANGED
@@ -7,7 +7,7 @@ class TestParser < Test::Unit::TestCase
|
|
7
7
|
def setup
|
8
8
|
@options = {}
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def test_parse_options
|
12
12
|
@options['band'] = Choice::Option.new do
|
13
13
|
short '-b'
|
@@ -20,36 +20,36 @@ class TestParser < Test::Unit::TestCase
|
|
20
20
|
long '--animal=ANIMAL'
|
21
21
|
cast String
|
22
22
|
desc 'Your favorite animal.'
|
23
|
-
end
|
23
|
+
end
|
24
24
|
band = 'Led Zeppelin'
|
25
25
|
animal = 'Reindeer'
|
26
|
-
|
26
|
+
|
27
27
|
args = ['-b', band, "--animal=#{animal}"]
|
28
|
-
|
28
|
+
|
29
29
|
choices, rest = Choice::Parser.parse(@options, args)
|
30
|
-
|
30
|
+
|
31
31
|
assert_equal band, choices['band']
|
32
32
|
assert_equal animal, choices['animal']
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def test_parse_no_options
|
36
36
|
assert_equal Hash.new, Choice::Parser.parse(nil, nil)
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def test_parse_default
|
40
40
|
@options['soda'] = Choice::Option.new do
|
41
41
|
short '-s'
|
42
42
|
long '--soda=SODA'
|
43
43
|
default 'PibbJr'
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
args = []
|
47
|
-
|
47
|
+
|
48
48
|
choices, rest = Choice::Parser.parse(@options, args)
|
49
|
-
|
49
|
+
|
50
50
|
assert_equal 'PibbJr', choices['soda']
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
def test_parse_options_with_filters
|
54
54
|
@options['host'] = Choice::Option.new do
|
55
55
|
short '-h'
|
@@ -57,27 +57,27 @@ class TestParser < Test::Unit::TestCase
|
|
57
57
|
opt.gsub!(/[^\w]/, '')
|
58
58
|
opt = opt.sub(/k/, 'c')
|
59
59
|
end
|
60
|
-
end
|
60
|
+
end
|
61
61
|
host = 'de.fun.kt'
|
62
62
|
args = ['-h', host]
|
63
63
|
choices, rest = Choice::Parser.parse(@options, args)
|
64
|
-
|
64
|
+
|
65
65
|
assert_equal 'defunct', choices['host']
|
66
|
-
end
|
67
|
-
|
66
|
+
end
|
67
|
+
|
68
68
|
def test_casting
|
69
69
|
@options['port'] = Choice::Option.new do
|
70
70
|
short '-p'
|
71
71
|
cast Integer
|
72
72
|
end
|
73
|
-
|
73
|
+
|
74
74
|
port = '3000'
|
75
75
|
args = ['-p', port]
|
76
76
|
choices, rest = Choice::Parser.parse(@options, args)
|
77
|
-
|
77
|
+
|
78
78
|
assert_equal port.to_i, choices['port']
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
def test_text_required
|
82
82
|
@options['name'] = Choice::Option.new do
|
83
83
|
short '-n'
|
@@ -88,29 +88,29 @@ class TestParser < Test::Unit::TestCase
|
|
88
88
|
long 'age[=AGE]'
|
89
89
|
cast Integer
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
args = ['-n', '-a', '21']
|
93
|
-
|
93
|
+
|
94
94
|
assert_raise(Choice::Parser::ArgumentRequired) do
|
95
95
|
choices, rest = Choice::Parser.parse(@options, args)
|
96
96
|
end
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
99
|
def test_text_optional
|
100
100
|
@options['color'] = Choice::Option.new do
|
101
101
|
short '-c'
|
102
102
|
long '--color[=COLOR]'
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
args = ['-c']
|
106
106
|
choices, rest = Choice::Parser.parse(@options, args)
|
107
|
-
|
107
|
+
|
108
108
|
assert choices['color']
|
109
|
-
|
109
|
+
|
110
110
|
color = 'ladyblue'
|
111
111
|
args = ['-c', color]
|
112
112
|
choices, rest = Choice::Parser.parse(@options, args)
|
113
|
-
|
113
|
+
|
114
114
|
assert_equal color, choices['color']
|
115
115
|
end
|
116
116
|
|
@@ -119,38 +119,38 @@ class TestParser < Test::Unit::TestCase
|
|
119
119
|
short '-c'
|
120
120
|
long '--color=[COLOR]'
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
args = ['-c']
|
124
124
|
choices, rest = Choice::Parser.parse(@options, args)
|
125
|
-
|
125
|
+
|
126
126
|
assert choices['color']
|
127
|
-
|
127
|
+
|
128
128
|
color = 'ladyblue'
|
129
129
|
args = ['-c', color]
|
130
130
|
choices, rest = Choice::Parser.parse(@options, args)
|
131
|
-
|
131
|
+
|
132
132
|
assert_equal color, choices['color']
|
133
133
|
end
|
134
|
-
|
134
|
+
|
135
135
|
def test_ignore_separator
|
136
136
|
options = []
|
137
137
|
options << ['keyboard', Choice::Option.new do
|
138
138
|
short '-k'
|
139
139
|
long '--keyboard=BOARD'
|
140
140
|
end]
|
141
|
-
|
141
|
+
|
142
142
|
options << ['mouse', Choice::Option.new do
|
143
143
|
short '-m'
|
144
144
|
long '--mouse=MOUSE'
|
145
145
|
end]
|
146
|
-
|
146
|
+
|
147
147
|
args = ['-m', 'onebutton']
|
148
148
|
choices, rest = Choice::Parser.parse([options.first, '----', options.last], args)
|
149
|
-
|
149
|
+
|
150
150
|
assert choices['mouse']
|
151
151
|
assert_equal 1, choices.size
|
152
152
|
end
|
153
|
-
|
153
|
+
|
154
154
|
def test_long_as_switch
|
155
155
|
@options['chunky'] = Choice::Option.new do
|
156
156
|
short '-b'
|
@@ -159,10 +159,10 @@ class TestParser < Test::Unit::TestCase
|
|
159
159
|
|
160
160
|
args = ['--bacon']
|
161
161
|
choices, rest = Choice::Parser.parse(@options, args)
|
162
|
-
|
162
|
+
|
163
163
|
assert choices['chunky']
|
164
164
|
end
|
165
|
-
|
165
|
+
|
166
166
|
def test_validate_regexp
|
167
167
|
@options['email'] = Choice::Option.new do
|
168
168
|
short '-e'
|
@@ -171,9 +171,9 @@ class TestParser < Test::Unit::TestCase
|
|
171
171
|
validate /^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]{2,4}$/i
|
172
172
|
end
|
173
173
|
|
174
|
-
email_bad = 'this will@neverwork'
|
174
|
+
email_bad = 'this will@neverwork'
|
175
175
|
email_good = 'chris@ozmm.org'
|
176
|
-
|
176
|
+
|
177
177
|
args = ['-e', email_bad]
|
178
178
|
assert_raise(Choice::Parser::ArgumentValidationFails) do
|
179
179
|
choices, rest = Choice::Parser.parse(@options, args)
|
@@ -181,10 +181,10 @@ class TestParser < Test::Unit::TestCase
|
|
181
181
|
|
182
182
|
args = ['-e', email_good]
|
183
183
|
choices, rest = Choice::Parser.parse(@options, args)
|
184
|
-
|
184
|
+
|
185
185
|
assert_equal email_good, choices['email']
|
186
186
|
end
|
187
|
-
|
187
|
+
|
188
188
|
def test_validate_block
|
189
189
|
@options['file'] = Choice::Option.new do
|
190
190
|
short '-f'
|
@@ -197,7 +197,7 @@ class TestParser < Test::Unit::TestCase
|
|
197
197
|
|
198
198
|
file_bad = 'not_a_file.rb'
|
199
199
|
file_good = __FILE__
|
200
|
-
|
200
|
+
|
201
201
|
args = ['-f', file_bad]
|
202
202
|
assert_raise(Choice::Parser::ArgumentValidationFails) do
|
203
203
|
choices, rest = Choice::Parser.parse(@options, args)
|
@@ -205,7 +205,7 @@ class TestParser < Test::Unit::TestCase
|
|
205
205
|
|
206
206
|
args = ['-f', file_good]
|
207
207
|
choices, rest = Choice::Parser.parse(@options, args)
|
208
|
-
|
208
|
+
|
209
209
|
assert_equal file_good, choices['file']
|
210
210
|
end
|
211
211
|
|
@@ -215,7 +215,7 @@ class TestParser < Test::Unit::TestCase
|
|
215
215
|
long '--cd=CD'
|
216
216
|
desc 'A CD you like.'
|
217
217
|
end
|
218
|
-
|
218
|
+
|
219
219
|
args = ['-c', 'BestOfYanni', '--grace']
|
220
220
|
assert_raise(Choice::Parser::UnknownOption) do
|
221
221
|
choices, rest = Choice::Parser.parse(@options, args)
|
@@ -232,7 +232,7 @@ class TestParser < Test::Unit::TestCase
|
|
232
232
|
|
233
233
|
suit_good = 'club'
|
234
234
|
suit_bad = 'joker'
|
235
|
-
|
235
|
+
|
236
236
|
args = ['-s', suit_bad]
|
237
237
|
assert_raise(Choice::Parser::InvalidArgument) do
|
238
238
|
choices, rest = Choice::Parser.parse(@options, args)
|
@@ -240,7 +240,7 @@ class TestParser < Test::Unit::TestCase
|
|
240
240
|
|
241
241
|
args = ['-s', suit_good]
|
242
242
|
choices, rest = Choice::Parser.parse(@options, args)
|
243
|
-
|
243
|
+
|
244
244
|
assert_equal suit_good, choices['suit']
|
245
245
|
end
|
246
246
|
|
@@ -251,7 +251,7 @@ class TestParser < Test::Unit::TestCase
|
|
251
251
|
valid %w[jeans slacks trunks boxers]
|
252
252
|
desc "Your preferred type of pants."
|
253
253
|
end
|
254
|
-
|
254
|
+
|
255
255
|
args = ['-p']
|
256
256
|
assert_raise(Choice::Parser::ArgumentRequiredWithValid) do
|
257
257
|
choices, rest = Choice::Parser.parse(@options, args)
|
@@ -293,15 +293,14 @@ class TestParser < Test::Unit::TestCase
|
|
293
293
|
|
294
294
|
instruments = %w[xylophone guitar piano]
|
295
295
|
|
296
|
-
args = ["--instruments=#{instruments.first}",
|
297
|
-
"--instruments=#{instruments[1]}",
|
296
|
+
args = ["--instruments=#{instruments.first}", "--instruments=#{instruments[1]}",
|
298
297
|
"--instruments=#{instruments.last}"]
|
299
298
|
choices, rest = Choice::Parser.parse(@options, args)
|
300
299
|
assert_equal instruments, choices['instruments']
|
301
300
|
|
302
301
|
args = %w[--instruments]
|
303
302
|
choices, rest = Choice::Parser.parse(@options, args)
|
304
|
-
assert_equal
|
303
|
+
assert_equal true, choices['instruments']
|
305
304
|
end
|
306
305
|
|
307
306
|
def test_long_as_array_with_valid
|
@@ -316,9 +315,9 @@ class TestParser < Test::Unit::TestCase
|
|
316
315
|
|
317
316
|
args = ['-s', suits.first, suits.last]
|
318
317
|
choices, rest = Choice::Parser.parse(@options, args)
|
319
|
-
|
318
|
+
|
320
319
|
assert_equal suits, choices['suits']
|
321
|
-
|
320
|
+
|
322
321
|
args = ['-s', suits.first, 'notasuit']
|
323
322
|
assert_raise(Choice::Parser::InvalidArgument) do
|
324
323
|
choices, rest = Choice::Parser.parse(@options, args)
|
@@ -336,7 +335,7 @@ class TestParser < Test::Unit::TestCase
|
|
336
335
|
|
337
336
|
args = ['--donut', donut]
|
338
337
|
choices, rest = Choice::Parser.parse(@options, args)
|
339
|
-
|
338
|
+
|
340
339
|
assert_equal donut, choices['donut']
|
341
340
|
end
|
342
341
|
|
@@ -371,7 +370,7 @@ class TestParser < Test::Unit::TestCase
|
|
371
370
|
choices, rest = Choice::Parser.parse(@options, args)
|
372
371
|
assert_equal donuts, choices['donuts']
|
373
372
|
end
|
374
|
-
|
373
|
+
|
375
374
|
def test_long_with_rest
|
376
375
|
@options['donut'] = Choice::Option.new do
|
377
376
|
short '-d'
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: choice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Chris Wanstrath
|
@@ -9,7 +15,7 @@ autorequire: choice
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2012-01-25 00:00:00 -07:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -22,7 +28,7 @@ extensions: []
|
|
22
28
|
extra_rdoc_files: []
|
23
29
|
|
24
30
|
files:
|
25
|
-
- README
|
31
|
+
- README.rdoc
|
26
32
|
- CHANGELOG
|
27
33
|
- LICENSE
|
28
34
|
- lib/choice/lazyhash.rb
|
@@ -48,21 +54,27 @@ rdoc_options: []
|
|
48
54
|
require_paths:
|
49
55
|
- lib
|
50
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
51
58
|
requirements:
|
52
59
|
- - ">="
|
53
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
54
64
|
version: "0"
|
55
|
-
version:
|
56
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
57
67
|
requirements:
|
58
68
|
- - ">="
|
59
69
|
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
60
73
|
version: "0"
|
61
|
-
version:
|
62
74
|
requirements: []
|
63
75
|
|
64
76
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.
|
77
|
+
rubygems_version: 1.4.2
|
66
78
|
signing_key:
|
67
79
|
specification_version: 3
|
68
80
|
summary: Choice is a command line option parser.
|