mirror of
https://github.com/manifoldco/promptui.git
synced 2026-03-14 22:35:53 +01:00
Improve documentation (#27)
This commit is contained in:
parent
55f4ad73fc
commit
ffe73ca679
4 changed files with 74 additions and 11 deletions
70
README.md
70
README.md
|
|
@ -11,9 +11,66 @@ Interactive prompt for command-line applications.
|
|||
[](https://goreportcard.com/report/github.com/manifoldco/promptui)
|
||||
[](./LICENSE.md)
|
||||
|
||||
## Usage
|
||||
## Overview
|
||||
|
||||
### Selection
|
||||

|
||||
|
||||
Promptui is a library providing a simple interface to create command-line
|
||||
prompts for go. It can be easily integrated into
|
||||
[spf13/cobra](https://github.com/spf13/cobra),
|
||||
[urfave/cli](https://github.com/urfave/cli) or any cli go application.
|
||||
|
||||
Promptui has two main input modes:
|
||||
|
||||
- `Prompt` provides a single line for user input. Prompt supports
|
||||
optional live validation, confirmation and masking the input.
|
||||
|
||||
- `Select` provides a list of options to choose from. Select supports
|
||||
pagination, search, detailed view and custom templates.
|
||||
|
||||
For a full list of options check [GoDoc](https://godoc.org/github.com/manifoldco/promptui).
|
||||
|
||||
## Basic Usage
|
||||
|
||||
### Prompt
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/manifoldco/promptui"
|
||||
)
|
||||
|
||||
func main() {
|
||||
validate := func(input string) error {
|
||||
_, err := strconv.ParseFloat(input, 64)
|
||||
if err != nil {
|
||||
return errors.New("Invalid number")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
prompt := promptui.Prompt{
|
||||
Label: "Number",
|
||||
Validate: validate,
|
||||
}
|
||||
|
||||
result, err := prompt.Run()
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Prompt failed %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("You choose %q\n", result)
|
||||
}
|
||||
```
|
||||
|
||||
### Select
|
||||
|
||||
```go
|
||||
package main
|
||||
|
|
@ -25,11 +82,10 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
values := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}
|
||||
|
||||
prompt := promptui.Select{
|
||||
Label: "Select Day",
|
||||
Items: values,
|
||||
Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
||||
"Saturday", "Sunday"},
|
||||
}
|
||||
|
||||
_, result, err := prompt.Run()
|
||||
|
|
@ -42,3 +98,7 @@ func main() {
|
|||
fmt.Printf("You choose %q\n", result)
|
||||
}
|
||||
```
|
||||
|
||||
### More Examples
|
||||
|
||||
See full list of [examples](https://github.com/manifoldco/promptui/tree/master/_examples)
|
||||
|
|
|
|||
|
|
@ -62,5 +62,5 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
fmt.Printf("You choose number %d: %+v\n", i+1, peppers[i])
|
||||
fmt.Printf("You choose number %d: %s\n", i+1, peppers[i].Name)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
|
|
@ -10,7 +11,10 @@ import (
|
|||
func main() {
|
||||
validate := func(input string) error {
|
||||
_, err := strconv.ParseFloat(input, 64)
|
||||
return err
|
||||
if err != nil {
|
||||
return errors.New("Invalid number")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
prompt := promptui.Prompt{
|
||||
|
|
|
|||
|
|
@ -7,11 +7,10 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
values := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
|
||||
prompt := promptui.Select{
|
||||
Label: "Select Number",
|
||||
Items: values,
|
||||
Label: "Select Day",
|
||||
Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
||||
"Saturday", "Sunday"},
|
||||
}
|
||||
|
||||
_, result, err := prompt.Run()
|
||||
|
|
@ -21,5 +20,5 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
fmt.Printf("You choose %s\n", result)
|
||||
fmt.Printf("You choose %q\n", result)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue