How you do specify multiple arguments or parameters in Thor?
Asked Answered
S

1

13

my_gem hello name1 name2 name3 give me a

my_gem hello requires at least 1 argument: my_gem hello name

Should I just parse them and separate the arguments with a delimeter?

e.g

my_gem hello name1,name2,name3,nameN

In the file it would look like

class MyCLI < Thor
  desc "hello NAMES", "say hello to names"

  def hello(names)
    say "hello #{names.split(',')}"
  end
end

Or is there anyway to do this?

Sender answered 2/5, 2012 at 5:42 Comment(0)
S
20

Yes, there is another way of doing this.

require 'thor'
class TestApp < Thor
    desc "hello NAMES", "long desc"
    def hello(*names)
        say "hello #{names.join('; ')}"
    end
end

And it can be called like this:

$ thor test_app:hello first second third
hello first; second; third
Smoothspoken answered 14/6, 2012 at 8:33 Comment(1)
That's also known as the splat operator: #4170537 and skorks.com/2009/08/method-arguments-in-rubyKingship

© 2022 - 2024 — McMap. All rights reserved.