AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.08k stars 351 forks source link

How to accept the return value #295

Closed lostmaniac closed 3 years ago

lostmaniac commented 4 years ago

I want to use a variable to receive the value set by the user, how should it be received?

package main

import (
    "fmt"

    "github.com/AlecAivazis/survey/v2"
)

func OptionsSelect(msg string, Opt []string, help string) string {
    color := ""
    prompt := &survey.Select{
        Message: msg,
        Options: Opt,
        Help:    help,
    }
    aaa := survey.AskOne(prompt, &color)
    if aaa != nil {
        return aaa.Error()
    } else {
        return aaa.Error()
    }
}

func UserInput(msg string) string {
    name := ""
    prompt := &survey.Input{
        Message: msg,
    }
    bbb := survey.AskOne(prompt, &name)
    if bbb != nil {
        return bbb.Error()
    }
    return ""
}

func main() {
    ddd := UserInput("domefd")
    ccc := OptionsSelect("run", []string{"sss", "rrr", "eeee", "qqqq"}, "nnnnn")
    fmt.Printf(ccc, ddd)
}

thank you very much

MarkusFreitag commented 4 years ago

To make your example code working, you just need to fix your return values.


package main

import (
  "fmt"

  "github.com/AlecAivazis/survey/v2"
)

func OptionsSelect(msg string, Opt []string, help string) string {
  color := ""
  prompt := &survey.Select{
    Message: msg,
    Options: Opt,
    Help:    help,
  }
  aaa := survey.AskOne(prompt, &color)
  if aaa != nil {
    return aaa.Error()
  }
  return color
}

func UserInput(msg string) string {
  name := ""
  prompt := &survey.Input{
    Message: msg,
  }
  bbb := survey.AskOne(prompt, &name)
  if bbb != nil {
    return bbb.Error()
  }
  return name
}

func main() {
  ddd := UserInput("domefd")
  ccc := OptionsSelect("run", []string{"sss", "rrr", "eeee", "qqqq"}, "nnnnn")
  fmt.Println(ccc, ddd)
}
lostmaniac commented 4 years ago

thank you very much