// stringListInput requests a list of a comma delimitted strings // // Use this where you need to take a list of string values, // such as the case where you want to take a list of strings // // It parses the strings based on commas, or double commas, // if the string input needs to include commas func stringListInput(ui cli.Ui, text string) ([]string, error) { in, err := ui.Ask(text + " [list,of,strings]") if err != nil { return nil, err } // if the user used double commas, single commas will be ignored if strings.Contains(in, ",,") { return strings.Split(in, ",,"), nil } return strings.Split(in, ","), nil }
// intInput requests an integer input (signed) // // Use intInput if you need to retrieve an integer. func intInput(ui cli.Ui, text string) (int, error) { for { input, err := ui.Ask(text + " [integer]:") if err != nil { return 0, err } i64, err := strconv.ParseInt(input, 10, 64) if err == nil { return int(i64), nil } out := "Invalid input, please try again. Valid integer expressions include: 1, 12, -300 etc." ui.Output(strings.TrimSpace(out)) } }
// boolInput requests a boolean input // // Use this where you need to take a boolean value. If you are // looking for a confirmation prompt, however, use 'yesNo' func boolInput(ui cli.Ui, text string) (bool, error) { for { input, err := ui.Ask(text + " [boolean]:") if err != nil { return false, err } switch input { case "yes": return true, err case "no": return false, err default: b, err := strconv.ParseBool(input) if err == nil { return b, nil } out := " Invalid input, please try again. Valid boolean expressions include: true, false, 0, 1 etc." ui.Output(strings.TrimSpace(out)) } } }
// stringInput requests textual input // // Use this where you need to take a string value, or where // you would want to parse a string input yourself func stringInput(ui cli.Ui, text string) (string, error) { return ui.Ask(text + " [string]:") }
// yesNo requests confirmation of something // // Use this for deciding what to do, like whether to request // additional information from a user or whether the user // intended to do something. func yesNo(ui cli.Ui, text string) (bool, error) { i, err := ui.Ask(text + " [y to confirm]") return (i == "y"), err }