golang / urfave.cli:无法手动设置标志位

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

golang / urfave.cli: can't get to set flags manually

问题

我们有一个应用程序,在一个函数中接收一个urfave cli上下文对象:

import "gopkg.in/urfave/cli.v1"

func runApp(ctx *cli.Context) {
   cache := ctx.GlobalInt(MyFlag.Name)
   .... // 做更多的事情
}

我试图在没有整个cli的情况下直接从代码中调用runApp。因此,我必须手动构建ctx

func MyTest() {
  fs := flag.NewFlagSet("", flag.ExitOnError)
  app := cli.NewApp()
  fs.Set(MyFlag.Name, "5000")
  ctx := cli.NewContext(app, fs, nil)
  runApp(ctx)
}

但实际上看起来标志从未设置过,runApp从未看到我手动设置的值。

我做错了什么?这样做真的可以吗?

英文:

We have an app which receives an urfave cli context object in a func:

import "gopkg.in/urfave/cli.v1"

func runApp(ctx *cli.Context) {
   cache := ctx.GlobalInt(MyFlag.Name)
   .... // do more stuff
}

I am trying to call runApp without the whole cli stuff, directly from code. Hence I have to build the ctx manually:


func MyTest() {
  fs := flag.NewFlagSet("", flag.ExitOnError)
  app := cli.NewApp()
  fs.Set(MyFlag.Name, "5000")
  ctx := cli.NewContext(app, fs, nil)
  runApp(ctx)
}

But it actually looks like the flag is never set, runApp never sees the value I set by hand.

What am I doing wrong? Can this actually be done at all?

答案1

得分: 2

看起来MyFlag.Name这个字段实际上并没有被定义为一个flag。
要将其定义为flag,你需要这样做:fs.String(MyFlag.Name, "5000", "usageOfTheFlag")
之后你可以使用fs.Set来设置MyFlag.Name的新值。

var MyFlag = flag.Flag{
	Name: "TestFlag",
}

func MyTest() {
	fs := flag.NewFlagSet("", flag.ExitOnError)
	app := cli.NewApp()
	// 定义一个带有默认值的flag
	fs.String(MyFlag.Name, "5000", "usageOfTheFlag")
	// 设置一个新值
	err := fs.Set(MyFlag.Name, "5000")

	if err != nil {
		fmt.Println(err)
		return
	}

	ctx := cli.NewContext(app, fs, nil)
	runApp(ctx)
}
func runApp(ctx *cli.Context) {
	cache := ctx.GlobalInt(MyFlag.Name)
	.... // 做更多的事情
}

如果你在没有将flag定义为flag的情况下对fs.Set进行错误检查:

// fs.String(MyFlag.Name, "5000", "usageOfTheFlag")
err := fs.Set(MyFlag.Name, "5000")

if err != nil {
	fmt.Println(err)
	return
}

你将会得到一个错误,其中包含当前未定义为flag的flag的名称。
在这个例子中,它是"TestFlag"

no such flag -TestFlag

因此,MyFlag.Name没有获得新值,因此你的程序没有看到你的新值。

英文:

It looks like MyFlag.Name this field is not actually defined as a flag.
To define it as a flag, you need do this: fs.String(MyFlag.Name, "5000", "usageOfTheFlag")
After this you can use fs.Set for MyFlag.Name.

var MyFlag = flag.Flag{
	Name: "TestFlag",
}

func MyTest() {
  fs := flag.NewFlagSet("", flag.ExitOnError)
  app := cli.NewApp()
  // define a flag with default value
  fs.String(MyFlag.Name, "5000", "usageOfTheFlag")
  // set a new value
  err := fs.Set(MyFlag.Name, "5000")

  if err != nil {
     fmt.Println(err)
     return
  }

  ctx := cli.NewContext(app, fs, nil)
  runApp(ctx)
}
func runApp(ctx *cli.Context) {
   cache := ctx.GlobalInt(MyFlag.Name)
   .... // do more stuff
}

If you would do an error checking on fs.Set with your flag name without define it as a flag:

// fs.String(MyFlag.Name, "5000", "usageOfTheFlag")
err := fs.Set(MyFlag.Name, "5000")

	if err != nil {
		fmt.Println(err)
		return
	}

You would get this error with the name of the flag that is currently not defined as a flag.
In this example it is "TestFlag":

no such flag -TestFlag

Thus MyFlag.Name didn't get the new value and because of it your program didn't see your new value.

huangapple
  • 本文由 发表于 2023年5月29日 05:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353647.html
匿名

发表评论

匿名网友

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

确定