-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
48 lines (41 loc) · 945 Bytes
/
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
//go:build ignore
// this is show case for argument action
//
// argument action will be executed when user input has a match to the binding argument
package main
import (
"fmt"
"io/ioutil"
"strconv"
"github.com/hellflame/argparse"
)
// run like: go run main.go 2 3 4 --b main.go
func main() {
parser := argparse.NewParser("basic", "this is a basic program", nil)
sum := 0
content := ""
parser.Strings("", "a", &argparse.Option{Positional: true, Action: func(args []string) error {
for _, arg := range args {
i, e := strconv.Atoi(arg)
if e != nil {
return e
}
sum += i
}
return nil
}})
parser.String("", "b", &argparse.Option{Action: func(args []string) error {
raw, e := ioutil.ReadFile(args[0])
if e != nil {
return e
}
content = string(raw)
return nil
}})
if e := parser.Parse(nil); e != nil {
fmt.Println(e.Error())
return
}
fmt.Printf("totals: %d\n", sum)
fmt.Println(content)
}