Golang execute function using flags

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

Golang execute function using flags

问题

你好!根据你提供的代码,当你执行最后一个标志时,所有的函数都会被执行。这是因为在你的代码中,你没有对每个标志进行单独的处理,而是在主函数中使用了三个条件判断来执行相应的函数。

在你的代码中,当执行最后一个标志时,forkPtr变量的值为true,因此会执行fork(forkPtr)函数。同时,由于wordPtrnumbPtr的值并没有被修改,它们的初始值仍然有效,所以word(wordPtr)numb(numbPtr)函数也会被执行。

如果你只想执行特定的函数,你可以在主函数中使用if-else语句来判断执行哪个函数。例如,你可以修改代码如下:

func main() {
	flag.StringVar(&wordPtr, "word", "foo", "a string")
	flag.IntVar(&numbPtr, "numb", 42, "an int")
	flag.BoolVar(&forkPtr, "fork", false, "a bool")
	flag.Parse()

	if flag.Arg(0) == "-wordPtr" {
		word(wordPtr)
	} else if flag.Arg(0) == "-numbPtr" {
		numb(numbPtr)
	} else if flag.Arg(0) == "-forkPtr" {
		fork(forkPtr)
	}
}

这样修改后,只有与输入的标志匹配的函数会被执行。希望这可以帮助到你!如果还有其他问题,请随时提问。

英文:

How can I execute a specific function by entering a flag, for example, I have three flags:

	wordPtr
	numbPtr
	forkPtr

And when executing a single one, the function that calls is executed:

.\data -wordPtr Test

When executing that flag, only the word function is executed:

package main

import (
	"flag"
	"fmt"
)

var (
	wordPtr string
	numbPtr int
	forkPtr bool
)

func main() {

	flag.StringVar(&wordPtr, "word", "foo", "a string")
	flag.IntVar(&numbPtr, "numb", 42, "an int")
	flag.BoolVar(&forkPtr, "fork", false, "a bool")
	flag.Parse()

	if wordPtr != `` {
		word(wordPtr)
	}

	if numbPtr != 0 {
		numb(numbPtr)
	}

	if forkPtr {
		fork(forkPtr)
	}

}

func word(word string) {
	fmt.Println("word:", word)
}

func numb(numb int) {
	fmt.Println("numb:", numb)
}

func fork(fork bool) {
	fmt.Println("fork:", fork)
}

But, when I execute the last flag, all functions are executed

PS C:\golang> .\logdata.exe -fork
word: foo
numb: 42      
fork: true    
PS C:\golang> 

Do you know why?

答案1

得分: 1

你为wordnumb标志提供了默认值,这意味着如果你没有将它们作为命令行参数提供,它们将使用默认值。

flag.Parse()之后,你只测试wordPtr不为空或numbPtr不为0,由于默认值不是这些值,测试将通过并执行函数。

如果你将空字符串和0作为默认值:

flag.StringVar(&wordPtr, "word", "", "a string")
flag.IntVar(&numbPtr, "numb", 0, "an int")

那么如果你没有将它们作为命令行参数提供,测试将不会通过:

.\logdata.exe -fork

将输出:

fork: true

如果你想测试是否提供了wordnumb作为命令行参数,请参考https://stackoverflow.com/questions/35809252/check-if-flag-was-provided-in-go。

英文:

You provide default values for the word and numb flags, which means if you don't provide them as cli arguments, they will get the default value.

And after flag.Parse() you only test if wordPtr is not empty or numbPtr is not 0, and since the default values are not these, the tests will pass and the functions will be executed.

If you use the empty string and 0 as the defaults:

flag.StringVar(&wordPtr, "word", "", "a string")
flag.IntVar(&numbPtr, "numb", 0, "an int")

Then if you don't provide these as cli arguments, your tests will not pass:

.\logdata.exe -fork

Will output:

fork: true

If you want to test if word or numb was provided as cli arguments, see https://stackoverflow.com/questions/35809252/check-if-flag-was-provided-in-go.

答案2

得分: 0

根据https://pkg.go.dev/flag#StringVar

StringVar定义了一个具有指定名称、默认值和用法说明的字符串标志。参数p指向一个字符串变量,用于存储标志的值。

所以在你的情况下,"foo"将是wordPtr的默认值,42将是numbPtr的默认值,false将是forkPtr的默认值。

英文:

According to https://pkg.go.dev/flag#StringVar

> StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.

So it means in your case "foo" - will be default value for wordPtr, 42 for numbPtr and false for forkPtr.

huangapple
  • 本文由 发表于 2022年1月26日 04:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/70855432.html
匿名

发表评论

匿名网友

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

确定