class YJCocoa::Command

Usage

Constants

DEFAULT_OPTIONS

Public Class Methods

options() click to toggle source
# File lib/yjcocoa/command.rb, line 26
def self.options
    if root_command?
        DEFAULT_ROOT_OPTIONS + DEFAULT_OPTIONS
        else
        DEFAULT_OPTIONS
    end
end

Public Instance Methods

askWithAnswers(question = "", answers = []) click to toggle source

@abstract 向用户提问,并获取输入

@param question 问题 @param answers 答案数组

@return 用户输入的选项。用户不输入时,默认返回第一个选项

# File lib/yjcocoa/command.rb, line 40
def askWithAnswers(question = "", answers = [])
    answersDowncase = answers.map { |item| item.downcase }
    return answersDowncase.first if answersDowncase.count <= 1            
    result = ""
    block = Proc.new { |again|
        str = again ? "可能的答案是 [" : "#{question}? ["
        answers.each_with_index { |item, i|
            str << " #{(i == 0) ? item.underlined : item}"
            str << " /" unless i == answers.count - 1
        }
        str << " ]: "
        print str
    }
    # do
    block.call(false)
    loop do
        result = STDIN.gets.chomp.downcase
        if result.empty?
            result = answersDowncase.first
            puts "select default: #{result}".yellow
        else
            result = "yes" if result == "y"
            result = "no" if result == "n"
        end
        break if answersDowncase.include?(result)
        block.call(true)
    end
    return result
end