-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
48 lines (40 loc) · 904 Bytes
/
example_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
package clino_test
import (
"context"
"flag"
"fmt"
"os"
"github.com/henvic/clino"
)
// RootCommand is the entrypoint of the application.
type RootCommand struct {
name string
}
// Name of the application.
func (rc *RootCommand) Name() string {
return "app"
}
// Long description of the application.
func (rc *RootCommand) Long() string {
return "Example application."
}
// Flags of the command.
func (rc *RootCommand) Flags(flags *flag.FlagSet) {
flags.StringVar(&rc.name, "name", "World", "your name")
}
// Run command.
func (rc *RootCommand) Run(ctx context.Context, args ...string) error {
fmt.Printf("Hello, %s!\n", rc.name)
return nil
}
func Example() {
p := clino.Program{
Root: &RootCommand{},
}
if err := p.Run(context.Background(), "-name", "Gopher"); err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(clino.ExitCode(err))
}
// Output:
// Hello, Gopher!
}