@@ -6,29 +6,33 @@ to rewrite a same one using [ecp](https://github.com/wrfly/ecp).
6
6
If you want to convert a golang structure into some command line flags,
7
7
just ` guá ` .
8
8
9
+ It's small and useful for some simple command line tools.
10
+
9
11
## Example
10
12
11
13
``` golang
12
14
package main
13
15
14
16
import (
15
- " flag "
17
+ " encoding/json "
16
18
" fmt"
17
19
" time"
18
20
19
21
" github.com/wrfly/gua"
20
22
)
21
23
22
24
type cliFlags struct {
23
- Name string ` name:"name " default:"wrfly" desc:"just a name"`
24
- Age int
25
+ Name string ` name:"nnnnname " default:"wrfly" desc:"just a name"`
26
+ Age int ` desc:"the age" `
25
27
Slice []string ` desc:"test string slice"`
26
- SliceInt []int ` desc:"test int slice"`
28
+ SliceInt []int ` desc:"test int slice" default:"1 2 3" `
27
29
Time time.Duration ` desc:"test time duration"`
28
30
Extra struct {
29
31
Loc string ` default:"home" desc:"location"`
30
32
Valid bool ` default:"true"`
33
+ Debug bool
31
34
}
35
+ Type string ` desc:"A|B|C"`
32
36
}
33
37
34
38
func main () {
@@ -37,31 +41,70 @@ func main() {
37
41
panic (err)
38
42
}
39
43
40
- fmt.Printf (" %+v \n " , *cli)
44
+ bs , _ := json.MarshalIndent (cli, " " , " " )
45
+ fmt.Printf (" %s \n " , bs)
41
46
}
42
47
```
43
48
44
- ``` txt
45
- ➜ /tmp/example -h
46
- Usage of /tmp/example:
47
- -cliFlags.Age
48
- -cliFlags.Extra.Loc location [home]
49
- -cliFlags.Extra.Valid [true]
50
- -cliFlags.Slice test string slice
51
- -cliFlags.SliceInt test int slice
52
- -cliFlags.Time test time duration
53
- -name just a name [wrfly]
49
+ ``` bash
50
+ # run the example
51
+ ./gua
52
+ {
53
+ " Name" : " wrfly" ,
54
+ " Age" : 0,
55
+ " Slice" : null,
56
+ " SliceInt" : [
57
+ 1,
58
+ 2,
59
+ 3
60
+ ],
61
+ " Time" : 0,
62
+ " Extra" : {
63
+ " Loc" : " home" ,
64
+ " Valid" : false,
65
+ " Debug" : false
66
+ },
67
+ " Type" : " "
68
+ }
69
+
54
70
55
- ➜ /tmp/example
56
- {Name:wrfly Age:0 Slice:[] SliceInt:[] Time:0s Extra:{Loc:home Valid:true}}
71
+ # show some help message
72
+ ./gua -h
73
+ Usage of ./gua:
74
+ -age the age
75
+ -extra.debug [false]
76
+ -extra.loc location [home]
77
+ -extra.valid [false]
78
+ -nnnnname just a name [wrfly]
79
+ -slice test string slice
80
+ -sliceInt test int slice [1 2 3]
81
+ -time test time duration
82
+ -type A| B| C
57
83
58
- ➜ /tmp/example \
59
- -name frog \
60
- -cliFlags.Age 3 \
61
- -cliFlags.Extra.Loc pool \
62
- -cliFlags.Extra.Valid true \
63
- -cliFlags.Slice "a b c" \
64
- -cliFlags.SliceInt "1 2 3" \
65
- -cliFlags.Time "365d"
66
- {Name:frog Age:3 Slice:[a b c] SliceInt:[1 2 3] Time:8760h0m0s Extra:{Loc:pool Valid:true}}
84
+
85
+ # add some flags
86
+ ./gua -age 18 -extra.debug -nnnnname gua \
87
+ -slice " hello world" -sliceInt " 1 3 5 7" \
88
+ -time 1m -type C
89
+ {
90
+ " Name" : " gua" ,
91
+ " Age" : 18,
92
+ " Slice" : [
93
+ " hello" ,
94
+ " world"
95
+ ],
96
+ " SliceInt" : [
97
+ 1,
98
+ 3,
99
+ 5,
100
+ 7
101
+ ],
102
+ " Time" : 60000000000,
103
+ " Extra" : {
104
+ " Loc" : " home" ,
105
+ " Valid" : false,
106
+ " Debug" : true
107
+ },
108
+ " Type" : " C"
109
+ }
67
110
```
0 commit comments