-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
57 lines (50 loc) · 1.16 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
//go:build ignore
// this is show case for some advanced use
//
// use Option.Validate to check if the user input is valid
//
// use Option.Formatter to pre-format the user input, before binding to the variable 'host'
//
// if there's more free-type argument need, check out another example 'any-type-action'
package main
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"github.com/hellflame/argparse"
)
func main() {
parser := argparse.NewParser("basic", "this is a basic program", nil)
path := parser.String("f", "file", &argparse.Option{
Validate: func(arg string) error {
if _, e := os.Stat(arg); e != nil {
return fmt.Errorf("unable to access '%s'", arg)
}
return nil
},
})
host := parser.String("u", "url", &argparse.Option{
Help: "give me some url to parse",
Formatter: func(arg string) (i interface{}, err error) {
u, err := url.ParseRequestURI(arg)
if err != nil {
return
}
i = u.Host
return
},
})
if e := parser.Parse(nil); e != nil {
fmt.Println(e.Error())
return
}
if *path != "" {
if read, e := ioutil.ReadFile(*path); e == nil {
fmt.Println(string(read))
}
}
if *host != "" {
fmt.Println(*host)
}
}