Go实例: Command-Line Flags

Ianthe ·
更新时间:2024-11-10
· 956 次阅读

package main import ( "flag" "fmt" ) func main() { wordPtr := flag.String("word", "foo", "a string") numbPtr := flag.Int("numb", 42, "an int") forkPtr := flag.Bool("fork", false, "a bool") var svar string flag.StringVar(&svar, "svar", "bar", "a string var") flag.Parse() fmt.Println("word:", *wordPtr) fmt.Println("numb:", *numbPtr) fmt.Println("fork:", *forkPtr) fmt.Println("svar:", svar) fmt.Println("tail:", flag.Args()) }$ go build command-line-flags.go Try out the built program by first giving it values for all flags. $ ./command-line-flags -word=opt -numb=7 -fork -svar=flag word: opt numb: 7 fork: true svar: flag tail: [] Note that if you omit flags they automatically take their default values. $ ./command-line-flags -word=opt word: opt numb: 42 fork: false svar: bar tail: [] Trailing positional arguments can be provided after any flags. $ ./command-line-flags -word=opt a1 a2 a3 word: opt ... tail: [a1 a2 a3] Note that the flag package requires all flags to appear before positional arguments (otherwise the flags will be interpreted as positional arguments). $ ./command-line-flags -word=opt a1 a2 a3 -numb=7 word: opt numb: 42 fork: false svar: bar tail: [a1 a2 a3 -numb=7] Use -h or --help flags to get automatically generated help text for the command-line program. $ ./command-line-flags -h Usage of ./command-line-flags: -fork=false: a bool -numb=42: an int -svar="bar": a string var -word="foo": a string If you provide a flag that wasn’t specified to the flag package, the program will print an error message and show the help text again. $ ./command-line-flags -wat flag provided but not defined: -wat Usage of ./command-line-flags: ...



GO实例 flags GO line

需要 登录 后方可回复, 如果你还没有账号请 注册新账号