class Athena::Console::Question::Choice(T)
Overview
A question whose answer MUST be within a set of predefined answers. If the user enters an invalid answer, an error is displayed and they are prompted again.
question = ACON::Question::Choice.new "What is your favorite color?", {"red", "blue", "green"}
color = helper.ask input, output, question
This would display something like the following:
What is your favorite color?
[0] red
[1] blue
[2] green
>
The user would be able to enter the name of the color, or the index associated with it. E.g. blue
or 2
for green
.
If a Hash
is used as the choices, the key of each choice is used instead of its index.
Similar to ACON::Question
, the third argument can be set to set the default choice.
This value can also either be the actual value, or the index/key of the related choice.
question = ACON::Question::Choice.new "What is your favorite color?", {"c1" => "red", "c2" => "blue", "c3" => "green"}, "c2"
color = helper.ask input, output, question
Which would display something like :
What is your favorite color?
[c1] red
[c2] blue
[c3] green
>
Defined in:
question/choice.crConstructors
- .new(question : String, choices : Hash(String | Int32, T), default : T | Nil = nil)
- .new(question : String, choices : Indexable(T), default : Int | T | Nil = nil)
Instance methods inherited from class Athena::Console::Question::AbstractChoice(T, T?)
choices : Hash(String | Int32, T)
choices,
error_message : String
error_message,
error_message=(error_message : String) : self
error_message=,
prompt : String
prompt,
prompt=(prompt : String)
prompt=,
validator : Proc(T | Nil, ChoiceType?) | Nilvalidator(&validator : T | Nil -> ChoiceType?) : Nil validator, validator=(validator : Proc(T | Nil, ChoiceType?) | Nil) validator=
Constructor methods inherited from class Athena::Console::Question::AbstractChoice(T, T?)
new(question : String, choices : Indexable(T), default : Int | T | Nil = nil)new(question : String, choices : Hash(String | Int32, T), default : T | Nil = nil) new
Instance methods inherited from module Athena::Console::Question::Base(T?)
default : T?
default,
hidden=(hidden : Bool) : self
hidden=,
hidden? : Bool
hidden?,
hidden_fallback=(hidden_fallback : Bool)
hidden_fallback=,
hidden_fallback? : Bool
hidden_fallback?,
max_attempts : Int32 | Nil
max_attempts,
max_attempts=(attempts : Int32 | Nil) : self
max_attempts=,
multi_line=(multi_line : Bool)
multi_line=,
multi_line? : Bool
multi_line?,
normalizer : Proc(T? | String, T?) | Nilnormalizer(&normalizer : T? | String -> T?) : Nil normalizer, normalizer=(normalizer : Proc(T? | String, T?) | Nil) normalizer=, question : String question, trimmable=(trimmable : Bool) trimmable=, trimmable? : Bool trimmable?
Constructor methods inherited from module Athena::Console::Question::Base(T?)
new(question : String, default : T?)
new
Constructor Detail
def self.new(question : String, choices : Hash(String | Int32, T), default : T | Nil = nil)
#
def self.new(question : String, choices : Indexable(T), default : Int | T | Nil = nil)
#