Skip to content

Commit eb31506

Browse files
committed
- change cobra back to spf13/cobra
- update example - update README.md
1 parent abdd9d2 commit eb31506

File tree

10 files changed

+35
-26
lines changed

10 files changed

+35
-26
lines changed

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ the created flags:
3131
```
3232
.\cptool summaryhelp
3333
= cptool [flags]
34-
--backupaddrlist: backup server address list
34+
-b, --backupaddrlist: backup server address list
35+
default:1.1.1.1,2.2.2.2
3536
-c, --configfile: working profile
3637
default:default.conf
38+
-f, --floatlist: list of numbers
39+
default:1.7,2.2,3.3
40+
--strlist: list of string
41+
default:a,bb,ccc
3742
--svraddr: server address to download the archive
3843
default:<nil>
3944
= cptool compress
@@ -87,12 +92,12 @@ the created flags:
8792
default:false
8893
```
8994
some parsing results:
90-
```
91-
.\cptool --svraddr 1.1.1.1 --backupaddrlist 2.2.2.2,2001:dead::1 compress -l 3 zipfile input1 out.zip
92-
zipfile &{ConfigFile:default.conf SvrAddr:1.1.1.1 BackupAddrList:[2.2.2.2 2001:dead::1] Arg1: Compress:{Loop:3 Profile: Skip:false NoFlag: DryRun:{} ZipFolder:{FolderName: ArchiveName: CreationTime:2025-01-02 03:04:05 +0000 UTC} ZipFile:{FileName:input1 ArchiveName:out.zip}} Extract:{InputFile: OutputFolder:}}
95+
```
96+
./cptool --svraddr 1.1.1.1 --backupaddrlist 2.2.2.2,2001:dead::1 compress -l 3 zipfile input1 out.zip
97+
zipfile &{ConfigFile:default.conf SvrAddr:1.1.1.1 BackupAddrList:[2.2.2.2 2001:dead::1] FloatList:[1.7 2.2 3.3] StrList:[a bb ccc] Compress:{Loop:3 Profile: Skip:false NoFlag: DryRun:{} ZipFolder:{FolderName: ArchiveName: CreationTime:2025-01-02 03:04:05 +0000 UTC} ZipFile:{FileName:input1 ArchiveName:out.zip}} Extract:{InputFile: OutputFolder:}}
9398
9499
.\cptool compress zipfolder folder1 out.zip "2030 01 Jun 13:01" -l 99
95-
zipfolder &{ConfigFile:default.conf SvrAddr:<nil> BackupAddrList:[] Arg1: Compress:{Loop:99 Profile: Skip:false NoFlag: DryRun:{} ZipFolder:{FolderName:folder1 ArchiveName:out.zip CreationTime:2030-06-01 13:01:00 +0000 UTC} ZipFile:{FileName:defaultzip.file ArchiveName:}} Extract:{InputFile: OutputFolder:}}
100+
zipfolder &{ConfigFile:default.conf SvrAddr:<nil> BackupAddrList:[1.1.1.1 2.2.2.2] FloatList:[1.7 2.2 3.3] StrList:[a bb ccc] Compress:{Loop:99 Profile: Skip:false NoFlag: DryRun:{} ZipFolder:{FolderName:folder1 ArchiveName:out.zip CreationTime:2030-06-01 13:01:00 +0000 UTC} ZipFile:{FileName:defaultzip.file ArchiveName:}} Extract:{InputFile: OutputFolder:}}
96101
```
97102

98103

@@ -117,32 +122,36 @@ Base:
117122
- string
118123
- bool
119124

120-
provided by `github.com/hujun-open/myflags/v2/types`:
125+
following are provided by `github.com/hujun-open/myflags/v2/types`:
121126

122127
- all int/uint types: support `base` tag for the base
123128
- net.HardwareAddr
124129
- net.IPNet
125130
- net.IP
126131
- time.Time
127132
- time.Duration
128-
- all int/uint types
129133

130134
Others:
131135
- All types implement both of following interface:
132-
- `encoding.TextUnmarshaler`
136+
- `encoding.TextMarshaler`
133137
- `encoding.TextUnmarshaler`
134138
- All type register via `myflags.Register` function
135139

136140
Note: flag is only created for exported struct field.
137141

142+
### Slice / Array
138143

139144
In addition to above types, following types are also supported:
140145

141146
- pointer to the type above
142147
- slice/array of type above
143148
- slice/array of pointer to the type above
144149

145-
for slice/array, use "," as separator.
150+
Use "," as separator of items in the input string
151+
152+
note: if the item type is struct, then it must be either registered type or implements `encoding.TextMarshaler`/`encoding.TextUnmarshaler`
153+
154+
### Nested / Embeded struct
146155

147156
myflags also supports following type of struct:
148157

docgen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"log"
66
"strconv"
77

8-
"github.com/hujun-open/cobra"
9-
"github.com/hujun-open/cobra/doc"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/cobra/doc"
1010
)
1111

1212
type genDoc struct {

example/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ module cptool
33
go 1.20
44

55
require (
6-
github.com/hujun-open/cobra v0.3.0
76
github.com/hujun-open/myflags/v2 v2.0.1
7+
github.com/spf13/cobra v1.10.1
88

99
)
1010

1111
require (
1212
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
1313
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1414
github.com/russross/blackfriday/v2 v2.1.0 // indirect
15-
github.com/spf13/pflag v1.0.7 // indirect
15+
github.com/spf13/pflag v1.0.9 // indirect
1616
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
1717
gopkg.in/yaml.v3 v3.0.1 // indirect
1818
)

example/main.go

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

9-
"github.com/hujun-open/cobra"
109
"github.com/hujun-open/myflags/v2"
1110
_ "github.com/hujun-open/myflags/v2/types"
11+
"github.com/spf13/cobra"
1212
)
1313

1414
type ZipCLI struct {
1515
ConfigFile string `short:"c" usage:"working profile"`
1616
SvrAddr net.IP `usage:"server address to download the archive" complete:"SvrComplete"` //using completer method SvrComplete for shell completion
1717
BackupAddrList []netip.Addr `short:"b" usage:"backup server address list"`
18-
IntList []float32 `short:"i" usage:"list of numbers"`
18+
FloatList []float32 `short:"f" usage:"list of numbers"`
1919
StrList []string `usage:"list of string"`
2020
Compress struct {
2121
Loop uint `base:"16" short:"l" usage:"number of compress iterations"`
@@ -79,7 +79,7 @@ func main() {
7979
}
8080
zipcli.BackupAddrList = []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("2.2.2.2")}
8181
zipcli.Compress.Loop = 0x20
82-
zipcli.IntList = []float32{1.7, 2.2, 3.3}
82+
zipcli.FloatList = []float32{1.7, 2.2, 3.3}
8383
zipcli.Compress.ZipFile.FileName = "defaultzip.file"
8484
zipcli.Compress.ZipFolder.CreationTime = time.Date(2025, 1, 2, 3, 4, 5, 0, time.UTC)
8585

flagvalidvalues.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package myflags
33
import (
44
"strings"
55

6-
"github.com/hujun-open/cobra"
6+
"github.com/spf13/cobra"
77
)
88

99
// validFlagValues is a helper struct used to support ValidValuesTag

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/hujun-open/myflags/v2
33
go 1.20
44

55
require (
6-
github.com/hujun-open/cobra v0.3.0
7-
github.com/spf13/pflag v1.0.7
6+
github.com/spf13/cobra v1.10.1
7+
github.com/spf13/pflag v1.0.9
88
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b
99
)
1010

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
22
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
3-
github.com/hujun-open/cobra v0.3.0 h1:S40jvWTCocUzG+dExWP8lOSfPaYdWacw1QOhmdlMNvM=
4-
github.com/hujun-open/cobra v0.3.0/go.mod h1:Bfy9ZzyA06VIvmbowRPx0O6dcvx8jv6iVfRVBq5EUAE=
53
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
64
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
75
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
86
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9-
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
10-
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
7+
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
8+
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
9+
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
10+
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1111
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
1212
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
1313
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

myflags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
flag "github.com/spf13/pflag"
1515
"golang.org/x/exp/slices"
1616

17-
"github.com/hujun-open/cobra"
17+
"github.com/spf13/cobra"
1818
)
1919

2020
// encodingTextMarshaler is the interface includes both encoding.TextMarshaler and encoding.TextUnmarshaler

myflags_test.go

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

12-
"github.com/hujun-open/cobra"
12+
"github.com/spf13/cobra"
1313
flag "github.com/spf13/pflag"
1414

1515
"github.com/hujun-open/myflags/v2"

noun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"text/tabwriter"
1010
"unicode"
1111

12-
"github.com/hujun-open/cobra"
12+
"github.com/spf13/cobra"
1313
"golang.org/x/exp/slices"
1414
)
1515

0 commit comments

Comments
 (0)