Hyper-Interaction is a very small tiny library to help making a command line app become interactive. It allowes user to fill in qustionaire, making configuration or just a simple information input.
go get -u github.com/hyperjumptech/hyper-interactive
import (
"fmt"
interactive "github.com/hyperjumptech/hyper-interactive"
)
...
name := interactive.Ask("Whats your name ?", "Bruce Wayne", false)
fmt.Println("Hi " + name)
...
The command line will look like
$ interact
Whats your name ? [default "Bruce Wayne"] : Ferdinand
Hi Ferdinand
If you see [default __]
, simply hit enter
will use the default value as your answer.
All question type have a confirmation flag in their argument. For example
// the last argument is a confirmation flag
name := interactive.Ask("Whats your name ?", "Bruce Wayne", true)
fmt.Println("Hi " + name)
This will result
$ interact
Whats your name ? [default "Bruce Wayne"] : Ferdinand
"Ferdinand", are you sure? (y/n/Y/N) [default : Y] ? Y
Hi Ferdinand
The logic in all of those ask function will make sure the user fill in the correct answer.
- User are ensured to choose valid option
- User are ensured to specify valid number
- User are ensured to specify valid time format
- etc
options := []string {
"One","Two","Three",
"Four","Five","Six",
"Seven","Eight","Nine",
"Ten",
}
choosen := interactive.Select("Please choose", options, 1,1, true)
fmt.Printf("You choose number %d\n", choosen)
This will be shown as
$ interact
Please choose :
(1) One (3) Three (5) Five (7) Seven (9) Nine
(2) Two (4) Four (6) Six (8) Eight (10) Ten
Choose from number above [default : (1) One] ? 5
(5) Five - Are you sure ? (y/n/Y/N) [default : Y] ? Y
You choose number 5
func Select(question string, options []string, startFrom, defaultOption int, confirm bool) int
func AskNumber(question string, from, to, def int, confirm bool) int
func AskTime(question string, def time.Time, confirm bool) time.Time
. The answer must follow format2006-01-02 15:04:05 -0700
func Ask(question, defaultAnswer string, confirm bool) string
func Confirm(question string, def bool) bool
- more to come...