Interactive prompt with thor
Asked Answered
G

2

12

I want to somehow ask the user to say their flickr_id, flickr_apikey and that stuff, but id' be most happy to do it under my install command so it dosn't end up being such a long and heavy line because of alle the arguments.

so something like

$ thor PhotoonRails:install
We're about to install your system.. blaa, blaa, blaa...
We have to know you're Flick ID, get i here http://idgettr.com/
Flickr ID: {here you should type your id}

We also has to know you're flick api key, make one here ...
API Key: {here you should type your key}

and so on? Do you get the idea, and can it be done?

Goodkin answered 5/1, 2011 at 14:5 Comment(0)
J
23

Indeed it can!

You are looking for ask.

An example:

class PhotoonRails < Thor
  desc "install", "install my cool stuff"
  def install
    say("We're about to install your system.. blaa, blaa, blaa... We have to know you're Flick ID, get i here http://idgettr.com")
    flickr_id = ask("Flickr ID: ")

    say("We also has to know you're flick api key, make one here ...")
    flickr_api_key = ask("API Key: ")

    # validate flickr creds
    # do cool stuff

    say("Complete!", GREEN)
  end
end
Jacobson answered 5/1, 2011 at 15:17 Comment(4)
My good! Is it that easy! All worked cool, except.. the say("Complete!", GREEN) - BUT Thank you SO much!Goodkin
instead of GREEN you just write "\e[32m" :-)!Goodkin
I'd suggest leaving the option of passing on the id and keys as parameters, and in that case skip the asking. This way the command will be scrip-table.Galvani
You can use Thor::Shell::Color::GREEN (found on: rubydoc.info/github/wycats/thor/Thor/Shell/Color)Granulocyte
E
6

It's also possible to set color as a symbol

say "Caution!", :yellow
ask 'Agreed?', :bold

# Choose limit:
ask "We have noticed.", :green, limited_to: ['proceed', 'exit']

# Default value (possible without :blue)
ask 'Type app name', :blue, default: 'blog'

Full list of available colors for Thor, here: http://www.rubydoc.info/github/wycats/thor/Thor/Shell/Color

Electrothermal answered 25/10, 2014 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.