diff --git a/examples/inputfilesuggestion.go b/examples/inputfilesuggestion.go index 8872cc4d..e3290bec 100644 --- a/examples/inputfilesuggestion.go +++ b/examples/inputfilesuggestion.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "path/filepath" @@ -21,7 +22,16 @@ var q = []*survey.Question{ Suggest: suggestFiles, Help: "Any file; do not need to exist yet", }, - Validate: survey.Required, + Validate: survey.ComposeValidators( + survey.Required, + func(file interface{}) error { + if file == "?" { + return errors.New("? is not a valid file name") + } + + return nil + }, + ), }, } diff --git a/input.go b/input.go index a1abab14..de72c83a 100644 --- a/input.go +++ b/input.go @@ -185,7 +185,7 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) { cursor.Up(1) // if we ran into the help string - if i.answer == config.HelpInput && i.Help != "" { + if i.answer == config.HelpInput && i.Help != "" && !i.showingHelp { // show the help and prompt again i.showingHelp = true return i.Prompt(config) diff --git a/input_test.go b/input_test.go index 38b0c645..399b4e46 100644 --- a/input_test.go +++ b/input_test.go @@ -184,6 +184,21 @@ func TestInputPrompt(t *testing.T) { }, "Satoshi Nakamoto", }, + { + "Test Input prompt interaction and using help input as answer", + &Input{ + Message: "Are you confused?", + Help: "You can use ? as answer when in doubt", + }, + func(c *expect.Console) { + c.ExpectString("Are you confused?") + c.SendLine("?") + c.ExpectString("You can use ? as answer when in doubt") + c.SendLine("?") + c.ExpectEOF() + }, + "?", + }, { // https://en.wikipedia.org/wiki/ANSI_escape_code // Device Status Report - Reports the cursor position (CPR) to the