Skip to content

Commit 5990026

Browse files
author
José Valim
committed
Merge pull request #2312 from alco/option-parser-fixes
Return invalid options as strings
2 parents 6bfcf2f + 4cc28c2 commit 5990026

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

lib/elixir/lib/option_parser.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ defmodule OptionParser do
7878
{[limit: 3], [], []}
7979
8080
iex> OptionParser.parse(["--limit", "xyz"], strict: [limit: :integer])
81-
{[], [], [limit: "xyz"]}
81+
{[], [], [{"--limit", "xyz"}]}
8282
8383
iex> OptionParser.parse(["--unknown", "xyz"], strict: [])
84-
{[], ["xyz"], [unknown: nil]}
84+
{[], ["xyz"], [{"--unknown", nil}]}
8585
8686
iex> OptionParser.parse(["--limit", "3", "--unknown", "xyz"],
8787
...> switches: [limit: :integer])
@@ -216,17 +216,17 @@ defmodule OptionParser do
216216

217217
defp next(["-" <> option|rest], aliases, switches, strict) do
218218
{option, value} = split_option(option)
219+
opt_name_bin = "-" <> option
219220
tagged = tag_option(option, value, switches, aliases)
220221

221222
if strict and not option_defined?(tagged, switches) do
222-
{_, opt_name} = tagged
223-
{:undefined, opt_name, value, rest}
223+
{:undefined, opt_name_bin, value, rest}
224224
else
225225
{opt_name, kinds, value} = normalize_option(tagged, value, switches)
226226
{value, kinds, rest} = normalize_value(value, kinds, rest, strict)
227227
case validate_option(opt_name, value, kinds) do
228228
{:ok, new_value} -> {:ok, opt_name, new_value, rest}
229-
:invalid -> {:invalid, opt_name, value, rest}
229+
:invalid -> {:invalid, opt_name_bin, value, rest}
230230
end
231231
end
232232
end

lib/elixir/test/elixir/option_parser_test.exs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ defmodule OptionParserTest do
4646
assert OptionParser.parse(["--no-bool"])
4747
== {[no_bool: true], [], []}
4848
assert OptionParser.parse(["--no-bool"], strict: [])
49-
== {[], [], [no_bool: nil]}
49+
== {[], [], [{"--no-bool", nil}]}
5050
assert OptionParser.parse(["--no-bool=...", "other"])
51-
== {[], ["other"], [no_bool: "..."]}
51+
== {[], ["other"], [{"--no-bool", "..."}]}
5252
end
5353

5454
test "does not parse -- as an alias" do
@@ -58,7 +58,7 @@ defmodule OptionParserTest do
5858

5959
test "does not parse - as a switch" do
6060
assert OptionParser.parse(["-source=from_docs/"], aliases: [s: :source])
61-
== {[], [], [source: "from_docs/"]}
61+
== {[], [], [{"-source", "from_docs/"}]}
6262
end
6363

6464
test "parses configured booleans" do
@@ -67,18 +67,18 @@ defmodule OptionParserTest do
6767
assert OptionParser.parse(["--docs=true"], switches: [docs: :boolean])
6868
== {[docs: true], [], []}
6969
assert OptionParser.parse(["--docs=other"], switches: [docs: :boolean])
70-
== {[], [], [docs: "other"]}
70+
== {[], [], [{"--docs", "other"}]}
7171
assert OptionParser.parse(["--docs="], switches: [docs: :boolean])
72-
== {[], [], [docs: ""]}
72+
== {[], [], [{"--docs", ""}]}
7373

7474
assert OptionParser.parse(["--docs", "foo"], switches: [docs: :boolean])
7575
== {[docs: true], ["foo"], []}
7676
assert OptionParser.parse(["--no-docs", "foo"], switches: [docs: :boolean])
7777
== {[docs: false], ["foo"], []}
7878
assert OptionParser.parse(["--no-docs=foo", "bar"], switches: [docs: :boolean])
79-
== {[], ["bar"], [no_docs: "foo"]}
79+
== {[], ["bar"], [{"--no-docs", "foo"}]}
8080
assert OptionParser.parse(["--no-docs=", "bar"], switches: [docs: :boolean])
81-
== {[], ["bar"], [no_docs: ""]}
81+
== {[], ["bar"], [{"--no-docs", ""}]}
8282
end
8383

8484
test "does not set unparsed booleans" do
@@ -92,7 +92,7 @@ defmodule OptionParserTest do
9292
== {[require: "foo", require: "bar"], ["baz"], []}
9393

9494
assert OptionParser.parse(["--require"], switches: [require: :keep])
95-
== {[], [], [require: nil]}
95+
== {[], [], [{"--require", nil}]}
9696
end
9797

9898
test "parses configured strings" do
@@ -101,9 +101,9 @@ defmodule OptionParserTest do
101101
assert OptionParser.parse(["--value=1", "foo"], switches: [value: :string])
102102
== {[value: "1"], ["foo"], []}
103103
assert OptionParser.parse(["--value"], switches: [value: :string])
104-
== {[], [], [value: nil]}
104+
== {[], [], [{"--value", nil}]}
105105
assert OptionParser.parse(["--no-value"], switches: [value: :string])
106-
== {[], [], [no_value: nil]}
106+
== {[], [], [{"--no-value", nil}]}
107107
end
108108

109109
test "parses configured integers" do
@@ -112,7 +112,7 @@ defmodule OptionParserTest do
112112
assert OptionParser.parse(["--value=1", "foo"], switches: [value: :integer])
113113
== {[value: 1], ["foo"], []}
114114
assert OptionParser.parse(["--value", "WAT", "foo"], switches: [value: :integer])
115-
== {[], ["foo"], [value: "WAT"]}
115+
== {[], ["foo"], [{"--value", "WAT"}]}
116116
end
117117

118118
test "parses configured integers with keep" do
@@ -131,7 +131,7 @@ defmodule OptionParserTest do
131131
assert OptionParser.parse(["--value=1.0", "foo"], switches: [value: :float])
132132
== {[value: 1.0], ["foo"], []}
133133
assert OptionParser.parse(["--value", "WAT", "foo"], switches: [value: :float])
134-
== {[], ["foo"], [value: "WAT"]}
134+
== {[], ["foo"], [{"--value", "WAT"}]}
135135
end
136136

137137
test "parses no switches as flags" do
@@ -184,7 +184,7 @@ defmodule OptionParserTest do
184184
test "collects multiple invalid options" do
185185
args = ["--bad", "opt", "foo", "-o", "bad", "bar"]
186186
assert OptionParser.parse(args, switches: [bad: :integer])
187-
== {[], ["foo", "bar"], [bad: "opt", o: "bad"]}
187+
== {[], ["foo", "bar"], [{"--bad", "opt"}, {"-o", "bad"}]}
188188
end
189189

190190
test "parses more than one key/value options using strict" do
@@ -194,16 +194,16 @@ defmodule OptionParserTest do
194194

195195
assert OptionParser.parse(["--source", "from_docs/", "--doc", "show"],
196196
strict: [source: :string, docs: :string])
197-
== {[source: "from_docs/"], ["show"], [doc: nil]}
197+
== {[source: "from_docs/"], ["show"], [{"--doc", nil}]}
198198

199199
assert OptionParser.parse(["--source", "from_docs/", "--doc=show"],
200200
strict: [source: :string, docs: :string])
201-
== {[source: "from_docs/"], [], [doc: nil]}
201+
== {[source: "from_docs/"], [], [{"--doc", nil}]}
202202
end
203203

204204
test "parses - as argument" do
205205
assert OptionParser.parse(["-a", "-", "-", "-b", "-"], aliases: [b: :boo])
206-
== {[boo: "-"], ["-"], [a: "-"]}
206+
== {[boo: "-"], ["-"], [{"-a", "-"}]}
207207

208208
assert OptionParser.parse(["--foo", "-", "-b", "-"], strict: [foo: :boolean, boo: :string], aliases: [b: :boo])
209209
== {[foo: true, boo: "-"], ["-"], []}
@@ -228,34 +228,34 @@ defmodule OptionParserTest do
228228
test "next strict: unknown options" do
229229
config = [strict: [bool: :boolean]]
230230
assert OptionParser.next(["--str", "13", "..."], config)
231-
== {:undefined, :str, nil, ["13", "..."]}
231+
== {:undefined, "--str", nil, ["13", "..."]}
232232
assert OptionParser.next(["--int=hello", "..."], config)
233-
== {:undefined, :int, "hello", ["..."]}
234-
assert OptionParser.next(["--no-bool=other", "..."], config)
235-
== {:undefined, :no_bool, "other", ["..."]}
233+
== {:undefined, "--int", "hello", ["..."]}
234+
assert OptionParser.next(["-no-bool=other", "..."], config)
235+
== {:undefined, "-no-bool", "other", ["..."]}
236236
end
237237

238238
test "next strict: bad type" do
239239
config = [strict: [str: :string, int: :integer, bool: :boolean]]
240240
assert OptionParser.next(["--str", "13", "..."], config)
241241
== {:ok, :str, "13", ["..."]}
242242
assert OptionParser.next(["--int=hello", "..."], config)
243-
== {:invalid, :int, "hello", ["..."]}
243+
== {:invalid, "--int", "hello", ["..."]}
244244
assert OptionParser.next(["--int", "hello", "..."], config)
245-
== {:invalid, :int, "hello", ["..."]}
245+
== {:invalid, "--int", "hello", ["..."]}
246246
assert OptionParser.next(["--bool=other", "..."], config)
247-
== {:invalid, :bool, "other", ["..."]}
247+
== {:invalid, "--bool", "other", ["..."]}
248248
end
249249

250250
test "next strict: missing value" do
251251
config = [strict: [str: :string, int: :integer, bool: :boolean]]
252252
assert OptionParser.next(["--str"], config)
253-
== {:invalid, :str, nil, []}
253+
== {:invalid, "--str", nil, []}
254254
assert OptionParser.next(["--int"], config)
255-
== {:invalid, :int, nil, []}
255+
== {:invalid, "--int", nil, []}
256256
assert OptionParser.next(["--bool=", "..."], config)
257-
== {:invalid, :bool, "", ["..."]}
257+
== {:invalid, "--bool", "", ["..."]}
258258
assert OptionParser.next(["--no-bool=", "..."], config)
259-
== {:undefined, :no_bool, "", ["..."]}
259+
== {:undefined, "--no-bool", "", ["..."]}
260260
end
261261
end

0 commit comments

Comments
 (0)