2011-01-05 16 views
9

Ich möchte den Benutzer irgendwie bitten, ihre flickr_id, flickr_apikey und dieses Zeug zu sagen, aber id 'am glücklichsten sein, es unter meinem Installationsbefehl zu tun, so dass es am Ende nicht so lange und schwer sein wird Zeile wegen aller Argumente.Interaktive Eingabeaufforderung mit thor

so etwas wie

$ 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} 

und so weiter? Hast du die Idee, und kann es gemacht werden?

Antwort

17

In der Tat kann es!

Sie suchen nach ask.

Ein Beispiel:

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 
+0

Mein gut! Ist es so einfach? Alles funktioniert cool, außer .. das sagen ("Complete!", GRÜN) - ABER Vielen Dank! –

+0

Whoops, versuchte, sich etwas einfallen zu lassen. Naja, froh, dass es geklappt hat! – hornairs

+0

anstatt GRÜN schreibst du einfach "\ e [32m" :-)! –

2

auch möglich Farbe als 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, deafult: 'blog' 

Liste der verfügbaren Farben für Thor zu setzen, hier: http://www.rubydoc.info/github/wycats/thor/Thor/Shell/Color