Skip to content

Commit 5b736a2

Browse files
mateochrMateo Chronis
andauthored
Fix default flag with multiple set to true (#8)
Co-authored-by: Mateo Chronis <[email protected]>
1 parent f2fca35 commit 5b736a2

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

cli_options/lib/cli_options/schema.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@ defmodule CliOptions.Schema do
295295
defp validate_default_value(opts) do
296296
opts = maybe_add_default_value(opts, opts[:type])
297297

298-
case validate_type_match(opts[:type], opts[:default]) do
298+
case validate_type_match(option_type(opts), opts[:default]) do
299299
true ->
300300
{:ok, opts}
301301

302302
false ->
303303
{:error,
304-
":default should be of type #{inspect(opts[:type])}, got: #{inspect(opts[:default])}"}
304+
":default should be of type #{inspect(option_type(opts))}, got: #{inspect(opts[:default])}"}
305305
end
306306
end
307307

@@ -310,6 +310,12 @@ defmodule CliOptions.Schema do
310310
defp maybe_add_default_value(opts, _other), do: opts
311311

312312
defp validate_type_match(_type, nil), do: true
313+
314+
defp validate_type_match({:list, type}, value) when is_list(value),
315+
do: Enum.all?(value, &validate_type_match(type, &1))
316+
317+
defp validate_type_match({:list, _type}, value) when not is_list(value), do: false
318+
313319
defp validate_type_match(:integer, value), do: is_integer(value)
314320
defp validate_type_match(:counter, value), do: is_integer(value)
315321
defp validate_type_match(:string, value), do: is_binary(value)

cli_options/test/cli_options/schema_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ defmodule CliOptions.SchemaTest do
170170
assert_raise ArgumentError, message, fn ->
171171
CliOptions.Schema.new!(schema)
172172
end
173+
174+
# with multiple set and default not a list
175+
schema = [foo: [type: :string, multiple: true, default: "foo"]]
176+
177+
message =
178+
"invalid schema for :foo, :default should be of type {:list, :string}, got: \"foo\""
179+
180+
assert_raise ArgumentError, message, fn ->
181+
CliOptions.Schema.new!(schema)
182+
end
183+
184+
# with multiple set and default invalid type
185+
schema = [foo: [type: :integer, multiple: true, default: ["foo"]]]
186+
187+
message =
188+
"invalid schema for :foo, :default should be of type {:list, :integer}, got: [\"foo\"]"
189+
190+
assert_raise ArgumentError, message, fn ->
191+
CliOptions.Schema.new!(schema)
192+
end
173193
end
174194

175195
test "with duplicate aliases" do

cli_options/test/cli_options_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ defmodule CliOptionsTest do
297297
CliOptions.parse(["--file", "foo.ex,bar.ex"], schema)
298298
end
299299

300+
test "multiple with default set" do
301+
schema = [file: [type: :string, multiple: true, default: ["foo.ex", "bar.ex"]]]
302+
303+
assert {:ok, {[file: ["foo.ex", "bar.ex"]], [], []}} = CliOptions.parse([], schema)
304+
305+
# multiple works as expected
306+
assert {:ok, {[file: ["bar.ex"]], [], []}} = CliOptions.parse(["--file", "bar.ex"], schema)
307+
308+
assert {:ok, {[file: ["foo.ex", "bar.ex"]], [], []}} =
309+
CliOptions.parse(["--file", "foo.ex", "--file", "bar.ex"], schema)
310+
end
311+
300312
test "multiple with separator set" do
301313
# without separator used
302314
schema = [project: [type: :string, short: "p", multiple: true, separator: ","]]

0 commit comments

Comments
 (0)