【问题标题】:Interactive prompt with thor与雷神互动提示
【发布时间】:2011-01-05 14:05:44
【问题描述】:

我想以某种方式要求用户说出他们的 flickr_id、flickr_apikey 和那些东西,但我很乐意在我的安装命令下这样做,这样它就不会因为 alle 而变得如此冗长而沉重论据。

有点像

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

等等?你明白这个想法吗?能做到吗?

【问题讨论】:

    标签: ruby-on-rails ruby installation thor


    【解决方案1】:

    确实可以!

    您正在寻找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
    

    【讨论】:

    • 天哪!有那么容易吗!一切都很酷,除了……说(“完成!”,绿色) - 但非常感谢!
    • 而不是绿色你只写“\e[32m”:-)!
    • 我建议保留将 id 和键作为参数传递的选项,在这种情况下跳过询问。这样命令将是脚本表。
    • 您可以使用 Thor::Shell::Color::GREEN(位于:rubydoc.info/github/wycats/thor/Thor/Shell/Color
    【解决方案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, default: 'blog'
    

    雷神可用颜色的完整列表,请点击此处:http://www.rubydoc.info/github/wycats/thor/Thor/Shell/Color

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      相关资源
      最近更新 更多