Documentation
¶
Overview ¶
Package interact collect some interactive methods for CLI
Index ¶
- Constants
- Variables
- func AnswerIsYes(defVal ...bool) bool
- func Ask(question, defVal string, fn func(ans string) error, maxTimes ...int) string
- func Checkbox(title string, options any, defOpts []string, allowQuit ...bool) []string
- func Choice(title string, options any, defOpt string, allowQuit ...bool) string
- func Confirm(message string, defVal ...bool) bool
- func GetHiddenInput(message string, trimmed bool) string
- func MultiSelect(title string, options any, 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(question ...string) string
- func ResetIO()
- func SelectOne(title string, options any, defOpt string, allowQuit ...bool) string
- func SetInput(in io.Reader)
- func SetOutput(out io.Writer)
- func SingleSelect(title string, options any, defOpt string, allowQuit ...bool) string
- func Unconfirmed(message string, defVal ...bool) bool
- type Collector
- func (c *Collector) AddParam(p InputParameter) error
- func (c *Collector) AddParams(ps ...InputParameter) error
- func (c *Collector) MustParam(name string) InputParameter
- func (c *Collector) Param(name string) (InputParameter, bool)
- func (c *Collector) Results() maputil.Data
- func (c *Collector) Run() error
- type ComOptions
- type InputParameter
- type Interactive
- type ItemFace
- type Options
- type Question
- type RunFace
- type Select
- type SelectResult
- type StepHandler
- type StepsRun
- type Value
Constants ¶
const ( // OK success exit code OK = 0 // ERR error exit code ERR = 2 )
Variables ¶
the global input output stream
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 ¶
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 ¶
Prompt query and read user answer.
Usage:
answer, err := Prompt(context.Background(), "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 SelectOne ¶
SelectOne 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 SingleSelect ¶
SingleSelect is alias of method SelectOne()
func Unconfirmed ¶
Unconfirmed a question, returns bool
Types ¶
type Collector ¶ added in v3.1.0
type Collector struct {
// contains filtered or unexported fields
}
Collector information collector 信息收集者 cli input values collector
func (*Collector) AddParam ¶ added in v3.1.1
func (c *Collector) AddParam(p InputParameter) error
AddParam to collector
func (*Collector) AddParams ¶ added in v3.1.1
func (c *Collector) AddParams(ps ...InputParameter) error
AddParams definitions at once.
func (*Collector) MustParam ¶ added in v3.1.1
func (c *Collector) MustParam(name string) InputParameter
MustParam get from collector
func (*Collector) Param ¶ added in v3.1.1
func (c *Collector) Param(name string) (InputParameter, bool)
Param get from collector
type ComOptions ¶ added in v3.1.0
ComOptions struct
type InputParameter ¶ added in v3.1.1
type InputParameter interface {
Type() string
Name() string
Desc() string
Value() structs.Value
Set(v string) error
Run() error
}
InputParameter interface
type Question ¶
type Question struct {
// Q the question message
Q string
// Func validate user input answer is right.
// if not set, will only check the 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 ¶
NewQuestion instance.
Usage:
q := NewQuestion("Please input your name?")
ans := q.Run().String()
type Select ¶
type Select struct {
// Title message for select. e.g "Your city?"
Title string
// Options the items data for select. allow: []int, []string, map[string]string
Options any
// 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
func NewSelect ¶
NewSelect instance.
- items allow: []int, []string, map[string]string
Usage:
s := NewSelect("Your city?", []string{"chengdu", "beijing"})
r := s.Run()
key := r.KeyString() // "1"
val := r.String() // "beijing"
func (*Select) EnableMulti ¶ added in v3.1.1
EnableMulti select
type SelectResult ¶
SelectResult data store
type StepsRun ¶
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
