-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_new_test.go
78 lines (71 loc) · 1.59 KB
/
example_new_test.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package warg_test
import (
"fmt"
"os"
"go.bbkane.com/warg"
"go.bbkane.com/warg/command"
"go.bbkane.com/warg/flag"
"go.bbkane.com/warg/section"
"go.bbkane.com/warg/value/scalar"
)
func login(ctx command.Context) error {
url := ctx.Flags["--url"].(string)
// timeout doesn't have a default value,
// so we can't rely on it being passed.
timeout, exists := ctx.Flags["--timeout"]
if exists {
timeout := timeout.(int)
fmt.Printf("Logging into %s with timeout %d\n", url, timeout)
return nil
}
fmt.Printf("Logging into %s\n", url)
return nil
}
func ExampleNew() {
app := warg.New(
"newAppName",
section.New(
"work with a fictional blog platform",
section.Command(
"login",
"Login to the platform",
login,
),
section.Flag(
"--timeout",
"Optional timeout. Defaults to no timeout",
scalar.Int(),
),
section.Flag(
"--url",
"URL of the blog",
scalar.String(
scalar.Default("https://www.myblog.com"),
),
flag.EnvVars("BLOG_URL"),
),
section.Section(
"comments",
"Deal with comments",
section.Command(
"list",
"List all comments",
// still prototyping how we want this
// command to look,
// so use a provided stub action
command.DoNothing,
),
),
),
)
// normally we would rely on the user to set the environment variable,
// bu this is an example
err := os.Setenv("BLOG_URL", "https://envvar.com")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
app.MustRun(warg.OverrideArgs([]string{"blog.exe", "login"}))
// Output:
// Logging into https://envvar.com
}