manifoldco / promptui

Interactive prompt for command-line applications
https://www.manifold.co
BSD 3-Clause "New" or "Revised" License
6.07k stars 336 forks source link

multiple/series of prompts #175

Open patrick-hermann-sva opened 3 years ago

patrick-hermann-sva commented 3 years ago

is it possible w/ promptui to implement a series of prompts (like pass an array of multiple "question" objects)

alexraileanu commented 3 years ago

@patrick-hermann-sva i use something like this for multiple prompts:

type CPrompt struct {
    PUI     promptui.Prompt
    VarName string
}

var vars = map[string]string{}
var prompts = []CPrompt{
    {
        PUI: promptui.Prompt{
            Label: "First Question",
        },
        VarName: "firstQ",
    },
    {
        PUI: promptui.Prompt{
            Label: "Second Question",
        },
        VarName: "secondQ",
    },
}
for _, prompt := range prompts {
    vars[prompt.VarName], _ = prompt.PUI.Run()
}

which will return an array like:

map[string]string{"firstQ":"test", "secondQ":"test"}

this is very bare-bones without any kind of validation passed but it can be easily extended to support validations.