Build command line prompts with ease, prompts provides several TUI components to create intuitive CLI applications faster,
go get github.com/yassinebenaid/prompts
The input api allows you to prompt the user for an input , it returns the input value
- Usage:
// [...]
value, err := prompts.InputBox(prompts.InputOptions{
Secure: false, // hides the user input, very common for passwords
Label: "what is your name?",
Placeholder: "what is your name",
Required: true,
Validator: func(value string) error {// will be called when user submit, and returned error will be displayed to the user below the input
if len(value) < 3{
return fmt.Errorf("minimum len is 3")
}
return nil
},
})
if err != nil{
log.Fatal(err)
}
fmt.Println("selected " + value)
- Result:
The password input api is just normal input but with Secure
option set to true
,
- Usage:
// [...]
value, err := prompts.InputBox(prompts.InputOptions{
Secure: true, // set password mode
Label: "what is your password?",
Placeholder: "what is your password",
Required: true,
Validator: func(value string) error {// will be called when user submit, and returned error will be displayed to the user below the input
if len(value) < 3{
return fmt.Errorf("minimum len is 3")
}
return nil
},
})
if err != nil{
log.Fatal(err)
}
fmt.Println("password : " + value)
- Result:
The confirmation api can be used to prompt the user for confirmation , it returns a boolean ,
- Usage:
// [...]
const DEFAULT = true
value, err := prompts.ConfirmBox("are you sure ?", DEFAULT)
if err != nil {
log.Fatal(err)
}
fmt.Println("answer : ", value)
The radio api can be used to prompt the user to choose one of several options , it returns a the index of the checked option ,
- Usage:
// [...]
genders := []string{"male", "female"}
value, err := prompts.RadioBox("Choose your gender : ", genders)
if err != nil {
log.Fatal(err)
}
fmt.Println("gender : ", genders[value])
The select box api can be used to prompt the user to choose between several options , it returns a slice of selected indexes,
- Usage:
// [...]
hobbies := []string{"swimming", "coding", "gaming", "playing"}
value, err := prompts.SelectBox("Choose your hobbies : ", hobbies)
if err != nil {
log.Fatal(err)
}
fmt.Println("gender : ", value)
these are helper apis you can use for better alerts and messages.
prompts.Info("Info alert")
prompts.Error("Error alert")
prompts.Warning("Warning alert")
prompts.Success("Success alert")
- Result:
prompts.InfoMessage("Info message")
prompts.ErrorMessage("Error message")
prompts.WarningMessage("Warning message")
prompts.SuccessMessage("Success message")