Skip to content

Commit 51fa473

Browse files
committed
docs(README): talk about values package first
1 parent 1800d5f commit 51fa473

1 file changed

Lines changed: 50 additions & 50 deletions

File tree

README.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,56 @@
22

33
Flag is a collection of packages extending the usability of the standard Go `flag` package. It has zero dependencies and aims to provide a simple, Go-idiomatic framework for implementing complex command-line interfaces.
44

5+
## flag/values
6+
7+
Package `flag/values` provides generic implementations of the `flag.Value` interface with support for:
8+
9+
- **Generic values**: define flags for any type with custom parse/format functions
10+
- **Basic types**: all Go basic types (int, string, bool, float, ...)
11+
- **Standard library types**: support for time.time, net/url.URL, net/netip.Addr, net/mail.Address, ...
12+
- **Collections**: support for both repeated flags (lists) and delimited values (slices)
13+
14+
```go
15+
func main() {
16+
flag.Var(values.Basic[int](), "count", "number of items")
17+
flag.Var(values.BasicList[string](), "tag", "tags (can be specified multiple times)")
18+
flag.Var(values.BasicSlice[string](","), "regions", "comma-separated list of regions")
19+
flag.Var(values.Stringer(url.Parse), "endpoint", "API endpoint URL")
20+
flag.Parse()
21+
flag.VisitAll(func(f *flag.Flag) { fmt.Printf("%s: %v\n", f.Name, f.Value.(flag.Getter).Get()) })
22+
}
23+
```
24+
```
25+
$ go run . -count 12 -endpoint http://example.com -tag foo -tag bar -regions euw,eune
26+
count: 12
27+
endpoint: http://example.com
28+
regions: [euw eune]
29+
tag: [foo bar]
30+
```
31+
32+
Alternatively, the `Registerer` provides an interface analogous to `flag.FlagSet` simplifying registration for common types:
33+
34+
```go
35+
func main() {
36+
var (
37+
reg = values.FlagSetRegisterer(flag.CommandLine)
38+
count = reg.Int("count", 10, "number of items")
39+
email = reg.MailAddr("email", &mail.Address{}, "contact email")
40+
bind = reg.IPAddrPort("bind", netip.MustParseAddrPort("0.0.0.0:8080"), "binding address")
41+
)
42+
flag.Parse()
43+
fmt.Println("Count:", *count)
44+
fmt.Println("Email:", *email)
45+
fmt.Println("Bind:", *bind)
46+
}
47+
```
48+
```
49+
$ go run . -count 12 -bind 10.0.0.1:80 -email foo@example.com
50+
Count: 12
51+
Email: <foo@example.com>
52+
Bind: 10.0.0.1:80
53+
```
54+
555
## flag/cli
656

757
Package `flag/cli` provides a very simple interface for building command-lines applications with:
@@ -91,53 +141,3 @@ Options:
91141
-port int
92142
port to listen on (default 8080)
93143
```
94-
95-
## flag/values
96-
97-
Package `flag/values` provides generic implementations of the standard Go `flag.Value` interface with support for:
98-
99-
- **Generic values**: define flags for any type with custom parse/format functions
100-
- **Basic types**: all Go basic types (int, string, bool, float, ...)
101-
- **Standard library types**: support for time.time, net/url.URL, net/netip.Addr, net/mail.Address, ...
102-
- **Collections**: support for both repeated flags (lists) and delimited values (slices)
103-
104-
```go
105-
func main() {
106-
flag.Var(values.Basic[int](), "count", "number of items")
107-
flag.Var(values.BasicList[string](), "tag", "tags (can be specified multiple times)")
108-
flag.Var(values.BasicSlice[string](","), "regions", "comma-separated list of regions")
109-
flag.Var(values.Stringer(url.Parse), "endpoint", "API endpoint URL")
110-
flag.Parse()
111-
flag.VisitAll(func(f *flag.Flag) { fmt.Printf("%s: %v\n", f.Name, f.Value.(flag.Getter).Get()) })
112-
}
113-
```
114-
```
115-
$ go run . -count 12 -endpoint http://example.com -tag foo -tag bar -regions euw,eune
116-
count: 12
117-
endpoint: http://example.com
118-
regions: [euw eune]
119-
tag: [foo bar]
120-
```
121-
122-
Alternatively, the `Registerer` provides an interface analogous to `flag.FlagSet` simplifying registration for common types:
123-
124-
```go
125-
func main() {
126-
var (
127-
reg = values.FlagSetRegisterer(flag.CommandLine)
128-
count = reg.Int("count", 10, "number of items")
129-
email = reg.MailAddr("email", &mail.Address{}, "contact email")
130-
bind = reg.IPAddrPort("bind", netip.MustParseAddrPort("0.0.0.0:8080"), "binding address")
131-
)
132-
flag.Parse()
133-
fmt.Printf("Count: %d\n", *count)
134-
fmt.Printf("Email: %v\n", *email)
135-
fmt.Printf("Bind: %v\n", *bind)
136-
}
137-
```
138-
```
139-
$ go run . -count 12 -bind 10.0.0.1:80 -email foo@example.com
140-
Count: 12
141-
Email: <foo@example.com>
142-
Bind: 10.0.0.1:80
143-
```

0 commit comments

Comments
 (0)