input

package module
v0.0.0-...-548a7d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2018 License: MIT Imports: 11 Imported by: 147

README

go-input Go Documentation Travis MIT License

go-input is a Go package for reading user input in console.

Here is the some good points compared with other/similar packages. It can handle SIGINT (Ctrl+C) while reading input and returns error. It allows to change IO interface as io.Writer and io.Reader so it's easy to test of your go program with this package (This package is also well-tested!). It also supports raw mode input (reading input without prompting) for multiple platform (Darwin, Linux and Windows). Not only this it allows to prompt complex input via Option struct.

The documentation is on GoDoc.

Install

Use go get to install this package:

$ go get github.com/tcnksm/go-input

Usage

The following is the simple example,

ui := &input.UI{
    Writer: os.Stdout,
    Reader: os.Stdin,
}

query := "What is your name?"
name, err := ui.Ask(query, &input.Options{
    Default: "tcnksm",
    Required: true,
    Loop:     true,
})

You can check other examples in here.

Contribution

  1. Fork (https://github.com/tcnksm/go-input/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

Author

Taichi Nakashima

Documentation

Overview

Package input reads user input at the console. http://github.com/tcnksm/go-input

ui := &input.UI{
    Writer: os.Stdout,
    Reader: os.Stdin,
}

query := "What is your name?"
name, err := ui.Ask(query, &input.Options{
    Default: "tcnksm",
    Required: true,
    Loop:     true,
})

Index

Examples

Constants

View Source
const LineSep = "\n"

LineSep is the separator for windows or unix systems

Variables

View Source
var (
	// Errs are error returned by input functions.
	// It's useful for handling error from outside of input functions.
	ErrEmpty       = errors.New("default value is not provided but input is empty")
	ErrNotNumber   = errors.New("input must be number")
	ErrOutOfRange  = errors.New("input is out of range")
	ErrInterrupted = errors.New("interrupted")
)

Functions

This section is empty.

Types

type Options

type Options struct {
	// Default is the default value which is used when no thing
	// is input.
	Default string

	// Loop loops asking user to input until getting valid input.
	Loop bool

	// Required returns error when input is empty.
	Required bool

	// HideDefault hides default var output.
	HideDefault bool

	// HideOrder hides order comment ('Enter a value')
	HideOrder bool

	// Hide hides user input is prompting console.
	Hide bool

	// Mask hides user input and will be matched by MaskVal
	// on the screen. By default, MaskVal is asterisk(*).
	Mask bool

	// MaskDefault hides default value. By default, MaskVal is asterisk(*).
	MaskDefault bool

	// MaskVal is a value which is used for masking user input.
	// By default, MaskVal is asterisk(*).
	MaskVal string

	// ValidateFunc is function to do extra validation of user
	// input string. By default, it does nothing (just returns nil).
	ValidateFunc ValidateFunc
}

Options is structure contains option for input functions.

type UI

type UI struct {
	// Writer is where output is written. For example a query
	// to the user will be written here. By default, it's os.Stdout.
	Writer io.Writer

	// Reader is source of input. By default, it's os.Stdin.
	Reader io.Reader
	// contains filtered or unexported fields
}

UI is user-interface of input and output.

func DefaultUI

func DefaultUI() *UI

DefaultUI returns default UI. It outputs to stdout and intputs from stdin.

func (*UI) Ask

func (i *UI) Ask(query string, opts *Options) (string, error)

Ask asks the user for input using the given query. The response is returned as string. Error is returned based on the given option. If Loop is true, it continue to ask until it receives valid input.

If the user sends SIGINT (Ctrl+C) while reading input, it catches it and return it as a error.

Example
ui := &UI{
	// In real world, Reader is os.Stdin and input comes
	// from user actual input.
	Reader: bytes.NewBufferString("tcnksm"),
	Writer: ioutil.Discard,
}

query := "What is your name?"
name, _ := ui.Ask(query, &Options{})

fmt.Println(name)
Output:

tcnksm

func (*UI) Select

func (i *UI) Select(query string, list []string, opts *Options) (string, error)

Select asks the user to select a item from the given list by the number. It shows the given query and list to user. The response is returned as string from the list. By default, it checks the input is the number and is not out of range of the list and if not returns error. If Loop is true, it continue to ask until it receives valid input.

If the user sends SIGINT (Ctrl+C) while reading input, it catches it and return it as a error.

Example
ui := &UI{
	// In real world, Reader is os.Stdin and input comes
	// from user actual input.
	Reader: bytes.NewBufferString("3\n"),
	Writer: ioutil.Discard,
}

query := "Which language do you prefer to use?"
lang, _ := ui.Select(query, []string{"go", "Go", "golang"}, &Options{
	Default: "Go",
})

fmt.Println(lang)
Output:

golang

type ValidateFunc

type ValidateFunc func(string) error

ValidateFunc is function to validate the user input.

The following example shows validating the user input is 'Y' or 'n' when asking yes or no question.

Example
ui := &UI{
	// In real world, Reader is os.Stdin and input comes
	// from user actual input
	Reader: bytes.NewBufferString("Y\n"),
	Writer: ioutil.Discard,
}

query := "Do you love golang [Y/n]"
ans, _ := ui.Ask(query, &Options{
	// Define validateFunc to validate user input is
	// 'Y' or 'n'. If not returns error.
	ValidateFunc: func(s string) error {
		if s != "Y" && s != "n" {
			return fmt.Errorf("input must be Y or n")
		}

		return nil
	},
})

fmt.Println(ans)
Output:

Y

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL