choice 0.1.3 → 0.1.4
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 +6 -4
- data/lib/choice.rb +15 -4
- data/lib/choice/parser.rb +36 -30
- data/lib/choice/version.rb +1 -1
- data/test/test_choice.rb +9 -6
- data/test/test_parser.rb +98 -82
- metadata +7 -6
data/README
CHANGED
@@ -68,13 +68,15 @@ programs utilizing the library have been placed. Here's a snippet:
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
puts 'port: ' + Choice
|
71
|
+
puts 'port: ' + Choice[:port]
|
72
72
|
|
73
73
|
Notice the last line. For free, you will be given a <tt>Choice.choices</tt>
|
74
|
-
hash which contain, at runtime, the options found and their values.
|
74
|
+
hash which contain, at runtime, the options found and their values.
|
75
|
+
|
76
|
+
<tt>Choice[:key]</tt> is a shortcut for <tt>Choice.choices[:key]</tt>.
|
75
77
|
|
76
78
|
Because we gave option <tt>:port</tt> a default of 21,
|
77
|
-
<tt>Choice
|
79
|
+
<tt>Choice[:port]</tt> should be 21 if we run ftpd.rb with no options.
|
78
80
|
Let's see.
|
79
81
|
|
80
82
|
$ ruby ftpd.rb
|
@@ -89,7 +91,7 @@ Alright. And, of course, there is the hard way of doing things.
|
|
89
91
|
|
90
92
|
$ ruby ftpd.rb --port=2100
|
91
93
|
port: 2100
|
92
|
-
|
94
|
+
|
93
95
|
That <tt>:version</tt> option looks pretty interesting, huh? I wonder what it
|
94
96
|
does...
|
95
97
|
|
data/lib/choice.rb
CHANGED
@@ -36,26 +36,36 @@ module Choice
|
|
36
36
|
@@options << [name.to_s, option]
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
# Return an array representing the rest of the command line arguments
|
41
|
+
def rest
|
42
|
+
@@rest
|
43
|
+
end
|
39
44
|
|
40
45
|
# Returns a hash representing options passed in via the command line.
|
41
46
|
def choices
|
42
47
|
@@choices
|
43
48
|
end
|
44
49
|
|
50
|
+
# Shortcut access to Choice.choices
|
51
|
+
def [](choice)
|
52
|
+
choices[choice]
|
53
|
+
end
|
54
|
+
|
45
55
|
# Defines an option.
|
46
56
|
def option(opt, options = {}, &block)
|
47
57
|
# Notice: options is maintained as an array of arrays, the first element
|
48
58
|
# the option name and the second the option object.
|
49
59
|
@@options << [opt.to_s, Option.new(options, &block)]
|
50
60
|
end
|
51
|
-
|
61
|
+
|
52
62
|
# Separators are text displayed by --help within the options block.
|
53
63
|
def separator(str)
|
54
|
-
# We store separators as simple strings in the options array to maintain
|
64
|
+
# We store separators as simple strings in the options array to maintain
|
55
65
|
# order. They are ignored by the parser.
|
56
66
|
@@options << str
|
57
67
|
end
|
58
|
-
|
68
|
+
|
59
69
|
# Define the banner, header, footer methods. All are just getters/setters
|
60
70
|
# of class variables.
|
61
71
|
%w[banner header footer].each do |method|
|
@@ -80,7 +90,8 @@ module Choice
|
|
80
90
|
begin
|
81
91
|
# Delegate parsing to our parser class, passing it our defined
|
82
92
|
# options and the passed arguments.
|
83
|
-
@@choices =
|
93
|
+
@@choices, @@rest = Parser.parse(@@options, @@args)
|
94
|
+
@@choices = LazyHash.new(@@choices)
|
84
95
|
rescue Choice::Parser::ParseError
|
85
96
|
# If we get an expected exception, show the help file.
|
86
97
|
help
|
data/lib/choice/parser.rb
CHANGED
@@ -18,6 +18,9 @@ module Choice
|
|
18
18
|
# Return empty hash if the parsing adventure would be fruitless.
|
19
19
|
return {} if options.nil? || !options || args.nil? || !args.is_a?(Array)
|
20
20
|
|
21
|
+
# Operate on a copy of the inputs
|
22
|
+
args = args.dup
|
23
|
+
|
21
24
|
# If we are passed an array, make the best of it by converting it
|
22
25
|
# to a hash.
|
23
26
|
options = options.inject({}) do |hash, value|
|
@@ -83,53 +86,53 @@ module Choice
|
|
83
86
|
# is definitely required.
|
84
87
|
required[name] = true if obj['valid']
|
85
88
|
end
|
86
|
-
|
89
|
+
|
90
|
+
rest = []
|
91
|
+
|
87
92
|
# Go through the arguments and try to figure out whom they belong to
|
88
93
|
# at this point.
|
89
|
-
|
94
|
+
while arg = args.shift
|
90
95
|
if hashes['shorts'].value?(arg)
|
91
96
|
# Set the value to the next element in the args array since
|
92
97
|
# this is a short.
|
93
|
-
value = args[i+1]
|
94
98
|
|
95
|
-
# If the next
|
96
|
-
|
97
|
-
|
99
|
+
# If the next argument isn't a value, set this value to true
|
100
|
+
if args.empty? || args.first.match(/^-/)
|
101
|
+
value = true
|
102
|
+
else
|
103
|
+
value = args.shift
|
104
|
+
end
|
98
105
|
|
99
106
|
# Add this value to the choices hash with the key of the option's
|
100
107
|
# name. If we expect an array, tack this argument on.
|
101
108
|
name = hashes['shorts'].index(arg)
|
102
109
|
if arrayed[name]
|
103
110
|
choices[name] ||= []
|
104
|
-
choices[name]
|
111
|
+
choices[name] << value unless value.nil?
|
112
|
+
choices[name] += arrayize_arguments(args)
|
105
113
|
else
|
106
114
|
choices[name] = value
|
107
115
|
end
|
108
116
|
|
109
|
-
elsif /^(--[^=]+)=?/
|
117
|
+
elsif (m = arg.match(/^(--[^=]+)=?/)) && longs.value?(m[1])
|
110
118
|
# The joke here is we always accept both --long=VALUE and --long VALUE.
|
111
119
|
|
112
120
|
# Grab values from --long=VALUE format
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
potential_args = args[i+1..-1]
|
118
|
-
else
|
121
|
+
name, value = arg.split('=', 2)
|
122
|
+
name = longs.index(name)
|
123
|
+
|
124
|
+
if value.nil? && args.first !~ /^-/
|
119
125
|
# Grab value otherwise if not in --long=VALUE format. Assume --long VALUE.
|
120
|
-
name = longs.index(arg)
|
121
126
|
# Value is nil if we don't have a = and the next argument is no good
|
122
|
-
value = args
|
123
|
-
# For the arrayed options.
|
124
|
-
potential_args = args[i+2..-1]
|
127
|
+
value = args.shift
|
125
128
|
end
|
126
129
|
|
127
130
|
# If we expect an array, tack this argument on.
|
128
|
-
if arrayed[name]
|
131
|
+
if arrayed[name]
|
129
132
|
# If this is arrayed and the value isn't nil, set it.
|
130
133
|
choices[name] ||= []
|
131
|
-
choices[name] << value
|
132
|
-
choices[name] += arrayize_arguments(
|
134
|
+
choices[name] << value unless value.nil?
|
135
|
+
choices[name] += arrayize_arguments(args)
|
133
136
|
else
|
134
137
|
# If we set the value to nil, that means nothing was set and we
|
135
138
|
# need to set the value to true. We'll find out later if that's
|
@@ -139,7 +142,11 @@ module Choice
|
|
139
142
|
|
140
143
|
else
|
141
144
|
# If we're here, we have no idea what the passed argument is. Die.
|
142
|
-
|
145
|
+
if arg =~ /^-/
|
146
|
+
raise UnknownOption
|
147
|
+
else
|
148
|
+
rest << arg
|
149
|
+
end
|
143
150
|
end
|
144
151
|
end
|
145
152
|
|
@@ -159,10 +166,10 @@ module Choice
|
|
159
166
|
|
160
167
|
# Make sure the argument is valid
|
161
168
|
raise InvalidArgument unless value.to_a.all? { |v| hashes['valids'][name].include?(v) } if hashes['valids'][name]
|
162
|
-
|
169
|
+
|
163
170
|
# Cast the argument using the method defined in the constant hash.
|
164
171
|
value = value.send(CAST_METHODS[hashes['casts'][name]]) if hashes['casts'].include?(name)
|
165
|
-
|
172
|
+
|
166
173
|
# Run the value through a filter and re-set it with the return.
|
167
174
|
value = hashes['filters'][name].call(value) if hashes['filters'].include?(name)
|
168
175
|
|
@@ -186,19 +193,18 @@ module Choice
|
|
186
193
|
choices[name] = value unless choices[name]
|
187
194
|
end
|
188
195
|
|
189
|
-
# Return the choices hash
|
190
|
-
choices
|
196
|
+
# Return the choices hash and the rest of the args
|
197
|
+
[ choices, rest ]
|
191
198
|
end
|
192
199
|
|
193
200
|
private
|
194
201
|
# Turns trailing command line arguments into an array for an arrayed value
|
195
|
-
def arrayize_arguments(
|
202
|
+
def arrayize_arguments(args)
|
196
203
|
# Go through trailing arguments and suck them in if they don't seem
|
197
204
|
# to have an owner.
|
198
205
|
array = []
|
199
|
-
|
200
|
-
|
201
|
-
array << arg
|
206
|
+
until args.empty? || args.first.match(/^-/)
|
207
|
+
array << args.shift
|
202
208
|
end
|
203
209
|
array
|
204
210
|
end
|
data/lib/choice/version.rb
CHANGED
data/test/test_choice.rb
CHANGED
@@ -32,27 +32,30 @@ class TestChoice < Test::Unit::TestCase
|
|
32
32
|
footer ""
|
33
33
|
footer "--help This message"
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
band = 'LedZeppelin'
|
37
37
|
animal = 'Reindeer'
|
38
|
-
|
38
|
+
|
39
39
|
args = ['-b', band, "--animal=#{animal}"]
|
40
40
|
Choice.args = args
|
41
|
-
|
41
|
+
|
42
42
|
assert_equal band, Choice.choices['band']
|
43
43
|
assert_equal animal, Choice.choices[:animal]
|
44
44
|
assert_equal ["Tell me about yourself?", ""], Choice.header
|
45
45
|
assert_equal ["", "--help This message"], Choice.footer
|
46
|
+
|
47
|
+
assert_equal Choice.choices['band'], Choice['band']
|
48
|
+
assert_equal Choice.choices[:animal], Choice[:animal]
|
46
49
|
end
|
47
|
-
|
50
|
+
|
48
51
|
def test_failed_parse
|
49
52
|
assert Hash.new, Choice.parse
|
50
53
|
end
|
51
|
-
|
54
|
+
|
52
55
|
HELP_STRING = ''
|
53
56
|
def test_help
|
54
57
|
Choice.output_to(HELP_STRING)
|
55
|
-
|
58
|
+
|
56
59
|
Choice.options do
|
57
60
|
banner "Usage: choice [-mu]"
|
58
61
|
header ""
|
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
|
-
|
29
|
-
choices = Choice::Parser.parse(@options, args)
|
30
|
-
|
28
|
+
|
29
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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
|
-
|
48
|
-
choices = Choice::Parser.parse(@options, args)
|
49
|
-
|
47
|
+
|
48
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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
|
-
choices = Choice::Parser.parse(@options, args)
|
64
|
-
|
63
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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
|
-
choices = Choice::Parser.parse(@options, args)
|
77
|
-
|
76
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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
|
-
choices = Choice::Parser.parse(@options, args)
|
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
|
-
choices = Choice::Parser.parse(@options, args)
|
107
|
-
|
106
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
107
|
+
|
108
108
|
assert choices['color']
|
109
|
-
|
109
|
+
|
110
110
|
color = 'ladyblue'
|
111
111
|
args = ['-c', color]
|
112
|
-
choices = Choice::Parser.parse(@options, args)
|
113
|
-
|
112
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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
|
-
choices = Choice::Parser.parse(@options, args)
|
125
|
-
|
124
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
125
|
+
|
126
126
|
assert choices['color']
|
127
|
-
|
127
|
+
|
128
128
|
color = 'ladyblue'
|
129
129
|
args = ['-c', color]
|
130
|
-
choices = Choice::Parser.parse(@options, args)
|
131
|
-
|
130
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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
|
-
choices = Choice::Parser.parse([options.first, '----', options.last], args)
|
149
|
-
|
148
|
+
choices, rest = Choice::Parser.parse([options.first, '----', options.last], args)
|
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'
|
@@ -158,11 +158,11 @@ class TestParser < Test::Unit::TestCase
|
|
158
158
|
end
|
159
159
|
|
160
160
|
args = ['--bacon']
|
161
|
-
choices = Choice::Parser.parse(@options, args)
|
162
|
-
|
161
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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,20 +171,20 @@ 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
|
-
choices = Choice::Parser.parse(@options, args)
|
179
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
180
180
|
end
|
181
181
|
|
182
182
|
args = ['-e', email_good]
|
183
|
-
choices = Choice::Parser.parse(@options, args)
|
184
|
-
|
183
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
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,15 +197,15 @@ 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
|
-
choices = Choice::Parser.parse(@options, args)
|
203
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
204
204
|
end
|
205
205
|
|
206
206
|
args = ['-f', file_good]
|
207
|
-
choices = Choice::Parser.parse(@options, args)
|
208
|
-
|
207
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
208
|
+
|
209
209
|
assert_equal file_good, choices['file']
|
210
210
|
end
|
211
211
|
|
@@ -215,10 +215,10 @@ 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
|
-
choices = Choice::Parser.parse(@options, args)
|
221
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
222
222
|
end
|
223
223
|
end
|
224
224
|
|
@@ -232,15 +232,15 @@ 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
|
-
choices = Choice::Parser.parse(@options, args)
|
238
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
239
239
|
end
|
240
240
|
|
241
241
|
args = ['-s', suit_good]
|
242
|
-
choices = Choice::Parser.parse(@options, args)
|
243
|
-
|
242
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
243
|
+
|
244
244
|
assert_equal suit_good, choices['suit']
|
245
245
|
end
|
246
246
|
|
@@ -251,10 +251,10 @@ 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
|
-
choices = Choice::Parser.parse(@options, args)
|
257
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
258
258
|
end
|
259
259
|
end
|
260
260
|
|
@@ -268,19 +268,19 @@ class TestParser < Test::Unit::TestCase
|
|
268
268
|
mediums = %w[canvas stone steel]
|
269
269
|
|
270
270
|
args = ['-m', mediums.first, '-m', mediums[1], '-m', mediums.last]
|
271
|
-
choices = Choice::Parser.parse(@options, args)
|
271
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
272
272
|
assert_equal mediums, choices['medium']
|
273
273
|
|
274
274
|
args = ['-m', mediums.first, mediums[1], mediums.last]
|
275
|
-
choices = Choice::Parser.parse(@options, args)
|
275
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
276
276
|
assert_equal mediums, choices['medium']
|
277
277
|
|
278
278
|
args = ["--medium=#{mediums.first}", "--medium=#{mediums[1]}", "--medium=#{mediums.last}"]
|
279
|
-
choices = Choice::Parser.parse(@options, args)
|
279
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
280
280
|
assert_equal mediums, choices['medium']
|
281
281
|
|
282
282
|
args = ["--medium=#{mediums.first}", mediums[1], mediums.last]
|
283
|
-
choices = Choice::Parser.parse(@options, args)
|
283
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
284
284
|
assert_equal mediums, choices['medium']
|
285
285
|
end
|
286
286
|
|
@@ -293,14 +293,15 @@ class TestParser < Test::Unit::TestCase
|
|
293
293
|
|
294
294
|
instruments = %w[xylophone guitar piano]
|
295
295
|
|
296
|
-
args = ["--instruments=#{instruments.first}",
|
296
|
+
args = ["--instruments=#{instruments.first}",
|
297
|
+
"--instruments=#{instruments[1]}",
|
297
298
|
"--instruments=#{instruments.last}"]
|
298
|
-
choices = Choice::Parser.parse(@options, args)
|
299
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
299
300
|
assert_equal instruments, choices['instruments']
|
300
301
|
|
301
302
|
args = %w[--instruments]
|
302
|
-
choices = Choice::Parser.parse(@options, args)
|
303
|
-
assert_equal
|
303
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
304
|
+
assert_equal [], choices['instruments']
|
304
305
|
end
|
305
306
|
|
306
307
|
def test_long_as_array_with_valid
|
@@ -314,13 +315,13 @@ class TestParser < Test::Unit::TestCase
|
|
314
315
|
suits = %w[spade heart]
|
315
316
|
|
316
317
|
args = ['-s', suits.first, suits.last]
|
317
|
-
choices = Choice::Parser.parse(@options, args)
|
318
|
-
|
318
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
319
|
+
|
319
320
|
assert_equal suits, choices['suits']
|
320
|
-
|
321
|
+
|
321
322
|
args = ['-s', suits.first, 'notasuit']
|
322
323
|
assert_raise(Choice::Parser::InvalidArgument) do
|
323
|
-
choices = Choice::Parser.parse(@options, args)
|
324
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
324
325
|
end
|
325
326
|
end
|
326
327
|
|
@@ -334,8 +335,8 @@ class TestParser < Test::Unit::TestCase
|
|
334
335
|
donut = 'long-john'
|
335
336
|
|
336
337
|
args = ['--donut', donut]
|
337
|
-
choices = Choice::Parser.parse(@options, args)
|
338
|
-
|
338
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
339
|
+
|
339
340
|
assert_equal donut, choices['donut']
|
340
341
|
end
|
341
342
|
|
@@ -349,11 +350,11 @@ class TestParser < Test::Unit::TestCase
|
|
349
350
|
donut = 'chocolate'
|
350
351
|
|
351
352
|
args = ['--donut', donut]
|
352
|
-
choices = Choice::Parser.parse(@options, args)
|
353
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
353
354
|
assert_equal donut, choices['donut']
|
354
355
|
|
355
356
|
args = ['--donut']
|
356
|
-
choices = Choice::Parser.parse(@options, args)
|
357
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
357
358
|
assert_equal true, choices['donut']
|
358
359
|
end
|
359
360
|
|
@@ -367,7 +368,22 @@ class TestParser < Test::Unit::TestCase
|
|
367
368
|
donuts = %w[glazed cream-filled]
|
368
369
|
|
369
370
|
args = ['--donuts', donuts.first, donuts.last]
|
370
|
-
choices = Choice::Parser.parse(@options, args)
|
371
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
371
372
|
assert_equal donuts, choices['donuts']
|
372
373
|
end
|
374
|
+
|
375
|
+
def test_long_with_rest
|
376
|
+
@options['donut'] = Choice::Option.new do
|
377
|
+
short '-d'
|
378
|
+
long '--donut [DONUT]'
|
379
|
+
desc "Your favorite donut style."
|
380
|
+
end
|
381
|
+
|
382
|
+
donut = 'chocolate'
|
383
|
+
|
384
|
+
args = ['eat', '--donut', donut]
|
385
|
+
choices, rest = Choice::Parser.parse(@options, args)
|
386
|
+
assert_equal donut, choices['donut']
|
387
|
+
assert_equal ['eat'], rest
|
388
|
+
end
|
373
389
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: choice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Wanstrath
|
@@ -9,7 +9,7 @@ autorequire: choice
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-10-03 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,7 +25,6 @@ files:
|
|
25
25
|
- README
|
26
26
|
- CHANGELOG
|
27
27
|
- LICENSE
|
28
|
-
- lib/choice
|
29
28
|
- lib/choice/lazyhash.rb
|
30
29
|
- lib/choice/option.rb
|
31
30
|
- lib/choice/parser.rb
|
@@ -39,8 +38,10 @@ files:
|
|
39
38
|
- test/test_writer.rb
|
40
39
|
- examples/ftpd.rb
|
41
40
|
- examples/gamble.rb
|
42
|
-
has_rdoc:
|
41
|
+
has_rdoc: true
|
43
42
|
homepage: http://choice.rubyforge.org/
|
43
|
+
licenses: []
|
44
|
+
|
44
45
|
post_install_message:
|
45
46
|
rdoc_options: []
|
46
47
|
|
@@ -61,9 +62,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
62
|
requirements: []
|
62
63
|
|
63
64
|
rubyforge_project:
|
64
|
-
rubygems_version: 1.3.
|
65
|
+
rubygems_version: 1.3.5
|
65
66
|
signing_key:
|
66
|
-
specification_version:
|
67
|
+
specification_version: 3
|
67
68
|
summary: Choice is a command line option parser.
|
68
69
|
test_files: []
|
69
70
|
|