-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathexample06.go
77 lines (60 loc) · 1.45 KB
/
example06.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"flag"
"fmt"
"os"
"strings"
)
func flagUsage() {
usageText := `example06 is an example cli tool.
Usage:
example06 command [arguments]
The commands are:
uppercase uppercase a string
lowercase lowercase a string
Use "example06 [command] --help" for more information about a command.`
fmt.Fprintf(os.Stderr, "%s\n\n", usageText)
}
func uppercaseCmdUsage() {
usageText := `usage: uppercase [-s]
-s
A string to be uppercased`
fmt.Fprintf(os.Stderr, "%s\n\n", usageText)
}
func lowercaseCmdUsage() {
usageText := `usage: lowercase [-s]
-s
A string to be lowercased`
fmt.Fprintf(os.Stderr, "%s\n\n", usageText)
}
func main() {
flag.Usage = flagUsage
uppercaseCmd := flag.NewFlagSet("uppercase", flag.ExitOnError)
uppercaseCmd.Usage = uppercaseCmdUsage
lowercaseCmd := flag.NewFlagSet("lowercase", flag.ExitOnError)
lowercaseCmd.Usage = lowercaseCmdUsage
if len(os.Args) == 1 {
flag.Usage()
return
}
switch os.Args[1] {
case "uppercase":
s := uppercaseCmd.String("s", "", "A string of text to be uppercased")
uppercaseCmd.Parse(os.Args[2:])
if *s == "" {
uppercaseCmd.Usage()
return
}
fmt.Println(strings.ToUpper(*s))
case "lowercase":
s := lowercaseCmd.String("s", "", "A string of text to be lowercased")
lowercaseCmd.Parse(os.Args[2:])
if *s == "" {
lowercaseCmd.Usage()
return
}
fmt.Println(strings.ToLower(*s))
default:
flag.Usage()
}
}