Class: YJCocoa::Command

Inherits:
CLAide::Command
  • Object
show all
Defined in:
lib/yjcocoa/command.rb

Overview

Usage

Direct Known Subclasses

Git, Log, Pod, Unused

Constant Summary collapse

DEFAULT_OPTIONS =
[['--help', 'Show help banner of specified command'],]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optionsObject



26
27
28
29
30
31
32
# File 'lib/yjcocoa/command.rb', line 26

def self.options
    if root_command?
        DEFAULT_ROOT_OPTIONS + DEFAULT_OPTIONS
        else
        DEFAULT_OPTIONS
    end
end

Instance Method Details

#askWithAnswers(question = "", answers = []) ⇒ Object

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

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

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


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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