2011-01-05 67 views
9

我想以某种方式要求用户说出他们的flickr_id,flickr_apikey和那些东西,但是id很乐意在我的安装命令下执行它,所以它不会最终变得如此漫长而沉重因为有理由支持。与托尔的交互式提示

所以像

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

等等?你有没有想到,可以做到吗?

回答

17

确实可以!

您正在寻找ask

一个例子:

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

我的好!这很容易!所有的工作都很酷,除了说(“完成!”,绿色) - 但非常感谢你! – 2011-01-05 15:25:28

+0

哎呀,试图得到幻想。噢,很高兴它解决了! – hornairs 2011-01-05 15:27:04

+0

而不是绿色,你只需写“\ e [32m”:-)! – 2011-01-05 15:27:54

2

也可以设置颜色为标志可用颜色的

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' 

完整列表托尔,在这里:http://www.rubydoc.info/github/wycats/thor/Thor/Shell/Color