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
12 changes: 10 additions & 2 deletions lib/elixir/lib/option_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ defmodule OptionParser do
msg = "#{option} : Unknown option"
msg <> ". Did you mean #{String.replace(option, "_", "-")}?"
else
"#{option} : Missing argument of type #{type}"
"#{option} : Missing argument of type #{format_type(type)}"
end
else
msg = "#{option} : Unknown option"
Expand All @@ -908,7 +908,7 @@ defmodule OptionParser do
{:error, {reason, position}} <- Regex.compile(value, "u") do
"#{option} : Invalid regular expression #{inspect(value)}: #{reason} at position #{position}"
else
_ -> "#{option} : Expected type #{type}, got #{inspect(value)}"
_ -> "#{option} : Expected type #{format_type(type)}, got #{inspect(value)}"
end
end

Expand All @@ -923,6 +923,14 @@ defmodule OptionParser do
end
end

defp format_type(type) when is_list(type) do
type |> List.delete(:keep) |> List.first(:string) |> format_type()
end

defp format_type(:keep), do: format_type(:string)

defp format_type(type) when is_atom(type), do: Atom.to_string(type)

defp did_you_mean(option, types) do
key = option |> String.trim_leading("-") |> String.replace("-", "_")
Enum.reduce(types, {nil, 0}, &max_similar(&1, key, &2))
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 @@ -162,6 +162,14 @@ defmodule OptionParserTest do
end
end

test "parse!/2 raises for a missing argument with :keep" do
assert_raise OptionParser.ParseError,
~r/--port : Missing argument of type integer/,
fn ->
OptionParser.parse!(["--port"], switches: [port: [:integer, :keep]])
end
end

test "parse!/2 lists all supported options and aliases" do
expected_suggestion =
"""
Expand Down Expand Up @@ -208,6 +216,16 @@ defmodule OptionParserTest do
end
end

test "parse_head!/2 raises for an invalid argument with :keep" do
assert_raise OptionParser.ParseError,
~r/--port : Expected type integer, got "oops"/,
fn ->
OptionParser.parse_head!(["--port", "oops"],
switches: [port: [:integer, :keep]]
)
end
end

describe "arguments" do
test "parses until --" do
assert OptionParser.parse(
Expand Down
Loading