Go语言 – 如何在Go中构建命令行菜单

huangapple go评论126阅读模式
英文:

Go Lang - How to build command line menu in Go

问题

我正在构建一个与用户交互的命令行应用程序,基于命令行菜单进行交互。

我使用了以下包来编写代码:https://github.com/dixonwille/wmenu。

它按预期工作,但我不知道如何获取所选菜单项的索引并将其返回给main()函数。

非常感谢任何提示或有用的链接。

谢谢。

mS

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/dixonwille/wmenu"
  7. )
  8. func createMenu(p string, m []string) {
  9. optFunc := func(option wmenu.Opt) error {
  10. fmt.Println("")
  11. fmt.Println("Option chosen: ", option.ID, option.Text)
  12. return nil
  13. }
  14. menu := wmenu.NewMenu(p)
  15. menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
  16. for i, m := range m {
  17. menu.Option(m, i, false, optFunc)
  18. }
  19. err := menu.Run()
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. // 返回 i 或 option.ID
  24. // 菜单项的索引
  25. }
  26. func main() {
  27. prompt := "选择一个水果"
  28. menuitems := []string{"苹果", "橙子", "芒果"}
  29. createMenu(prompt, menuitems)
  30. // index := createMenu(prompt, menuitems)
  31. // fmt.Println("选择的水果:", menuitems[index])
  32. }
英文:

I am building a CLI app which interacts with humans on a CLI based menu.
e.g
Sample Menu Picture

I wrote the code using the following package
https://github.com/dixonwille/wmenu.

It is working as expected but I am lost how to retrieve an index of the selected menu item and return it back to the main() function.

I highly appreciate any tips or helpful links.

Thank you

mS

  1. import (
  2. "fmt"
  3. "log"
  4. "os"
  5. "github.com/dixonwille/wmenu"
  6. )
  7. func createMenu(p string, m []string) {
  8. optFunc := func(option wmenu.Opt) error {
  9. fmt.Println("")
  10. fmt.Println("Option chosen: ", option.ID, option.Text)
  11. return nil
  12. }
  13. menu := wmenu.NewMenu(p)
  14. menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
  15. for i, m := range m {
  16. menu.Option(m, i, false, optFunc)
  17. }
  18. err := menu.Run()
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. // return i or option.ID
  23. // index of a menu item
  24. }
  25. func main() {
  26. prompt := "Select a Fruit"
  27. menuitems := []string{"Apple", "Orange", "Mango"}
  28. createMenu(prompt, menuitems)
  29. // index := createMenu(prompt, menuitems)
  30. // fmt.Println("Fruit Selected ",menuitems[index])
  31. }

答案1

得分: 0

这是一个自解释的工作示例,只做了最小的更改:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/dixonwille/wmenu"
  7. )
  8. type userInput struct {
  9. option wmenu.Opt
  10. }
  11. func (u *userInput) optFunc(option wmenu.Opt) error {
  12. u.option = option
  13. return nil
  14. }
  15. func createMenu(p string, m []string, u *userInput) {
  16. menu := wmenu.NewMenu(p)
  17. menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
  18. for i, m := range m {
  19. menu.Option(m, i, false, u.optFunc)
  20. }
  21. err := menu.Run()
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }
  26. func main() {
  27. prompt := "选择一个水果"
  28. menuitems := []string{"苹果", "橙子", "芒果"}
  29. u := &userInput{}
  30. createMenu(prompt, menuitems, u)
  31. fmt.Println("")
  32. fmt.Println("选择的选项:", u.option.ID, u.option.Text)
  33. }

我认为这不是该库设计的使用方式。

英文:

Here is the self-explanatory working example with minimal changes:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/dixonwille/wmenu"
  7. )
  8. type userInput struct {
  9. option wmenu.Opt
  10. }
  11. func (u *userInput) optFunc(option wmenu.Opt) error {
  12. u.option = option
  13. return nil
  14. }
  15. func createMenu(p string, m []string, u *userInput) {
  16. menu := wmenu.NewMenu(p)
  17. menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
  18. for i, m := range m {
  19. menu.Option(m, i, false, u.optFunc)
  20. }
  21. err := menu.Run()
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }
  26. func main() {
  27. prompt := "Select a Fruit"
  28. menuitems := []string{"Apple", "Orange", "Mango"}
  29. u := &userInput{}
  30. createMenu(prompt, menuitems, u)
  31. fmt.Println("")
  32. fmt.Println("Option chosen: ", u.option.ID, u.option.Text)
  33. }

I don't think that this is the way the library was designed to be used, though.

huangapple
  • 本文由 发表于 2021年11月19日 03:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/70025812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定