questions

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 5 Imported by: 0

README

Go Reference

CLI Questions

Simple CLI questions and answers

Installation
go get github.com/matt9mg/go-cli-questions
Examples
q := questions.NewQuestion()
answer, err := q.Ask("hi there")

if err != nil {
    log.Fatalln(err)
}

log.Println(answer)

// asks a question but the terminal does not display the input
pw, err := q.AskSecurely("password")

if err != nil {
    log.Fatalln(err)
}

log.Println(pw)

Custom Template Handler

type CustomTemplate struct {}

func (*CustomTemplate) Write(data []byte) error {
	t, err := template.ParseGlob("/*.tmpl")

	if err != nil {
		return err
	}

	return t.ExecuteTemplate(os.Stdout, "hello.tmpl", data)
}

func main() {
	q := questions.NewQuestion(
		questions.WithCustomTemplate(&CustomTemplate{}),
	)
	a, err := q.Ask("hi there")

	if err != nil {
		log.Fatalln(err)
	}

	log.Println(a)
}
LICENSE

This project is licensed under the MIT License - see the LICENSE.md file for details

Disclaimer

We take no legal responsibility for anything this code is used for.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

type ConfigFunc

type ConfigFunc func(*Config)

func WithCustomTemplate

func WithCustomTemplate(w Writer) ConfigFunc

WithCustomTemplate takes a Writer interface that allows you to customise the look and feel of the questions asked.

type Question

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

func NewQuestion

func NewQuestion(configs ...ConfigFunc) *Question

NewQuestion returns a Question Client based on the config functional options. Provide additional config functional options to further configure the behavior of the Question client, such as changing the render.

func (*Question) Ask

func (q *Question) Ask(question string) (string, error)

Ask takes a question and returns the answer or an error

func (*Question) AskForConfirmation added in v1.1.0

func (q *Question) AskForConfirmation(question string) (bool, error)

AskForConfirmation asks your question and appends a yes/no response expectation If the answer is no what is expected it prompts the user again with a hint A true|false boolean is returned or an error

func (*Question) AskSecurely

func (q *Question) AskSecurely(question string) (string, error)

AskSecurely asks your question and returns the answer or an error The terminal input is hidden when entering the answer

type Stdout

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

func NewStdout

func NewStdout() *Stdout

func (*Stdout) Write

func (s *Stdout) Write(data []byte) error

type Writer

type Writer interface {
	Write(data []byte) error
}

Jump to

Keyboard shortcuts

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