英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论