如何在Go中使用Cli访问命令数组?

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

How can I access an array of commands w/ Cli in Go?

问题

当前正在使用Codegangsta的Cli库。我运行的命令如下:

myGoProgram arg1 arg2 arg3 --flag1 flag1arg 

运行

app.Action = func(c *cli.Context) {
    fmt.Println("Context: ", c.Args())
}

返回:[arg1 arg2 arg3 --flag1 flag1arg]c.Args()的返回类型

我如何访问arg1arg2arg3,但不包括--flag1flag1arg?我需要遍历这个数组吗?

英文:

Currently using Codegangsta's Cli Library. I run a command like so:

myGoProgram arg1 arg2 arg3 --flag1 flag1arg 

Running

app.Action = func(c *cli.Context) {
		fmt.Println("Context: ", c.Args())
}

returns: [arg1 arg2 arg3 --flag1 flag1arg] (c.Args()'s return type)

How can I access arg1, arg2, and arg3, but not --flag1 or flag1arg? Do I have to iterate through this array?

答案1

得分: 0

好的,我会为你翻译这段代码:

func noFlagCliArgs(cliArgs []string) []string {
    a := cliArgs
    for pos, arg := range cliArgs {
        if strings.Compare(string(arg[0]), "-") == 0 {
            // 去掉两个元素,以消除标志和其参数
            a = append(a[:pos], a[pos+2:]...)
        }
    }
    return a
}

这段代码的作用是创建两个具有相同内容的数组,并根据需要删除元素,以创建一个只包含原始命令的数组。

英文:

Ok, so what I did was create two arrays of the same contents and just removed elements accordingly to create an array of only the raw commands.

func noFlagCliArgs(cliArgs []string) []string {
	a := cliArgs
	for pos, arg := range cliArgs {
		if strings.Compare(string(arg[0]), "-") == 0 {
			// Cut out two to get rid of flag and its argument
			a = append(a[:pos], a[pos+2:]...)
		}
	}
	return a
}

答案2

得分: 0

我认为你可能没有正确定义你的标志,看看我准备的这个示例:

package main

import (
	"fmt"
	"os"

	"github.com/codegangsta/cli"
)

func main() {
	app := cli.NewApp()
	app.Name = "so-example"
	app.Usage = "演示CLI用法"

	app.Commands = []cli.Command{
		{
			Name:  "one",
			Usage: "第一件事",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "test",
					Value: "foobar",
					Usage: "一些很酷的东西",
				},
			},
			Action: func(c *cli.Context) {
				fmt.Println("完成任务", c.Command.Name, "带参数", c.Args())
				if c.String("test") != "foobar" {
					fmt.Println("foobar去哪了?")
				}
			},
		},
		{
			Name:  "two",
			Usage: "第二件事",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "test",
					Value: "foobar",
					Usage: "一些很酷的东西",
				},
			},
			Action: func(c *cli.Context) {
				fmt.Println("完成任务", c.Command.Name, "带参数", c.Args())
				fmt.Println("测试值:", c.String("test"))
				if c.String("test") != "foobar" {
					fmt.Println("foobar去哪了?")
				}
			},
		},
	}
	app.Run(os.Args)
}

快速示例:

无标志

$ ./sandbox two foo bar hello world
完成任务 two 带参数 [foo bar hello world]
测试值: foobar

使用标志

$ ./sandbox two foo bar hello world --test "see"
完成任务 two 带参数 [foo bar hello world]
测试值: see
foobar去哪了?
英文:

I think you might not have your flags defined correctly, see this example I cooked up:

package main

import (
	"fmt"
	"os"
	
	"github.com/codegangsta/cli"
)

func main() {
	app := cli.NewApp()
	app.Name = "so-example"
	app.Usage = "Demonstrate CLI usage"

	app.Commands = []cli.Command{
		{
			Name:    "one",
			Usage:   "first thing",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "test",
					Value: "foobar",
					Usage: "something cool",
				},
			},
			Action: func(c *cli.Context) {
				fmt.Println("completed task", c.Command.Name, " with args ", c.Args())
				if (c.String("test") != "foobar") {
					fmt.Println("Where'd foobar go?")
				}
			},
		},
		{
			Name:    "two",
			Usage:   "second thing",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "test",
					Value: "foobar",
					Usage: "something cool",
				},
			},
			Action: func(c *cli.Context) {
				fmt.Println("completed task", c.Command.Name, " with args ", c.Args())
				fmt.Println("Testing value:", c.String("test"))
				if (c.String("test") != "foobar") {
					fmt.Println("Where'd foobar go?")
				}
			},
		},
	}
	app.Run(os.Args)
}

Quick examples:

Without flag

$ ./sandbox two foo bar hello world
completed task two  with args  [foo bar hello world]
Testing value: foobar

With flag

$ ./sandbox two foo bar hello world --test "see"
completed task two  with args  [foo bar hello world]
Testing value: see
Where'd foobar go?

huangapple
  • 本文由 发表于 2015年12月22日 13:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/34408854.html
匿名

发表评论

匿名网友

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

确定