2011-01-05 69 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