Skip to content

Commit 6d6eda8

Browse files
committed
change to v2, mainly due to myflagsv2
1 parent c87c782 commit 6d6eda8

File tree

7 files changed

+156
-110
lines changed

7 files changed

+156
-110
lines changed

Readme.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ include support following field types:
2020

2121
Additional types could be supported by using `Register`, see [github.com/hujun-open/shouchantypes](https://github.com/hujun-open/shouchantypes) for example.
2222

23+
## curent release is `github.com/hujun-open/shouchan/v2`
24+
2325
## CLI & YAML Support
2426

2527
- YAML: shouchan uses [extyaml](https://pkg.go.dev/github.com/hujun-open/extyaml) for YAML marshal and unmarshal
@@ -35,30 +37,27 @@ Output:
3537

3638
- Usage
3739
```
38-
.\test.exe -?
39-
flag provided but not defined: -?
40+
.\test.exe --help
4041
shouchan example
41-
- addr: employee address
42-
default:defAddrPointer
43-
- employer-name: company name
44-
default:defCom
45-
- ipaddr: employee IP address
46-
default:1.2.3.4
47-
- jointtime: employee join time
48-
default:2023-01-02 13:22:33
49-
- mac: employee MAC address
50-
default:11:22:33:44:55:66
51-
- n2addr:
52-
default:1.1.1.1
53-
- naddr:
54-
default:2.2.2.2
55-
- name: employee name
56-
default:defName
57-
- subnet: employee IP subnet
58-
default:192.168.1.0/24
59-
60-
-cfgfromfile: load configuration from the specified file
61-
default:test.yaml
42+
43+
Usage:
44+
example [flags]
45+
46+
Flags:
47+
--addr string employee address (default "defAddrPointer")
48+
--cfgfromfile string config file path (default "test.yaml")
49+
--employer-name string company name (default "defCom")
50+
-h, --help help for example
51+
--ipaddr net.IP employee IP address (default 1.2.3.4)
52+
--jointtime time.Time employee join time (default 2023-01-02 13:22:33)
53+
--mac net.HardwareAddr employee MAC address (default 11:22:33:44:55:66)
54+
--n2addr (default 1.1.1.1)
55+
--naddr (default 2.2.2.2)
56+
--name string employee name (default "defName")
57+
--r retired
58+
--subnet net.IPNet employee IP subnet (default 192.168.1.0/24)
59+
ferr failed to open config file test.yaml, open test.yaml: The system cannot find the file specified.,aerr <nil>
60+
final result is &{Name:defName Addr:0xc000028ee0 Naddr:2.2.2.2 N2addr:1.1.1.1 IPAddr:1.2.3.4 Subnet:{IP:192.168.1.0 Mask:ffffff00} MAC:11:22:33:44:55:66 JointTime:2023-01-02 13:22:33 +0000 UTC IsRetired:false Employer:{Name:defCom}}
6261
```
6362

6463
- no command line args, no config file, default is used
@@ -70,19 +69,19 @@ final result is &{Name:defName Addr:0xc0000528b0 Naddr:2.2.2.2 N2addr:1.1.1.1 IP
7069

7170
- config file via "-f" command args, value from file take procedence
7271
```
73-
.\test.exe -cfgfromfile cfg.yaml
72+
.\test.exe --cfgfromfile cfg.yaml
7473
ferr <nil>,aerr <nil>
7574
final result is &{Name:nameFromFile Addr:0xc0000528b0 Naddr:2.2.2.2 N2addr:1.1.1.1 IPAddr:1.2.3.4 Subnet:{IP:192.168.1.0 Mask:ffffff00} MAC:11:22:33:44:55:66 JointTime:2023-01-02 13:22:33 +0000 UTC Employer:{Name:comFromFile}}
7675
```
7776
- mix command line args and config file, args to override employee name:
7877
```
79-
.\test.exe -cfgfromfile cfg.yaml -name nameFromArg
78+
.\test.exe --cfgfromfile cfg.yaml --name nameFromArg
8079
ferr <nil>,aerr <nil>
8180
final result is &{Name:nameFromArg Addr:0xc000088880 Naddr:2.2.2.2 N2addr:1.1.1.1 IPAddr:1.2.3.4 Subnet:{IP:192.168.1.0 Mask:ffffff00} MAC:11:22:33:44:55:66 JointTime:2023-01-02 13:22:33 +0000 UTC Employer:{Name:comFromFile}}
8281
```
8382
- mix command line args and config file, args to override company name:
8483
```
85-
.\test.exe -cfgfromfile cfg.yaml -employer-name comFromArg
84+
.\test.exe --cfgfromfile cfg.yaml --employer-name comFromArg
8685
ferr <nil>,aerr <nil>
8786
final result is &{Name:nameFromFile Addr:0xc000104880 Naddr:2.2.2.2 N2addr:1.1.1.1 IPAddr:1.2.3.4 Subnet:{IP:192.168.1.0 Mask:ffffff00} MAC:11:22:33:44:55:66 JointTime:2023-01-02 13:22:33 +0000 UTC Employer:{Name:comFromArg}}
8887
```

example/go.mod

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ module test
33
go 1.20
44

55
require (
6-
github.com/hujun-open/shouchan v0.2.0
7-
github.com/hujun-open/shouchantypes v0.0.0-00010101000000-000000000000
6+
github.com/hujun-open/shouchan/v2 v2.0.0
7+
github.com/hujun-open/shouchantypes/v2 v2.0.0-00010101000000-000000000000
88
)
99

1010
require (
11-
github.com/hujun-open/extyaml v0.4.0 // indirect
12-
github.com/hujun-open/myflags v0.3.1 // indirect
11+
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
12+
github.com/hujun-open/cobra v0.1.0 // indirect
13+
github.com/hujun-open/extyaml v0.5.3 // indirect
14+
github.com/hujun-open/myflags/v2 v2.0.1 // indirect
15+
github.com/hujun-open/pflag v0.2.0 // indirect
16+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
17+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1318
gopkg.in/yaml.v3 v3.0.1 // indirect
1419
)
1520

16-
replace github.com/hujun-open/shouchan => ../
21+
replace github.com/hujun-open/shouchan/v2 => ../
1722

18-
replace github.com/hujun-open/shouchantypes => ../../shouchantypes
23+
replace github.com/hujun-open/shouchantypes/v2 => ../../shouchantypes

example/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"net/netip"
77
"time"
88

9-
"github.com/hujun-open/shouchan"
10-
_ "github.com/hujun-open/shouchantypes"
9+
"github.com/hujun-open/myflags/v2"
10+
"github.com/hujun-open/shouchan/v2"
11+
_ "github.com/hujun-open/shouchantypes/v2"
1112
)
1213

1314
type Company struct {
@@ -48,10 +49,14 @@ func main() {
4849
def.Subnet = *prefix
4950
def.JointTime, _ = time.Parse(time.DateTime, "2023-01-02 13:22:33")
5051
cnf, err := shouchan.NewSConf(&def, "example", "shouchan example",
51-
shouchan.WithDefaultConfigFilePath[*Employee]("test.yaml"))
52+
shouchan.WithDefaultConfigFilePath[Employee]("test.yaml"),
53+
shouchan.WithFillOptions[Employee]([]myflags.FillerOption{
54+
myflags.WithRootMethod(myflags.DefRunMethod)}),
55+
)
5256
if err != nil {
5357
panic(err)
5458
}
59+
5560
ferr, aerr := cnf.ReadwithCMDLine()
5661
fmt.Printf("ferr %v,aerr %v\n", ferr, aerr)
5762
fmt.Printf("final result is %+v\n", cnf.GetConf())

go.mod

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
module github.com/hujun-open/shouchan
1+
module github.com/hujun-open/shouchan/v2
22

33
go 1.20
44

55
require (
6-
github.com/hujun-open/extyaml v0.4.0
7-
github.com/hujun-open/myflags v0.3.2
6+
github.com/hujun-open/extyaml v0.5.3
7+
github.com/hujun-open/myflags/v2 v2.0.1
8+
github.com/hujun-open/pflag v0.2.0
89
)
910

10-
require gopkg.in/yaml.v3 v3.0.1 // indirect
11+
require (
12+
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
13+
github.com/hujun-open/cobra v0.1.0 // indirect
14+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
15+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
16+
gopkg.in/yaml.v3 v3.0.1 // indirect
17+
)

register.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55

66
"github.com/hujun-open/extyaml"
7-
"github.com/hujun-open/myflags"
7+
"github.com/hujun-open/myflags/v2"
88
)
99

1010
// FromStr is the function convert a string into a instance of to-be-supported-type

0 commit comments

Comments
 (0)