-
Notifications
You must be signed in to change notification settings - Fork 0
/
provide_examples_test.go
69 lines (56 loc) · 1.26 KB
/
provide_examples_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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package clifx
import (
"fmt"
"github.com/alecthomas/kong"
"go.uber.org/fx"
)
// ExampleProvide_basic shows how to bootstrap a basic command line using clifx.
func ExampleProvide_basic() {
var cli *CommandLine
var kctx *kong.Context // this is completely optional
fx.New(
fx.NopLogger,
// if we don't supply any arguments, clifx will use os.Args[1:]
fx.Supply(
Arguments{
"-f", "/path/to/configfile.yml",
},
),
Provide(
SuppressExit(), // in case of an error, prevent this example from calling os.Exit
),
fx.Populate(
&cli,
&kctx,
),
)
fmt.Println(cli.ConfigFile)
fmt.Println(kctx.Args)
// Output:
// /path/to/configfile.yml
// [-f /path/to/configfile.yml]
}
// ExampleProvide_custom shows how to use a custom command line object.
func ExampleProvide_custom() {
type CustomCommandLine struct {
TurnSomethingOn bool `name:"on" optional:"" help:"this is just an example"`
}
var cli *CustomCommandLine
fx.New(
fx.NopLogger,
SupplyArguments(
"--on",
),
ProvideCustom[CustomCommandLine](
SuppressExit(),
),
fx.Populate(
&cli,
),
)
fmt.Println(cli.TurnSomethingOn)
// Output:
// true
}