Permalink
Browse files

Merge pull request #4036 from joeyates/feature/optionparser-raises-wi…

…th-both-strict-and-switches

Raise error for incompatible OptionParser options
  • Loading branch information...
josevalim committed Dec 3, 2015
2 parents ae12b80 + 9b9b324 commit d6f0e6416407c8d2f3ae81198570a71699dbaa0d
Showing with 9 additions and 1 deletion.
  1. +3 −1 lib/elixir/lib/option_parser.ex
  2. +6 −0 lib/elixir/test/elixir/option_parser_test.exs
@@ -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:
@@ -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] ->
@@ -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", "-"}]}

0 comments on commit d6f0e64

Please sign in to comment.