Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
103 lines (93 loc) · 2.74 KB
/
Copy pathexample_test.go
File metadata and controls
103 lines (93 loc) · 2.74 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
95
96
97
98
99
100
101
102
103
// This example demonstrates a simple "Hello, World!" CLI program.
package climux
import (
"context"
"fmt"
"strings"
)
// Each flag holds its own value, and State returns the half of it the
// handler reads.
var (
flagNoNewLines = Bool("n", "Do not print the trailing newline character").State()
// String flag to select a desired language. Can be specified with
// -l, --language or the HW_LANG environment variable.
flagLanguage = String("language", "Language (en, es, it or nl)").
Default("en").
Aliases("l").
Env("HW_LANG").
State()
// StringSlice flag to optionally print multiple positional
// arguments. Positional arguments are not denoted with "-" or "--".
flagMessage = Strings("MESSAGE", "Optional message to print").
Positional().
State()
)
var translations = map[string]string{
"en": "Hello, World!",
"es": "Hola, Mundo!",
"it": "Ciao, Mondo!",
"nl": "Hallo, Wereld!",
}
var App = NewCommand("helloworld", "Print \"Hello, World!\"").
// By convention the flag that prints help is declared first, so it
// heads the list of options; nothing requires it.
HelpFlag().
Description(
"The helloworld utility writes \"Hello, World!\" to the standard\n"+
" output multiple languages.",
).
Flags(flagNoNewLines, flagLanguage, flagMessage).
HandleFunc(helloWorld)
// helloWorld is the HandlerFunc for the main App command.
func helloWorld(ctx context.Context, inv *Invocation) error {
s, ok := translations[flagLanguage.Value()]
if !ok {
return fmt.Errorf("unsupported language: %s", flagLanguage.Value())
}
if message := flagMessage.Value(); len(message) > 0 {
s = strings.Join(message, " ")
}
fmt.Fprint(inv.Stdout, s)
if !flagNoNewLines.Value() {
fmt.Fprint(inv.Stdout, "\n")
}
return nil
}
func Example() {
ctx := context.Background()
fmt.Println("+ helloworld --help")
Run(ctx, App, WithArgs("--help"))
// Most programs will call the following from main:
//
// func main() {
// ctx, stop := climux.NotifyContext(context.Background())
// defer stop()
// os.Exit(climux.Run(ctx, App))
// }
//
fmt.Println()
fmt.Println("+ helloworld --language=es")
Run(ctx, App, WithArgs("--language=es"))
// Output:
// + helloworld --help
// Usage: helloworld [OPTIONS] [MESSAGE...]
//
// Print "Hello, World!"
//
// Positional arguments:
// MESSAGE Optional message to print
//
// Options:
// -h, --help Show this help message and exit
// -n Do not print the trailing newline character
// -l, --language Language (en, es, it or nl)
//
// Environment variables:
// HW_LANG Language (en, es, it or nl)
//
// The helloworld utility writes "Hello, World!" to the standard
// output multiple languages.
//
// + helloworld --language=es
// Hola, Mundo!
}