davetron5000 / optparse-plus

Start your command line scripts off right in Ruby
http://davetron5000.github.com/optparse-plus
Apache License 2.0
521 stars 54 forks source link

How to handle a list of arguments? #86

Closed blysik closed 9 years ago

blysik commented 9 years ago

Hi,

I would like to write a CLI tool with an interface like this:

myapp hostname1 hostname2 hostname3

I can do it if I comma separate the values, but I'd really like using spaces. Is this supported somehow? Thanks!

davetron5000 commented 9 years ago

yup:

main do |*args|
  args.each do |hostname|
    puts hostname
  end
end

arg :hosts, :any, "Hosts to whatever"

It looks at the argument list for the block given to main and passes only those in, so if you did this:

main do |arg1,arg2|
end

It would set arg1 to hostname and arg2 to hostname2, but throw away hostname3.

So, it's kinda optimized for a fixed-size argument list, but if you use the splat (*args), it matches everything.

blysik commented 9 years ago

Worked perfectly, thanks! Closing out.