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
8 changes: 7 additions & 1 deletion lib/elixir/lib/option_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,13 @@ defmodule OptionParser do

defp format_error({option, nil}, opts, types) do
if type = get_type(option, opts, types) do
"#{option} : Missing argument of type #{type}"
if String.contains?(option, "_") do
msg = "#{option} : Unknown option"

msg <> ". Did you mean #{String.replace(option, "_", "-")}?"
else
"#{option} : Missing argument of type #{type}"
end
else
msg = "#{option} : Unknown option"

Expand Down
9 changes: 9 additions & 0 deletions lib/elixir/test/elixir/option_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ defmodule OptionParserTest do
end
end

test "parse!/2 raises an exception for an unknown option using strict when it is only off by underscores" do
msg = "1 error found!\n--docs_bar : Unknown option. Did you mean --docs-bar?"

assert_raise OptionParser.ParseError, msg, fn ->
argv = ["--source", "from_docs/", "--docs_bar", "show"]
OptionParser.parse!(argv, strict: [source: :string, docs_bar: :string])
end
end

test "parse!/2 raises an exception when an option is of the wrong type" do
assert_raise OptionParser.ParseError, fn ->
argv = ["--bad", "opt", "foo", "-o", "bad", "bar"]
Expand Down
Loading