prompter

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 7 Imported by: 184

README

prompter

Build Status Coverage Status MIT License Go Documentation

Description

utility for easy prompting in Golang

Synopsis

twitterID := prompter.Prompt("Enter your twitter ID", "")
lang := prompter.Choose("Which language do you like the most?", []string{"Perl", "Golang", "Scala", "Ruby"}, "Perl")
passwd := prompter.Password("Enter your password")
var likeSushi bool = prompter.YN("Do you like sushi?", true)
var likeBeer bool = prompter.YesNo("Do you like beer?", false)

Features

  • Easy to use
  • Care non-interactive (not a tty) environment
    • Default is used and the process is not blocked
  • No howeyc/gopass (which uses cgo) dependency
    • cross build friendly
  • Customizable prompt setting by using &prompter.Prompter{} directly

License

MIT

Author

Songmu

Documentation

Overview

Package prompter is utility for easy prompting

Index

Examples

Constants

View Source
const VERSION = "0.5.0"

VERSION version of prompter

Variables

This section is empty.

Functions

func Choose

func Choose(message string, choices []string, defaultChoice string) string

Choose make a choice

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	lang := prompter.Choose("Which language do you like the most?", []string{"Perl", "Golang", "Scala", "Ruby"}, "Perl")
	fmt.Println("Perl")
	fmt.Printf("Great! You like %s!", lang)
}
Output:

Which language do you like the most? (Perl/Golang/Scala/Ruby) [Perl]: Perl
Great! You like Perl!

func Password

func Password(message string) string

Password asks password

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	passwd := prompter.Password("Enter your password")
	_ = passwd
	fmt.Println("****")
	fmt.Print("I got your password! :P")
}
Output:

Enter your password: ****
I got your password! :P

func Prompt

func Prompt(message, defaultAnswer string) string

Prompt simple prompting

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	answer := prompter.Prompt("Enter your twitter ID", "")
	_ = answer
	fmt.Println("Songmu")
	fmt.Printf("Hi Songmu!")
}
Output:

Enter your twitter ID: Songmu
Hi Songmu!

func Regexp

func Regexp(message string, reg *regexp.Regexp, defaultAnswer string) string

Regexp checks the answer by regexp

func YN

func YN(message string, defaultToYes bool) bool

YN y/n choice

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	if prompter.YN("Do you like sushi?", true) {
		fmt.Println("y")
		fmt.Print("Nice! Let's go sushi bar!")
	}
}
Output:

Do you like sushi? (y/n) [y]: y
Nice! Let's go sushi bar!

func YesNo

func YesNo(message string, defaultToYes bool) bool

YesNo yes/no choice

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	if !prompter.YesNo("Do you like beer?", false) {
		fmt.Println("no")
		fmt.Print("Oops!")
	}
}
Output:

Do you like beer? (yes/no) [no]: no
Oops!

Types

type Prompter

type Prompter struct {
	Message string
	// choices of answer
	Choices    []string
	IgnoreCase bool
	Default    string
	// specify answer pattern by regexp. When both Choices and Regexp are specified, Regexp takes a priority.
	Regexp *regexp.Regexp
	// for passwords and so on.
	NoEcho     bool
	UseDefault bool
	// contains filtered or unexported fields
}

Prompter is object for prompting

Example
package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {
	input := (&prompter.Prompter{
		Choices:    []string{"aa", "bb", "cc"},
		Default:    "aa",
		Message:    "please select",
		IgnoreCase: true,
	}).Prompt()
	fmt.Println("aa")
	fmt.Printf("your input is %s.", input)
}
Output:

please select (aa/bb/cc) [aa]: aa
your input is aa.

func (*Prompter) Prompt

func (p *Prompter) Prompt() string

Prompt displays a prompt and returns answer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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