-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_exec_option_bind_env_test.go
More file actions
94 lines (79 loc) · 2.21 KB
/
example_exec_option_bind_env_test.go
File metadata and controls
94 lines (79 loc) · 2.21 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cmder_test
import (
"context"
"flag"
"fmt"
"os"
"github.com/brandon1024/cmder"
)
func ExampleWithEnvironmentBinding() {
_ = os.Setenv("BINDENV_SHOW_FORMAT", "overidden-by-flag")
_ = os.Setenv("BINDENV_SHOW_PAGECOUNT", "20")
args := []string{"show", "--format=pretty"}
ops := []cmder.ExecuteOption{
cmder.WithArgs(args),
cmder.WithEnvironmentBinding(),
}
if err := cmder.Execute(context.Background(), GetCommand(), ops...); err != nil {
fmt.Printf("unexpected error occurred: %v", err)
}
// Output:
// format: pretty
// page-count: 20
}
const BindEnvHelpText = `
'bind-env' desmonstrates how cmder can be configured to bind environment variables to flags. Explicit command-line
arguments always take precedence over environment variables.
`
const BindEnvExamples = `
# print all default flag values
bind-env show
> default 10
# print flag values from environment
BINDENV_SHOW_FORMAT=pretty BINDENV_SHOW_PAGECOUNT=20 bind-env show --page-count=15
> pretty 15
`
func GetCommand() *cmder.BaseCommand {
return &cmder.BaseCommand{
CommandName: "bind-env",
CommandDocumentation: cmder.CommandDocumentation{
Usage: "bind-env [subcommand] [flags]",
ShortHelp: "Simple demonstration of binding environment variables to command flags.",
Help: BindEnvHelpText,
Examples: BindEnvExamples,
},
Children: []cmder.Command{GetShowCommand()},
}
}
func GetShowCommand() *cmder.BaseCommand {
return &cmder.BaseCommand{
CommandName: "show",
CommandDocumentation: cmder.CommandDocumentation{
Usage: `show [flags]`,
ShortHelp: `Show flag values`,
Help: `'show' dumps flag values to stdout.`,
Examples: BindEnvExamples,
},
InitFlagsFunc: showFlags,
RunFunc: show,
}
}
var (
format string = "default"
count uint = 10
)
func showFlags(fs *flag.FlagSet) {
fs.StringVar(&format, "format", format, "output format (default, pretty)")
fs.UintVar(&count, "page-count", count, "number of pages")
}
func show(ctx context.Context, args []string) error {
switch format {
case "default":
fmt.Printf("%v %v\n", format, count)
case "pretty":
fmt.Printf("format: %v\npage-count: %v\n", format, count)
default:
return fmt.Errorf("illegal format: %s", format)
}
return nil
}