Skip to content

Commit 633a6cc

Browse files
committed
chore: upate example code
1 parent e1f0ad8 commit 633a6cc

File tree

2 files changed

+70
-27
lines changed

2 files changed

+70
-27
lines changed

README.md

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,33 @@ to rewrite a same one using [ecp](https://github.com/wrfly/ecp).
66
If you want to convert a golang structure into some command line flags,
77
just `guá`.
88

9+
It's small and useful for some simple command line tools.
10+
911
## Example
1012

1113
```golang
1214
package main
1315

1416
import (
15-
"flag"
17+
"encoding/json"
1618
"fmt"
1719
"time"
1820

1921
"github.com/wrfly/gua"
2022
)
2123

2224
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"`
2527
Slice []string `desc:"test string slice"`
26-
SliceInt []int `desc:"test int slice"`
28+
SliceInt []int `desc:"test int slice" default:"1 2 3"`
2729
Time time.Duration `desc:"test time duration"`
2830
Extra struct {
2931
Loc string `default:"home" desc:"location"`
3032
Valid bool `default:"true"`
33+
Debug bool
3134
}
35+
Type string `desc:"A|B|C"`
3236
}
3337

3438
func main() {
@@ -37,31 +41,70 @@ func main() {
3741
panic(err)
3842
}
3943

40-
fmt.Printf("%+v\n", *cli)
44+
bs, _ := json.MarshalIndent(cli, "", " ")
45+
fmt.Printf("%s\n", bs)
4146
}
4247
```
4348

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+
5470

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
5783

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+
}
67110
```

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
type cliFlags struct {
12-
Name string `name:"name" default:"wrfly" desc:"just a name"`
12+
Name string `name:"nnnnname" default:"wrfly" desc:"just a name"`
1313
Age int `desc:"the age"`
1414
Slice []string `desc:"test string slice"`
1515
SliceInt []int `desc:"test int slice" default:"1 2 3"`

0 commit comments

Comments
 (0)