Improve documentation (#27)

This commit is contained in:
҈҈҈Luiz Branco 2017-11-23 14:26:04 -04:00 committed by GitHub
commit ffe73ca679
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 11 deletions

View file

@ -11,9 +11,66 @@ Interactive prompt for command-line applications.
[![Go Report Card](https://goreportcard.com/badge/github.com/manifoldco/promptui)](https://goreportcard.com/report/github.com/manifoldco/promptui)
[![License](https://img.shields.io/badge/license-BSD-blue.svg)](./LICENSE.md)
## Usage
## Overview
### Selection
![promptui](https://media.giphy.com/media/xUNda0Ngb5qsogLsBi/giphy.gif)
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)

View file

@ -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)
}

View file

@ -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{

View file

@ -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)
}