Documentation
¶
Overview ¶
Package interact collect some interactive methods for CLI
Index ¶
- Constants
- func AnswerIsYes(defVal ...bool) bool
- func Ask(question, defVal string, fn func(ans string) error, maxTimes ...int) string
- func Checkbox(title string, options interface{}, defOpts []string, allowQuit ...bool) []string
- func Choice(title string, options interface{}, defOpt string, allowQuit ...bool) string
- func Confirm(message string, defVal ...bool) bool
- func GetHiddenInput(message string, trimmed bool) string
- func MultiSelect(title string, options interface{}, defOpts []string, allowQuit ...bool) []string
- func Prompt(ctx context.Context, query, defaultAnswer string) (string, error)
- func Query(question, defVal string, fn func(ans string) error, maxTimes ...int) string
- func ReadFirst(question string) (string, error)
- func ReadInput(question string) (string, error)
- func ReadLine(question string) (string, error)
- func ReadPassword(message ...string) string
- func SingleSelect(title string, options interface{}, defOpt string, allowQuit ...bool) string
- func Unconfirmed(message string, defVal ...bool) bool
- type Interactive
- type Option
- type Question
- type RunFace
- type Select
- type StepHandler
- type StepsRun
- type Value
Constants ¶
const ( // OK success exit code OK = 0 // ERR error exit code ERR = 2 )
Variables ¶
This section is empty.
Functions ¶
func AnswerIsYes ¶
AnswerIsYes check user inputted answer is right Usage:
fmt.Print("are you OK?")
ok := AnswerIsYes()
ok := AnswerIsYes(true)
func Ask ¶
Ask a question and return the result of the input. Usage:
answer := Ask("Your name?", "", nil)
answer := Ask("Your name?", "tom", nil)
answer := Ask("Your name?", "", nil, 3)
func GetHiddenInput ¶ added in v1.1.2
GetHiddenInput interactively prompts for input without echoing to the terminal. usage:
// askPassword
pwd := GetHiddenInput("Enter Password:")
func MultiSelect ¶
MultiSelect select multi of the options, returns selected option values. like SingleSelect(), but allow select multi option
func Prompt ¶ added in v1.1.2
Prompt query and read user answer.
Usage:
answer,err := Prompt(context.TODO(), "your name?", "")
from package golang.org/x/tools/cmd/getgo
func ReadLine ¶
ReadLine read one line from user input. Usage:
in := ReadLine("")
ans := ReadLine("your name?")
func ReadPassword ¶ added in v1.1.2
ReadPassword from terminal
func SingleSelect ¶ added in v1.1.2
SingleSelect select one of the options, returns selected option value map options:
{
// option value => option name
'a' => 'chengdu',
'b' => 'beijing'
}
array options:
{
// only name, value will use index
'chengdu',
'beijing'
}
func Unconfirmed ¶ added in v1.1.2
Unconfirmed a question, returns bool
Types ¶
type Interactive ¶
type Interactive struct {
Name string
}
Interactive
func New ¶
func New(name string) *Interactive
type Question ¶
type Question struct {
// Q the question string
Q string
// Func validate user input answer is right.
// if not set, will only check answer is empty.
Func func(ans string) error
// DefVal default value
DefVal string
// MaxTimes maximum allowed number of errors, 0 is don't limited
MaxTimes int
// contains filtered or unexported fields
}
Question definition
func NewQuestion ¶ added in v1.1.2
NewQuestion instance. usage:
q := NewQuestion("Please input your name?")
val := q.Run().String()
type Select ¶
type Select struct {
// Title message for select. e.g "Your city?"
Title string
// Options the options data for select. allow: []int,[]string,map[string]string
Options interface{}
// DefOpt default option when not input answer
DefOpt string
// DefOpts use for `MultiSelect` is true
DefOpts []string
// DisableQuit option. if is false, will display "quit" option. default False
DisableQuit bool
// MultiSelect allow multi select. default False
MultiSelect bool
// contains filtered or unexported fields
}
Select definition
type StepHandler ¶ added in v1.1.2
StepHandler for steps run
type StepsRun ¶ added in v1.1.2
type StepsRun struct {
// Steps step name and handler define.
// {
// // step 1
// func(ctx context.Context) { do something.}
// // step 2
// func(ctx context.Context) { do something.}
// }
Steps []StepHandler
// contains filtered or unexported fields
}
StepsRun follow the steps to run