Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/elixir/lib/option_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ defmodule OptionParser do
{:error, argv}
end

defp next(["-"|_]=argv, _aliases, _switches, _strict) do
{:error, argv}
end

defp next(["- " <> _|_]=argv, _aliases, _switches, _strict) do
{:error, argv}
end

defp next(["-" <> option|rest], aliases, switches, strict) do
{option, value} = split_option(option)
opt = tag_option(option, value, switches, aliases)
Expand Down Expand Up @@ -353,9 +361,11 @@ defmodule OptionParser do
{value, kinds, t}
end

defp value_in_tail?(["-" <> _|_]), do: false
defp value_in_tail?([]), do: false
defp value_in_tail?(_), do: true
defp value_in_tail?(["-"|_]), do: true
defp value_in_tail?(["- " <> _|_]), do: true
defp value_in_tail?(["-" <> _|_]), do: false
defp value_in_tail?([]), do: false
defp value_in_tail?(_), do: true

defp split_option(option) do
case :binary.split(option, "=") do
Expand Down
18 changes: 18 additions & 0 deletions lib/elixir/test/elixir/option_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ defmodule OptionParserTest do
== {[source: "from_docs/", docs: "show"], [], []}
end

test "collects multiple invalid options" do
args = ["--bad", "opt", "foo", "-o", "bad", "bar"]
assert OptionParser.parse(args, switches: [bad: :integer])
== {[], ["foo", "bar"], [bad: "opt", o: "bad"]}
end

test "parses more than one key/value options using strict" do
assert OptionParser.parse(["--source", "from_docs/", "--docs", "show"],
strict: [source: :string, docs: :string])
Expand All @@ -180,6 +186,18 @@ defmodule OptionParserTest do
assert OptionParser.parse(["--source", "from_docs/", "--doc", "show"],
strict: [source: :string, docs: :string])
== {[source: "from_docs/"], ["show"], [doc: nil]}

assert OptionParser.parse(["--source", "from_docs/", "--doc=show"],
strict: [source: :string, docs: :string])
== {[source: "from_docs/"], [], [doc: "show"]}
end

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

assert OptionParser.parse(["--foo", "-", "-b", "-"], strict: [foo: :boolean, boo: :string], aliases: [b: :boo])
== {[foo: true, boo: "-"], ["-"], []}
end

test "next strict: good options" do
Expand Down