Hi, I’m @Luo Zhu

This article was first published on luo Zhu’s official website

This article synchronizes in the public account “luo Zhu early teahouse”, reprint please contact the author.

Creation is not easy, form a habit, quality three even!

Interactive prompts for command line applications.

We created Promptui because we want to make it easy and fun to explore cloud services using the Manifold CLI

preview

Promptui is a library that provides a simple interface to create command line prompts for GO. It can be easily integrated into SPF13 / Cobra, Urfave/CLI, or any GO CLI program.

Promptui has two input modes:

  • PromptA single line of input is provided to the user. Prompt supports optional real-time validation, validation, and mask input.
  • SelectA list of options to choose from is provided. Select support for paging, search, detailed view, and custom templates.

Based on using

Prompt

package main

import (
	"errors"
	"fmt"
	"strconv"

	"github.com/manifoldco/promptui"
)

func main(a) {
	validate := func(input string) error {
		_, err := strconv.ParseFloat(input, 64)
		iferr ! =nil {
			return errors.New("Invalid number")}return nil
	}

	prompt := promptui.Prompt{
		Label:    "Number",
		Validate: validate,
	}

	result, err := prompt.Run()

	iferr ! =nil {
		fmt.Printf("Prompt failed %v\n", err)
		return
	}

	fmt.Printf("You choose %q\n", result)
}
Copy the code

Select

package main

import (
	"fmt"

	"github.com/manifoldco/promptui"
)

func main(a) {
	prompt := promptui.Select{
		Label: "Select Day",
		Items: []string{"Monday"."Tuesday"."Wednesday"."Thursday"."Friday"."Saturday"."Sunday"},
	}

	_, result, err := prompt.Run()

	iferr ! =nil {
		fmt.Printf("Prompt failed %v\n", err)
		return
	}

	fmt.Printf("You choose %q\n", result)
}
Copy the code

confirm

package main

import (
	"fmt"

	"github.com/manifoldco/promptui"
)

func main(a) {
	prompt := promptui.Prompt{
		Label:     "Delete Resource",
		IsConfirm: true,
	}

	result, err := prompt.Run()

	iferr ! =nil {
		fmt.Printf("Prompt failed %v\n", err)
		return
	}

	fmt.Printf("You choose %q\n", result)
}
Copy the code

More examples

See the complete list examples