Skip to content

Commit

Permalink
Merge pull request #4036 from joeyates/feature/optionparser-raises-wi…
Browse files Browse the repository at this point in the history
…th-both-strict-and-switches

Raise error for incompatible OptionParser options
  • Loading branch information
josevalim committed Dec 3, 2015
2 parents ae12b80 + 9b9b324 commit d6f0e64
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/elixir/lib/option_parser.ex
Expand Up @@ -51,7 +51,7 @@ defmodule OptionParser do
in the list is returned in the invalid options list.
Note that you should only supply the `:switches` or `:strict` option. If you
supply both, the `:strict` option will be ignored.
supply both, an error will be raised.
For each switch, the following types are supported:
Expand Down Expand Up @@ -330,6 +330,8 @@ defmodule OptionParser do
aliases = opts[:aliases] || []

{switches, strict} = cond do
opts[:switches] && opts[:strict] ->
raise ArgumentError, ":switches and :strict cannot be supplied together"
s = opts[:switches] ->
{s, false}
s = opts[:strict] ->
Expand Down
6 changes: 6 additions & 0 deletions lib/elixir/test/elixir/option_parser_test.exs
Expand Up @@ -198,6 +198,12 @@ defmodule OptionParserTest do
== {[source: "from_docs/"], [], [{"--doc", nil}]}
end

test ":switches with :strict raises" do
assert_raise ArgumentError, ":switches and :strict cannot be supplied together", fn ->
OptionParser.parse([], strict: [], switches: [])
end
end

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

0 comments on commit d6f0e64

Please sign in to comment.