Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash type is not supported by the repeatable option. #768

Open
patrick-motard opened this issue Jan 12, 2022 · 0 comments · May be fixed by #769
Open

Hash type is not supported by the repeatable option. #768

patrick-motard opened this issue Jan 12, 2022 · 0 comments · May be fixed by #769

Comments

@patrick-motard
Copy link

Here is an example to replicate the bug, as well as examples of running it, including the output and expected output. Repeatable options don't seem to work with the hash type.

#!/usr/bin/env ruby
# frozen_string_literal: true

require "thor"

class ExampleCLI < Thor
  desc "example", "shows how broken things are"
  option :doesnt,
    :required => false,
    :default => {},
    :repeatable => true,
    :type => :hash,
    :desc => "Should be repeatable but isn't"
  option :works,
    :repeatable => true,
    :required => false,
    :type => :string
  def create()
    puts options
  end

  def self.exit_on_failure?
    true
  end
end

ExampleCLI.start(ARGV)

You hand in two hashes and expect them to be set as two hashes in an array. What do you get? one hash, not in an array, with all key/vals merged into one hash.

./script/example create --works string1 --works string2 --doesnt key1:value1 key2:value2 --doesnt key3:val3 key4:val4
{"doesnt"=>{"key1"=>"value1", "key2"=>"value2", "key3"=>"val3", "key4"=>"val4"}, "works"=>["string1", "string2"]}

Instead, you should end up with an array of two different hashes:

{"doesnt"=>[{"key1"=>"value1", "key2"=>"value2"}, {"key3"=>"val3", "key4"=>"val4"}], "works"=>["string1", "string2"]}

Ordering doesn't seem to matter. It's still broken.

 ./script/example create --doesnt key3:val3 key4:val4 --works string1 --works string2 --doesnt key1:value1 key2:value2
{"doesnt"=>{"key3"=>"val3", "key4"=>"val4", "key1"=>"value1", "key2"=>"value2"}, "works"=>["string1", "string2"]}

if your keys in your objects are the same, youll end up with one object whos key values are the last ones you handed in.

./script/example create --works string1 --works string2 --doesnt key1:value1 key2:value2 --doesnt key1:dup1 key2:dup3
{"doesnt"=>{"key1"=>"dup1", "key2"=>"dup3"}, "works"=>["string1", "string2"]}

Instead, you should end up with an array of two different hashes:

{"doesnt"=>[{"key1"=>"value1", "key2"=>"value2"}, {"key1"=>"dup1", "key2"=>"dup3"}], "works"=>["string1", "string2"]}
patrick-motard added a commit to patrick-motard/thor that referenced this issue Jan 17, 2022
They treated like every other type now. Instead of merging each key/val
into a hash, each flag is treated like it's own hash that should be put
into an array.

fixes rails#768
@patrick-motard patrick-motard linked a pull request Jan 17, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant