Class: Cliprompt::Optionset

Inherits:
Object
  • Object
show all
Defined in:
lib/cliprompt/optionset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Optionset

Returns a new instance of Optionset.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cliprompt/optionset.rb', line 10

def initialize(options = nil)
  @choices = []
  @default = nil
  @boolean = false
  @envdefault = nil
  @aslist = false
  @type = options.class.name.downcase
  meth = "parse_#{@type}".to_sym
  if respond_to? meth
    send(meth, options)
  else
    fail OptionException, "Undefined parser ::#{meth}"
  end
end

Instance Attribute Details

#aslistObject (readonly)

Returns the value of attribute aslist.



8
9
10
# File 'lib/cliprompt/optionset.rb', line 8

def aslist
  @aslist
end

#booleanObject (readonly)

Returns the value of attribute boolean.



8
9
10
# File 'lib/cliprompt/optionset.rb', line 8

def boolean
  @boolean
end

#choicesObject (readonly)

Returns the value of attribute choices.



8
9
10
# File 'lib/cliprompt/optionset.rb', line 8

def choices
  @choices
end

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/cliprompt/optionset.rb', line 8

def default
  @default
end

#envdefaultObject (readonly)

Returns the value of attribute envdefault.



8
9
10
# File 'lib/cliprompt/optionset.rb', line 8

def envdefault
  @envdefault
end

Instance Method Details

#ask_again(question, msg) ⇒ Object



165
166
167
168
# File 'lib/cliprompt/optionset.rb', line 165

def ask_again(question, msg)
  Cliprompt.shout msg
  Cliprompt.ask question, self
end

#check_boolean(question, answer) ⇒ Object



149
150
151
152
# File 'lib/cliprompt/optionset.rb', line 149

def check_boolean(question, answer)
  return ask_again(question, Cliprompt::MSG_YES_OR_NO) unless /^([yY1](es)?|[nN0](o)?)$/.match(answer)
  !/^[yY1](es)?$/.match(answer).nil?
end

#check_choices(question, answer) ⇒ Object



154
155
156
157
# File 'lib/cliprompt/optionset.rb', line 154

def check_choices(question, answer)
  return ask_again(question, Cliprompt::MSG_CHOSE_IN_LIST) unless @choices.include?(answer)
  answer
end

#check_default(question) ⇒ Object



144
145
146
147
# File 'lib/cliprompt/optionset.rb', line 144

def check_default(question)
  return ask_again(question, Cliprompt::MSG_MANDATORY_TEXT) if @default.nil?
  @default
end

#check_list(question, answer) ⇒ Object



159
160
161
162
163
# File 'lib/cliprompt/optionset.rb', line 159

def check_list(question, answer)
  return ask_again(question, Cliprompt::MSG_CHOSE_IN_LIST) unless answer.to_i < @choices.count
  return ask_again(question, Cliprompt::MSG_INPUT_A_NUMBER) unless /[0-9]+/.match(answer)
  @choices[answer.to_i]
end

#displayObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cliprompt/optionset.rb', line 79

def display
  if @boolean
    display_boolean
  elsif @choices.count > 0
    if @aslist
      display_list
    else
      display_choices
    end
  else
    display_default
  end
end

#display_booleanObject



93
94
95
# File 'lib/cliprompt/optionset.rb', line 93

def display_boolean
  @default ? "[Y/n] " : "[y/N] "
end

#display_choicesObject



111
112
113
114
115
116
117
118
# File 'lib/cliprompt/optionset.rb', line 111

def display_choices
  back = ''
  if @choices.count > 0
    back << "(#{@choices.join(' / ')}) "
  end
  back << display_default.to_s
  return back
end

#display_defaultObject



120
121
122
# File 'lib/cliprompt/optionset.rb', line 120

def display_default
  "[#{@default}] " if @default
end

#display_default_indexObject



124
125
126
# File 'lib/cliprompt/optionset.rb', line 124

def display_default_index
  "[#{@choices.index(@default)}] " if @default
end

#display_listObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cliprompt/optionset.rb', line 97

def display_list
  back = "\n"
  choices.each_with_index do |choice, i|
    if @default == choice
      back << sprintf("> %-3s %s\n", i, choice)
    else
      back << sprintf("  %-3s %s\n", i, choice)
    end
  end
  back << "#{Cliprompt::MSG_CHOSE_A_NUMBER} "
  back << display_default_index.to_s
  return back
end

#parse_array(args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/cliprompt/optionset.rb', line 47

def parse_array(args)
  @choices = args.map(&:to_s).map do |a|
    if a[0] && a[0] == '='
      @default = a[1..-1]
    else
      a
    end
  end
  @aslist = (@choices.count > 5)
end

#parse_fixnum(arg) ⇒ Object



58
59
60
# File 'lib/cliprompt/optionset.rb', line 58

def parse_fixnum(arg)
  @default = arg.to_s
end

#parse_float(arg) ⇒ Object



62
63
64
# File 'lib/cliprompt/optionset.rb', line 62

def parse_float(arg)
  @default = arg.to_s
end

#parse_hash(args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cliprompt/optionset.rb', line 29

def parse_hash(args)
  @choices = args[:choices] || args['choices'] || []
  parse_array @choices
  if args[:default] == false || args['default'] == false
    @default ||= false
  else
    @default ||= args[:default] || args['default']
  end
  if args[:aslist] == false || args['aslist'] == false
    @aslist = false
  elsif args[:aslist] == true || args['aslist'] == true
    @aslist = true
  end
  @boolean = args[:boolean] || args['boolean']
  @default = true if (@boolean && @default.nil?)
  @envdefault = args[:env] || args['env']
end

#parse_nilclass(args) ⇒ Object

placeholder for nil type objects



26
27
# File 'lib/cliprompt/optionset.rb', line 26

def parse_nilclass(args)
end

#parse_string(arg) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cliprompt/optionset.rb', line 66

def parse_string(arg)
  if /^[yY1](es)?(\/)?[nN0](o)?/.match(arg)
    @boolean = true
    if /y(es)?(\/)?N/.match(arg)
      @default = false
    else
      @default = true
    end
  else
    @default = arg
  end
end

#validate(question, answer) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cliprompt/optionset.rb', line 128

def validate(question, answer)
  if answer == ''
    check_default question
  elsif @boolean
    check_boolean question, answer
  elsif @choices.count > 0
    if @aslist
      check_list question, answer
    else
      check_choices question, answer
    end
  else
    answer
  end
end