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

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

Go Lang - How to build command line menu in Go

问题

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

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

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

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

谢谢。

mS

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/dixonwille/wmenu"
)

func createMenu(p string, m []string) {

	optFunc := func(option wmenu.Opt) error {
		fmt.Println("")
		fmt.Println("Option chosen: ", option.ID, option.Text)
		return nil
	}

	menu := wmenu.NewMenu(p)
	menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
	for i, m := range m {
		menu.Option(m, i, false, optFunc)
	}
	err := menu.Run()
	if err != nil {
		log.Fatal(err)
	}

	// 返回 i 或 option.ID
	// 菜单项的索引
}

func main() {

	prompt := "选择一个水果"
	menuitems := []string{"苹果", "橙子", "芒果"}

	createMenu(prompt, menuitems)

	// index := createMenu(prompt, menuitems)
	// fmt.Println("选择的水果:", menuitems[index])
}
英文:

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


import (
	"fmt"
	"log"
	"os"

	"github.com/dixonwille/wmenu"
)

func createMenu(p string, m []string) {

	optFunc := func(option wmenu.Opt) error {
		fmt.Println("")
		fmt.Println("Option chosen: ", option.ID, option.Text)
		return nil
	}

	menu := wmenu.NewMenu(p)
	menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
	for i, m := range m {
		menu.Option(m, i, false, optFunc)

	}
	err := menu.Run()
	if err != nil {
		log.Fatal(err)
	}

	// return i or option.ID
	// index of a menu item
}

func main() {

	prompt := "Select a Fruit"
	menuitems := []string{"Apple", "Orange", "Mango"}

	createMenu(prompt, menuitems)

	// index := createMenu(prompt, menuitems)
	// fmt.Println("Fruit Selected ",menuitems[index])
}

答案1

得分: 0

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

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/dixonwille/wmenu"
)

type userInput struct {
	option wmenu.Opt
}

func (u *userInput) optFunc(option wmenu.Opt) error {
	u.option = option
	return nil
}

func createMenu(p string, m []string, u *userInput) {
	menu := wmenu.NewMenu(p)
	menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
	for i, m := range m {
		menu.Option(m, i, false, u.optFunc)
	}
	err := menu.Run()
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	prompt := "选择一个水果"
	menuitems := []string{"苹果", "橙子", "芒果"}
	u := &userInput{}
	createMenu(prompt, menuitems, u)
	fmt.Println("")
	fmt.Println("选择的选项:", u.option.ID, u.option.Text)
}

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

英文:

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

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/dixonwille/wmenu"
)

type userInput struct {
	option wmenu.Opt
}

func (u *userInput) optFunc(option wmenu.Opt) error {
	u.option = option
	return nil
}

func createMenu(p string, m []string, u *userInput) {
	menu := wmenu.NewMenu(p)
	menu.ChangeReaderWriter(os.Stdin, os.Stdout, os.Stderr)
	for i, m := range m {
		menu.Option(m, i, false, u.optFunc)

	}
	err := menu.Run()
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	prompt := "Select a Fruit"
	menuitems := []string{"Apple", "Orange", "Mango"}
	u := &userInput{}
	createMenu(prompt, menuitems, u)
	fmt.Println("")
	fmt.Println("Option chosen: ", u.option.ID, u.option.Text)
}

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:

确定