PromptChoice is a prompt giving possible choices Return the index starting at zero of the choice selected.
(prompt string, choices []string)
| 134 | // PromptChoice is a prompt giving possible choices |
| 135 | // Return the index starting at zero of the choice selected. |
| 136 | func PromptChoice(prompt string, choices []string) (int, error) { |
| 137 | for { |
| 138 | for i, choice := range choices { |
| 139 | _, _ = fmt.Fprintf(os.Stderr, "[%d]: %s\n", i+1, choice) |
| 140 | } |
| 141 | _, _ = fmt.Fprintf(os.Stderr, "%s: ", prompt) |
| 142 | |
| 143 | line, err := bufio.NewReader(os.Stdin).ReadString('\n') |
| 144 | fmt.Println() |
| 145 | if err != nil { |
| 146 | return 0, err |
| 147 | } |
| 148 | |
| 149 | line = strings.TrimSpace(line) |
| 150 | |
| 151 | index, err := strconv.Atoi(line) |
| 152 | if err != nil || index < 1 || index > len(choices) { |
| 153 | _, _ = fmt.Fprintln(os.Stderr, "invalid input") |
| 154 | continue |
| 155 | } |
| 156 | |
| 157 | return index - 1, nil |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func PromptURLWithRemote(prompt, name string, validRemotes []string, validators ...PromptValidator) (string, error) { |
| 162 | if len(validRemotes) == 0 { |
no test coverage detected