forked from Masterminds/go-in-practice
/
count_cli.go
58 lines (55 loc) · 1000 Bytes
/
count_cli.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
package main
import (
"fmt"
"github.com/codegangsta/cli"
"os"
)
func main() {
app := cli.NewApp()
app.Usage = "Count up or down."
app.Commands = []cli.Command{
{
Name: "up",
ShortName: "u",
Usage: "Count Up",
Flags: []cli.Flag{
cli.IntFlag{
Name: "stop, s",
Usage: "Value to count up to",
Value: 10,
},
},
Action: func(c *cli.Context) {
start := c.Int("stop")
if start <= 0 {
fmt.Println("Stop cannot be negative.")
}
for i := 1; i <= start; i++ {
fmt.Println(i)
}
},
},
{
Name: "down",
ShortName: "d",
Usage: "Count Down",
Flags: []cli.Flag{
cli.IntFlag{
Name: "start, s",
Usage: "Start counting down from",
Value: 10,
},
},
Action: func(c *cli.Context) {
start := c.Int("start")
if start < 0 {
fmt.Println("Start cannot be negative.")
}
for i := start; i >= 0; i-- {
fmt.Println(i)
}
},
},
}
app.Run(os.Args)
}