YesNoPrompt performs a console prompt that asks the user for Yes/No answer. If the user just press Enter (aka. doesn't type anything) it returns the fallback value.
(message string, fallback bool)
| 38 | // |
| 39 | // If the user just press Enter (aka. doesn't type anything) it returns the fallback value. |
| 40 | func YesNoPrompt(message string, fallback bool) bool { |
| 41 | options := "Y/n" |
| 42 | if !fallback { |
| 43 | options = "y/N" |
| 44 | } |
| 45 | |
| 46 | r := bufio.NewReader(os.Stdin) |
| 47 | |
| 48 | var s string |
| 49 | for { |
| 50 | fmt.Fprintf(os.Stderr, "%s (%s) ", message, options) |
| 51 | |
| 52 | s, _ = r.ReadString('\n') |
| 53 | |
| 54 | s = strings.ToLower(strings.TrimSpace(s)) |
| 55 | |
| 56 | switch s { |
| 57 | case "": |
| 58 | return fallback |
| 59 | case "y", "yes": |
| 60 | return true |
| 61 | case "n", "no": |
| 62 | return false |
| 63 | } |
| 64 | } |
| 65 | } |
searching dependent graphs…