Command-Line Subcommands

Command-Line Subcommands

原文:https://gobyexample.com/command-line-subcommands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Note:
// This code is from https://gobyexample.com.
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())
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
PS D:\Dev\Go\byExample\command_line_subcommands> ./main.exe foo -enable -name=joe a1 a2
subcommand 'foo'
  enable: true
  name: joe
  tail: [a1 a2]
  
PS D:\Dev\Go\byExample\command_line_subcommands> ./main.exe bar -level 8 a1          
subcommand 'bar'
  level: 8
  tail: [a1]
  
PS D:\Dev\Go\byExample\command_line_subcommands> ./main.exe bar -enable a1 
flag provided but not defined: -enable
Usage of bar:
  -level int
        level
最后修改 October 10, 2024: 更新 (a4b8f85)