Skip to content
Open
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
4 changes: 2 additions & 2 deletions lib/dry/cli/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def valid_value?(value)
def cast(value)
return value unless cast_callable.respond_to?(:call)

if type == :array
value.map { |el| cast_single(el) }
if array?
Array(value).map { |el| cast_single(el) }
else
cast_single(value)
end
Expand Down
29 changes: 22 additions & 7 deletions lib/dry/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ def self.call(command, arguments, prog_name)
original_arguments = arguments.dup
parsed_options = {}

OptionParser.new do |opts|
command.options.each do |option|
opts.on(*option.parser_options) do |value|
parsed_options[option.name.to_sym] = option.cast(value)
end
end

option_parser(command.options, parsed_options) do |opts|
opts.on_tail("-h", "--help") do
return Result.help
end
Expand All @@ -37,6 +31,27 @@ def self.call(command, arguments, prog_name)
Result.failure(exception.message)
end

# @since NEXT
# @api private
def self.option_parser(command_options, parsed_options)
OptionParser.new do |opts|
command_options.each do |option|
option_name = option.name.to_sym
opts.on(*option.parser_options) do |value|
value = option.cast(value)
if option.array?
parsed_options[option_name] ||= []
parsed_options[option_name] += value
else
parsed_options[option_name] = value
end
end
end

yield(opts) if block_given?
end
end

# @since 0.1.0
# @api private
#
Expand Down
10 changes: 10 additions & 0 deletions spec/support/shared_examples/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@
output = capture_output { cli.call(arguments: %w[exec test api admin]) }
expect(output).to eq("exec - Task: test - Directories: [\"api\", \"admin\"]\n")
end

it "captures repeated array options" do
output = capture_output { cli.call(arguments: %w[server --deps=dep42 --deps=dep43]) }

if RUBY_VERSION < "3.4"
expect(output).to eq("server - {:code_reloading=>true, :deps=>[\"dep42\", \"dep43\"]}\n")
else
expect(output).to eq("server - {code_reloading: true, deps: [\"dep42\", \"dep43\"]}\n")
end
end
end

context "with supported values" do
Expand Down
Loading