package main import ( "bufio" "fmt" "os" "strings" ) func prompt(question string, options []string, defaultAnswer string, currentValue string, permitEmpty bool) (string, error) { r := bufio.NewReader(os.Stdin) if len(currentValue) == 0 { currentValue = "None" } if len(defaultAnswer) == 0 { defaultAnswer = "None" } if options == nil { fmt.Print("->", question, "( defaults to:", defaultAnswer, " current value:", currentValue, " )", ":") } else { fmt.Print("->", question, "( defaults to:", defaultAnswer, " current value:", currentValue, " )[ ", strings.Join(options, " | "), " ]", ":") } response, _ := r.ReadString('\n') response = strings.Trim(response, "\n") if len(response) == 0 && currentValue != "None" { response = currentValue } if len(response) == 0 && defaultAnswer != "None" { response = defaultAnswer } if options != nil { validOption := false for _, opt := range options { if strings.EqualFold(response, opt) { validOption = true response = opt break } } if !validOption { return response, fmt.Errorf("invalid option") } } if !permitEmpty && len(response) == 0 { return response, fmt.Errorf("empty response") } return response, nil }