input

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2021 License: MIT Imports: 11 Imported by: 0

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 prompting complex input via Option struct.

The documentation is on GoDoc.

Install

Use go get to install this package:

$ go get github.com/qiuzhanghua/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: "Bob",
    Required: true,
    Loop:     true,
})

I10N

go get github.com/qiuzhanghua/i10n

add local resource to your app, see

https://github.com/qiuzhanghua/SimpleInput

i10n.SetDefaultLang("zh-CN")

// ...

input.T=i10n.T

You can check other examples in here.

Author

Taichi Nakashima

邱张华

Documentation

Overview

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

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

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

Index

Examples

Constants

View Source
const LineSep = "\n"

LineSep is the separator for windows or unix systems

Variables

This section is empty.

Functions

This section is empty.

Types

type Format added in v0.2.0

type Format func(string, ...interface{}) string
var T Format = func(stringId string, a ...interface{}) string {
	if format, ok := messages[stringId]; ok {
		return fmt.Sprintf(format, a...)
	}
	return ""
}

type Options

type Options struct {
	// Default is the default value which is used when nothing
	// 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 continues to ask until it receives valid input.

If the user sends SIGINT (Ctrl+C) while reading input, it catches it and return it as an 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 continues to ask until it receives valid input.

If the user sends SIGINT (Ctrl+C) while reading input, it catches it and return it as an 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