Documentation
¶
Overview ¶
Package prompter is utility for easy prompting
Index ¶
- Constants
- func Choose(message string, choices []string, defaultChoice string) string
- func Password(message string) string
- func Prompt(message, defaultAnswer string) string
- func Regexp(message string, reg *regexp.Regexp, defaultAnswer string) string
- func YN(message string, defaultToYes bool) bool
- func YesNo(message string, defaultToYes bool) bool
- type Prompter
Examples ¶
Constants ¶
View Source
const VERSION = "1.0.0"
VERSION version of prompter
Variables ¶
This section is empty.
Functions ¶
func Choose ¶
Choose make a choice
Example ¶
package main
import (
"fmt"
"github.com/Songmu/prompter"
)
func main() {
lang := prompter.Choose("Which language do you like the most?", []string{"Perl", "Golang", "Scala", "Ruby"}, "Perl")
fmt.Println("Perl")
fmt.Printf("Great! You like %s!", lang)
}
Output: Perl Great! You like Perl!
func Password ¶
Password asks password
Example ¶
package main
import (
"fmt"
"github.com/Songmu/prompter"
)
func main() {
passwd := prompter.Password("Enter your password")
_ = passwd
fmt.Println("****")
fmt.Print("I got your password! :P")
}
Output: **** I got your password! :P
func Prompt ¶
Prompt simple prompting
Example ¶
package main
import (
"fmt"
"github.com/Songmu/prompter"
)
func main() {
answer := prompter.Prompt("Enter your twitter ID", "")
_ = answer
fmt.Println("Songmu")
fmt.Printf("Hi Songmu!")
}
Output: Songmu Hi Songmu!
Types ¶
type Prompter ¶
type Prompter struct {
Message string
// choices of answer
Choices []string
IgnoreCase bool
Default string
// specify answer pattern by regexp. When both Choices and Regexp are specified, Regexp takes a priority.
Regexp *regexp.Regexp
// for passwords and so on.
NoEcho bool
UseDefault bool
// contains filtered or unexported fields
}
Prompter is object for prompting
Example ¶
package main
import (
"fmt"
"github.com/Songmu/prompter"
)
func main() {
input := (&prompter.Prompter{
Choices: []string{"aa", "bb", "cc"},
Default: "aa",
Message: "please select",
IgnoreCase: true,
}).Prompt()
fmt.Println("aa")
fmt.Printf("your input is %s.", input)
}
Output: aa your input is aa.
func (*Prompter) Prompt ¶
Prompt displays a prompt and returns the answer. On a read error, it reports the error to stderr and returns an empty string. Use PromptWithError to handle read errors directly.
func (*Prompter) PromptWithError ¶ added in v1.0.0
PromptWithError displays a prompt and returns the answer or a read error. EOF and read errors are not treated as empty answers or replaced by Default.
Click to show internal directories.
Click to hide internal directories.