-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
62 lines (55 loc) · 1.13 KB
/
main.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"flag"
"fmt"
"io"
"os"
)
func handleCmdA(w io.Writer, args []string) error {
var v string
fs := flag.NewFlagSet("cmd-a", flag.ContinueOnError)
fs.SetOutput(w)
fs.StringVar(&v, "verb", "argument-value", "Argument 1")
err := fs.Parse(args)
if err != nil {
return err
}
fmt.Fprintf(w, "Executing command A")
return nil
}
func handleCmdB(w io.Writer, args []string) error {
var v string
fs := flag.NewFlagSet("cmd-b", flag.ContinueOnError)
fs.SetOutput(w)
fs.StringVar(&v, "verb", "argument-value", "Argument 1")
err := fs.Parse(args)
if err != nil {
return err
}
fmt.Fprintf(w, "Executing command B")
return nil
}
func printUsage(w io.Writer) {
fmt.Fprintf(w, "Usage: %s [cmd-a|cmd-b] -h\n", os.Args[0])
handleCmdA(w, []string{"-h"})
handleCmdB(w, []string{"-h"})
}
func main() {
var err error
if len(os.Args) < 2 {
printUsage(os.Stdout)
os.Exit(1)
}
switch os.Args[1] {
case "cmd-a":
err = handleCmdA(os.Stdout, os.Args[2:])
case "cmd-b":
err = handleCmdB(os.Stdout, os.Args[2:])
default:
printUsage(os.Stdout)
}
if err != nil {
fmt.Fprintln(os.Stdout, err)
os.Exit(1)
}
}