Skip to content

Commit e8a6709

Browse files
authored
Provide suggestions when OptionParser flag has underscores (#13925)
1 parent 09d4356 commit e8a6709

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/elixir/lib/option_parser.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,13 @@ defmodule OptionParser do
865865

866866
defp format_error({option, nil}, opts, types) do
867867
if type = get_type(option, opts, types) do
868-
"#{option} : Missing argument of type #{type}"
868+
if String.contains?(option, "_") do
869+
msg = "#{option} : Unknown option"
870+
871+
msg <> ". Did you mean #{String.replace(option, "_", "-")}?"
872+
else
873+
"#{option} : Missing argument of type #{type}"
874+
end
869875
else
870876
msg = "#{option} : Unknown option"
871877

lib/elixir/test/elixir/option_parser_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ defmodule OptionParserTest do
109109
end
110110
end
111111

112+
test "parse!/2 raises an exception for an unknown option using strict when it is only off by underscores" do
113+
msg = "1 error found!\n--docs_bar : Unknown option. Did you mean --docs-bar?"
114+
115+
assert_raise OptionParser.ParseError, msg, fn ->
116+
argv = ["--source", "from_docs/", "--docs_bar", "show"]
117+
OptionParser.parse!(argv, strict: [source: :string, docs_bar: :string])
118+
end
119+
end
120+
112121
test "parse!/2 raises an exception when an option is of the wrong type" do
113122
assert_raise OptionParser.ParseError, fn ->
114123
argv = ["--bad", "opt", "foo", "-o", "bad", "bar"]

0 commit comments

Comments
 (0)