2 回答
TA贡献1845条经验 获得超8个赞
好的,所以我所做的是创建两个相同内容的数组,并相应地删除元素以创建一个仅包含原始命令的数组。
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
}
TA贡献2011条经验 获得超2个赞
我认为您可能没有正确定义标志,请参阅我编写的这个示例:
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)
}
快速示例:
无旗
$ ./sandbox two foo bar hello world
completed task two with args [foo bar hello world]
Testing value: foobar
带旗
$ ./sandbox two foo bar hello world --test "see"
completed task two with args [foo bar hello world]
Testing value: see
Where'd foobar go?
- 2 回答
- 0 关注
- 172 浏览
添加回答
举报