A simple terminal-style menu for obtaining user input. Inspried heavily by inquirer.
var menu = new Menu(
Questions.NumberInput<int>("money", "How much money do you have?", defaultValue: 33),
Questions.TextInput("lastName", "What is your last name?", defaultValue: "Tyson"),
Questions.ListInput("favoriteFood", "What is your favorite food?", availableFoods),
Questions.CheckboxInput("favoriteSongs", "What are your favorite songs?", availableSongs),
Questions.ConfirmInput("isWeird", "Are you weird?"));
var answers = menu.Go();
Console.WriteLine(answers.Get<int>("money")); // Prints 33 or whatever I typed in.
// Or you can cast the result manually.
Console.WriteLine((int)answers["money"]);-
Represents a menu object with a fixed number of questions. Constructed by the only available constructor.
public Menu(params IQuestion[] questions)
Go- Shows the menu. Each question handed to the constructor is asked in the order they were handed to the constructor.
- Returns a
Dictionary<string, object>where the keys represents the question keys and the values represent the user's responses.
-
Represents a simple single-line text input that filters out all non-numeric keys and only accepts valid input for the given numeric type. Constructed by the
Questions.NumericInput<T>method.The answers object type is
T.public static IQuestion NumberInput<T>(string key, string questionText, Func<Dictionary<string, object>, bool> condition = null, T? defaultValue = null) where T : struct;
T- The numeric type desired to be used for parsing and validation.key- The question key.questionText- The question message to display to the user.condition- Optional. A predicate to determine if the question should be asked. The current answers are passed as the only parameter.defaultValue- Optional. The default value ofTto prompt to the user.
-
Represents a boolean input in the form of Yes/No. Constructed by the
Questions.ConfirmInputmethod.The answers object type is
YesNo.public static IQuestion ConfirmInput(string key, string questionText, Func<Dictionary<string, object>, bool> condition = null, YesNo defaultValue = YesNo.Yes);
key- The question key.questionText- The question message to display to the user.condition- Optional. A predicate to determine if the question should be asked. The current answers are passed as the only parameter.defaultValue- Optional. This value will be selected if the user presses enter immediately after prompted for input.
-
Represents a listbox for selecting multiple items. Constructed by the
Questions.NumericInputmethod.The answers object type is
string[].public static IQuestion CheckboxInput(string key, string questionText, string[] options, Func<Dictionary<string, object>, bool> condition = null, bool allowEmptyInput = false, string[] defaultValue = null, int pageSize = 10, bool disableSearch = false, bool showSearchOptions = true);
key- The question key.questionText- The question message to display to the user.options- The items to display in the listbox.condition- A predicate to determine if the question should be asked. The current answers are passed as the only parameter.allowEmptyInput- If true, an empty string array can be returned if the user selected no items.defaultValue- The items that will start out selected when the listbox is shown.pageSize- An options array larger than this number will force the user to scroll to see all available choices.disableSearch- If true, the user will be unable to filter the choices.showSearchOptions- If false, the helper lines shown at the bottom of the terminal showing your current search mode and some selection options will not be shown.
-
Represents a listbox for selecting a single item. Constructed by the
Questions.ListInputmethod.The answers object type is
string.public static IQuestion ListInput(string key, string questionText, string[] options, Func<Dictionary<string, object>, bool> condition = null, bool allowEmptyInput = false, string defaultValue = "", int pageSize = 10, bool disableSearch = false, bool showSearchOptions = true);
key- The question key.questionText- The question message to display to the user.options- The items to display in the listbox.condition- Optional. A predicate to determine if the question should be asked. The current answers are passed as the only parameter.allowEmptyInput- Optional. If true, null can be returned if the user's search term has filtered out all available items.defaultValue- Optional. The default selection to highlight when the listbox is shown. If this item isn't present in the options array, the first item in the options array will be highlighted.pageSize- Optional. An options array larger than this number will force the user to scroll to see all available choices.disableSearch- Optional. If true, the user will be unable to filter the choices.showSearchOptions- Optional. If false, the helper line shown at the bottom of the terminal showing your current search mode will not be shown.
-
Represents a simple single-line text input. Constructed by the
Questions.TextInputmethod.The answers object type is
string.public static IQuestion TextInput(string key, string questionText, Func<Dictionary<string, object>, bool> condition = null, bool allowEmptyInput = false, string defaultValue = "");
key- The question key.questionText- The question message to display to the user.condition- Optional. A predicate to determine if the question should be asked. The current answers are passed as the only parameter.allowEmptyInput- Optional. If true, the empty string is accepted as valid input.defaultValue- Optional. The default string to prompt to the user.
-
Attempts to retrieve the value at the specified key in the answers dictionary, casting the result to T if successful and returning the default value for T if unsuccessful. If the cast is unsuccessful, an exception will be thrown.
public static T Get<T>(this IDictionary<string, object> dictionary, string key)
T- The type to which to cast the result.dictionary- The input dictionary.key- The key.