当我从“–”更改为没有“–”标志时,得到不同的输出。

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

Getting different outputs when I change from "--" to no -- on flag

问题

好的,以下是翻译好的内容:

好的,我在处理标志(flags)时遇到了问题。我认为我目前走在正确的轨道上,但是如果我在我的PrintRepeater程序中输入"go run *.go printrepeater 3 --slow",我的println语句将输出true,但是如果我输入"go run *.go printrepeater 3 slow",我得到的是false。

testCli.go

package main

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

func main() {
	app := cli.NewApp()
	app.Name = "Learn CLI"
	app.Usage = "basic things in cli"
	/*	app.Flags = []gangstaCli.Flag{
		gangstaCli.StringFlag{
			Name:  "s",
			//Value:  "y",
			Usage: "slowing down",
		},
	}*/

	//	app.Flags = []cli.Flag{
	//cli.StringFlag{"slow", "yes", "for when you have too much time", ""},
	//}

	app.Commands = []cli.Command{
		{
			Name:   "countup",
			Usage:  "counting up",
			Action: PrintRepeater,
		},

		{
			Name:   "countdown",
			Usage:  "counting down",
			Action: GoDown,
		},

		{
			Name:  "printrepeater",
			Usage: "prints hello x number of times",
			Flags: []cli.Flag{
				cli.BoolFlag{
					Name:  "slow",
					Usage: "to slow things down by a certian amount",
				},
			},
			Action: PrintRepeater,
		},
	}

	app.Run(os.Args)
}

PrintRepeater.go

package main

import "github.com/codegangsta/cli"
import "strconv"

func PrintRepeater(c *cli.Context) {
	println(c.Bool("slow"))

	i1 := c.Args()[0]
	i2, err := strconv.Atoi(i1)
	if err != nil {
		println(err)
	}
	for i := i2; i >= 1; i-- {
		println("hello")
	}
}
英文:

Ok, I have having trouble with flags. I think I currently am on the right track but my println in my PrintRepeater program will output a true if I type "go run *.go printrepeater 3 --slow", but if I type "go run *.go printrepeater 3 slow" I get flase.

testCli.go

package main

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

func main() {
app := cli.NewApp()
app.Name = "Learn CLI"
app.Usage = "basic things in cli"
/*	app.Flags = []gangstaCli.Flag{
	gangstaCli.StringFlag{
		Name: "s",
		//Value:  "y",
		Usage: "slowing down",
	},
	}*/

  //	app.Flags = []cli.Flag{
  //cli.StringFlag{"slow", "yes", "for when you have too much time", ""},
 //	}

 app.Commands = []cli.Command{
	{
		Name:   "countup",
		Usage:  "counting up",
		Action: PrintRepeater,
	},

	{
		Name:   "countdown",
		Usage:  "counting down",
		Action: GoDown,
	},

	{
		Name:  "printrepeater",
		Usage: "prints hello x number of times",
		Flags: []cli.Flag{
			cli.BoolFlag{
				Name:  "slow",
				Usage: "to slow things down by a certian amount",
			},
		},
		Action: PrintRepeater,
	},
}

app.Run(os.Args)
}

PrintRepeater.go

package main

import "github.com/codegangsta/cli"
import "strconv"

func PrintRepeater(c *cli.Context) {
    println(c.Bool("slow"))

    i1 := c.Args()[0]
    i2, err := strconv.Atoi(i1)
    if err != nil {
	    println(err)
    }
    for i := i2; i >= 1; i-- {
	    println("hello")
    }
}

答案1

得分: 1

标志以“-”开头,这就是它们的定义方式。

当你使用printrepeater 3 slow时,“slow”现在是一个额外的参数,不会影响“slow”标志的状态。

英文:

Flags start with a -, that's just how they are defined.

When you use printrepeater 3 slow, "slow" is now an extra argument, and doesn't affect the state of the slow flag.

huangapple
  • 本文由 发表于 2015年7月1日 21:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/31163176.html
匿名

发表评论

匿名网友

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

确定