-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_slice_test.go
More file actions
49 lines (40 loc) · 1.46 KB
/
example_slice_test.go
File metadata and controls
49 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package flagr_test
import (
"errors"
"fmt"
"net/url"
"os"
"strconv"
"github.com/flga/flagr"
)
func Example_slices() {
var set flagr.Set
// We can use flagr.Slice to create repeatable flags. Custom slice types are supported.
type bools []bool
a := flagr.Add[bools](&set, "a", flagr.Slice(bools{true, false, true}, strconv.ParseBool), "usage")
// Making use of MustSlice so that we can pass default values as strings and delegate the parsing to flagr.
// Makes it much more comfortable to use things that have to be parsed, like urls.
b := flagr.Add[[]*url.URL](&set, "b", flagr.MustSlice([]string{"https://a.com", "https://b.com"}, url.Parse), "usage")
// Using helper functions allows us to have cleaner signatures (type inference is still a little wonky)
c := flagr.Add(&set, "c", Urls("https://a.com", "https://b.com"), "usage")
args := []string{
"-a", "-a", // std/flag bool semantics apply
"-b", "https://c.com", "-b", "https://d.com",
}
if err := set.Parse(args); err != nil {
if errors.Is(err, flagr.ErrHelp) {
os.Exit(0)
}
os.Exit(2)
}
fmt.Printf("a = %v and is a %T\n", *a, a)
fmt.Printf("b = %v and is a %T\n", *b, b)
fmt.Printf("c = %v and is a %T\n", *c, c)
// Output:
// a = [true true] and is a *flagr_test.bools
// b = [https://c.com https://d.com] and is a *[]*url.URL
// c = [https://a.com https://b.com] and is a *[]*url.URL
}
func Urls(defaults ...string) flagr.Getter[[]*url.URL] {
return flagr.MustSlice(defaults, url.Parse)
}