prompter

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: MIT Imports: 8 Imported by: 1

README

Prompter

Go Reference

Minimal prompting library for Go.

Features

  • Fluent API
  • Supports inputs, passwords and confirmations
  • Supports validations, defaults and optionals
  • Supports context canceling

Install

go get github.com/matthewmueller/prompter

Examples

prompt := prompter.Default()

// Ask for some input
name, err := prompt.Ask("What is your name?")

// Optional inputs
age, err := prompt.Optional(true).Ask("What is your age?")

// Default values
age, err = prompt.Default("21").Ask("What is your age?")

// Validations
func validPass(input string) error {
  if len(input) < 8 {
    return errors.New("password is too short")
  }
}

// Passwords
pass, err := prompt.Is(validPass).Password("What is your password?")

// Confirmations
shouldCreate, err := prompt.Confirm("Create new user? (yes/no)")

// Chaining
func validAge(input string) error {
  n, err := strconv.Atoi(input)
  if err != nil {
    return fmt.Errorf("%q must be a number")
  } else if n < 0 {
    return fmt.Errorf("%q must be greater than 0")
  }
  return nil
}
age, err := prompt.Default("21").Is(validAge).Ask("What is your age?")

Development

First, clone the repo:

git clone https://github.com/matthewmueller/prompter
cd prompter

Next, install dependencies:

go mod tidy

Finally, try running the tests:

go test ./...

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRequired = fmt.Errorf("prompter: input is required")

ErrRequired is returned when a required input is empty

Functions

This section is empty.

Types

type Prompt added in v0.0.2

type Prompt struct {
	// contains filtered or unexported fields
}

Prompt can ask for inputs and validate them

func Default

func Default() *Prompt

Default creates a default prompt using stdin and stdout

func New

func New(w io.Writer, r io.Reader) *Prompt

New prompt

func (*Prompt) Ask added in v0.0.2

func (p *Prompt) Ask(ctx context.Context, prompt string) (string, error)

Ask asks a question and returns the input

func (*Prompt) Confirm added in v0.0.2

func (p *Prompt) Confirm(ctx context.Context, prompt string) (bool, error)

Confirm asks for a confirmation and returns the input

func (*Prompt) Default added in v0.0.2

func (p *Prompt) Default(defaultTo string) *Question

Default sets the default value for the question

func (*Prompt) Is added in v0.0.2

func (p *Prompt) Is(validators ...func(string) error) *Question

Is adds validators to the question

func (*Prompt) Optional added in v0.0.2

func (p *Prompt) Optional(optional bool) *Question

Optional sets the question as optional

func (*Prompt) Password added in v0.0.2

func (p *Prompt) Password(ctx context.Context, prompt string) (string, error)

Password asks for a password and returns the input

type Question

type Question struct {
	// contains filtered or unexported fields
}

Question that can be asked

func (*Question) Ask

func (q *Question) Ask(ctx context.Context, prompt string) (string, error)

Ask asks a question and returns the input

func (*Question) Confirm

func (q *Question) Confirm(ctx context.Context, prompt string) (bool, error)

Confirm asks for a confirmation and returns the input

func (*Question) Default

func (q *Question) Default(defaultTo string) *Question

Default sets the default value for the question

func (*Question) Is

func (q *Question) Is(validators ...func(string) error) *Question

Is adds validators to the question

func (*Question) Optional

func (q *Question) Optional(optional bool) *Question

Optional sets the question as optional

func (*Question) Password

func (q *Question) Password(ctx context.Context, prompt string) (string, error)

Password asks for a password and returns the input

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳