This article Subject: line parsing os.Args and flag realization Golang-based commands.
Small slow brother of the original article, welcome to reprint
table of Contents
▪ a. Os.Args
▪ two. Flag
▪ three. Os.Args binding achieved with the flag subcommand
▪ attachment. Reference Document
A. Os.Args
os.Args used to get command line parameters passed
▷ os.Args [0] : Execution Path
▷ os.Args [. 1] : The first parameter
▷ os.Args [2] : The second parameter
▷ len (os.Args) number of parameters:
Examples are as follows
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("Program:", os.Args[0])
for i, v := range os.Args[1:] {
fmt.Printf("Arg[%d]: %v\n", i, v)
}
}
Test results
# 编译执行
go build arg.go
./arg foo "hello world" bar
# 输出
Program: ./arg
Arg[0]: foo
Arg[1]: hello world
Arg[2]: bar
Two. Flag
golang flag module built-in command line flag can achieve resolution.
What is the flag, illustration (bold part is the flag):
▷ wc -l
▷ ./configure --prefix=/usr/local/nginx
Examples are as follows (removal from gobyexample)
package main
import (
"flag"
"fmt"
)
func main() {
// flag.String返回的是指针
// word表示参数名,foo表示默认值,a string表示参数释义(在-h或解析异常时候会看到)
// flag.Int、flag.Bool与flag.String同理,不再赘述
wordPtr := flag.String("word", "foo", "a string")
numbPtr := flag.Int("numb", 42, "an int")
boolPtr := flag.Bool("fork", false, "a bool")
// flag.StringVar返回的是非指针
var svar string
flag.StringVar(&svar, "svar", "bar", "a string var")
// 进行flag解析
flag.Parse()
fmt.Println("word:", *wordPtr)
fmt.Println("numb:", *numbPtr)
fmt.Println("fork:", *boolPtr)
fmt.Println("svar:", svar)
fmt.Println("tail:", flag.Args())
}
Test results
# 编译
go build flag.go
./flag -word=opt -numb=7 -fork -svar=flag
# 输出
word: opt
numb: 7
fork: true
svar: flag
tail: []
./flag -word=opt
# 输出
word: opt
numb: 42
fork: false
svar: bar
tail: []
./flag -word=opt a1 a2 a3
# 输出
word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3]
./flag -word=opt a1 a2 a3 -numb=7
# 输出
word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3 -numb=7]
./flag -h
# 输出
Usage of ./flag:
-fork
a bool
-numb int
an int (default 42)
-svar string
a string var (default "bar")
-word string
a string (default "foo")
./flag -wat
# 输出
flag provided but not defined: -wat
Usage of ./flag:
...
This example, only when the test "single horizontal line parameter name +", actually there are four ways, the effect is the same
-word opt
-word=opt
--word opt
--word=opt
III. Os.Args combined with the realization of the sub-command flag
If you want to achieve sub-command effects, such as:
▷ docker ps --last 10
▷ docker images --help
This requires os.Args and flag combination. Rationale: Different create multiple flag collection (ie * flag.FlagSet), each flag corresponding set os.Args
Examples are as follows (removal from gobyexample)
package main
import (
"flag"
"fmt"
"os"
)
func main() {
fooCmd := flag.NewFlagSet("foo", flag.ExitOnError)
fooEnable := fooCmd.Bool("enable", false, "enable")
fooName := fooCmd.String("name", "", "name")
barCmd := flag.NewFlagSet("bar", flag.ExitOnError)
barLevel := barCmd.Int("level", 0, "level")
if len(os.Args) < 2 {
fmt.Println("expected 'foo' or 'bar' subcommands")
os.Exit(1)
}
switch os.Args[1] {
case "foo":
fooCmd.Parse(os.Args[2:])
fmt.Println("subcommand 'foo'")
fmt.Println(" enable:", *fooEnable)
fmt.Println(" name:", *fooName)
fmt.Println(" tail:", fooCmd.Args())
case "bar":
barCmd.Parse(os.Args[2:])
fmt.Println("subcommand 'bar'")
fmt.Println(" level:", *barLevel)
fmt.Println(" tail:", barCmd.Args())
default:
fmt.Println("expected 'foo' or 'bar' subcommands")
os.Exit(1)
}
}
Test results
# 编译
go build subflag.go
./subflag foo -enable -name=joe a1 a2
# 输出
subcommand 'foo'
enable: true
name: joe
tail: [a1 a2]
./subflag bar -level 8 a1
# 输出
subcommand 'bar'
level: 8
tail: [a1]
./subflag bar -enable a1
# 输出
flag provided but not defined: -enable
Usage of bar:
-level int
level
Attached Reference Documents
# os.Args
https://gobyexample.com/command-line-arguments
# flag
https://gobyexample.com/command-line-flags
# 子命令
https://gobyexample.com/command-line-subcommands