2011-01-05 10 views
9

Quiero de alguna manera pedirle al usuario que diga su flickr_id, flickr_apikey y esas cosas, pero id 'esté muy contento de hacerlo bajo mi comando de instalación para que no termine siendo tan largo y pesado línea debido a alle los argumentos.Indicación interactiva con thor

así que algo como

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

y así sucesivamente? ¿Entiendes la idea y se puede hacer?

Respuesta

17

¡De hecho, puede!

Está buscando ask.

Un ejemplo:

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

Mi buena! ¿Es así de fácil? Todo funcionó genial, excepto ... el dicho ("¡Completa!", VERDE) - PERO ¡Muchas gracias! –

+0

Vaya, traté de ser elegante. ¡Oh, bueno, me alegro de que funcionó! – hornairs

+0

en vez de VERDE simplemente escribe "\ e [32m" :-)! –

2

También es posible configurar el color como símbolo

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' 

lista completa de colores disponibles para Thor, aquí: http://www.rubydoc.info/github/wycats/thor/Thor/Shell/Color

Cuestiones relacionadas