davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

flags don't aggregate into an array #320

Closed yegor256 closed 3 months ago

yegor256 commented 3 months ago

Thanks for your library, it's amazing! Now, the problem. I'm doing this:

require 'gli'
include GLI::App
command :bar do |c|
  c.flag([:foo], type: Array)
  c.action do |global, options, args|
    p options
  end
end
run(["bar", "--foo", "first", "--foo", "second"])

I'm getting (the --foo first is lost 👎 ):

{"foo"=>["second"], :foo=>["second"]}

Instead, I would expect:

{"foo"=>["first", "second"], :foo=>["first", "second"]}

This is what I'm getting, when I change the last line to:

run(["bar", "--foo", "first,second"])

I believe, it's a normal practice to pass options in command line by using the same flag multiple times. Joining values with a comma may be a good practice too, but not always suitable.

Maybe I missed some configuration option for this particular behavior?

davetron5000 commented 3 months ago

There are two ways to do this. The one that is simplest is to set multiple: true:

  require 'gli' 
  include GLI::App
  command :bar do |c|
→   c.flag([:foo], multiple: true)
    c.action do |global, options, args|
      p options
    end
  end
  run(["bar", "--foo", "first", "--foo", "second"])

That said, what you have should work with comma-delimited arguments, due the use of type: Array. For me,

run(["bar", "--foo", "first,second"])

# {"foo"=>["first", "second"], :foo=>["first", "second"]}
yegor256 commented 3 months ago

@davetron5000 it works! your library is awesome (I'm migrating from slop)

davetron5000 commented 3 months ago

Thanks! Let me know how things go - I haven't been doing many updates as it's generally stable, but I'm still around to support it.

yegor256 commented 3 months ago

@davetron5000 works perfectly, here. If any more issues, I will report :)