Class: Cliprompt::Optionset
- Inherits:
-
Object
- Object
- Cliprompt::Optionset
- Defined in:
- lib/cliprompt/optionset.rb
Instance Attribute Summary collapse
-
#aslist ⇒ Object
readonly
Returns the value of attribute aslist.
-
#boolean ⇒ Object
readonly
Returns the value of attribute boolean.
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#envdefault ⇒ Object
readonly
Returns the value of attribute envdefault.
Instance Method Summary collapse
- #ask_again(question, msg) ⇒ Object
- #check_boolean(question, answer) ⇒ Object
- #check_choices(question, answer) ⇒ Object
- #check_default(question) ⇒ Object
- #check_list(question, answer) ⇒ Object
- #display ⇒ Object
- #display_boolean ⇒ Object
- #display_choices ⇒ Object
- #display_default ⇒ Object
- #display_default_index ⇒ Object
- #display_list ⇒ Object
-
#initialize(options = nil) ⇒ Optionset
constructor
A new instance of Optionset.
- #parse_array(args) ⇒ Object
- #parse_fixnum(arg) ⇒ Object
- #parse_float(arg) ⇒ Object
- #parse_hash(args) ⇒ Object
-
#parse_nilclass(args) ⇒ Object
placeholder for nil type objects.
- #parse_string(arg) ⇒ Object
- #validate(question, answer) ⇒ Object
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( = nil) @choices = [] @default = nil @boolean = false @envdefault = nil @aslist = false @type = .class.name.downcase meth = "parse_#{@type}".to_sym if respond_to? meth send(meth, ) else fail OptionException, "Undefined parser ::#{meth}" end end |
Instance Attribute Details
#aslist ⇒ Object (readonly)
Returns the value of attribute aslist.
8 9 10 |
# File 'lib/cliprompt/optionset.rb', line 8 def aslist @aslist end |
#boolean ⇒ Object (readonly)
Returns the value of attribute boolean.
8 9 10 |
# File 'lib/cliprompt/optionset.rb', line 8 def boolean @boolean end |
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
8 9 10 |
# File 'lib/cliprompt/optionset.rb', line 8 def choices @choices end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
8 9 10 |
# File 'lib/cliprompt/optionset.rb', line 8 def default @default end |
#envdefault ⇒ Object (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 |
#display ⇒ Object
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_boolean ⇒ Object
93 94 95 |
# File 'lib/cliprompt/optionset.rb', line 93 def display_boolean @default ? "[Y/n] " : "[y/N] " end |
#display_choices ⇒ Object
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_default ⇒ Object
120 121 122 |
# File 'lib/cliprompt/optionset.rb', line 120 def display_default "[#{@default}] " if @default end |
#display_default_index ⇒ Object
124 125 126 |
# File 'lib/cliprompt/optionset.rb', line 124 def display_default_index "[#{@choices.index(@default)}] " if @default end |
#display_list ⇒ Object
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 |