Repository navigation
Expand file tree
/
Copy pathexample_exec_options_test.go
More file actions
167 lines (141 loc) · 3.81 KB
/
Copy pathexample_exec_options_test.go
File metadata and controls
167 lines (141 loc) · 3.81 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package cmder_test
import (
"context"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"errors"
"flag"
"fmt"
"hash"
"github.com/brandon1024/cmder"
"github.com/brandon1024/cmder/getopt"
)
func ExampleWithInterspersedArgs() {
args := []string{"string-1", "-a", "md5", "string-2", "-c10", "string-3"}
ops := []cmder.ExecuteOption{
cmder.WithArgs(args),
cmder.WithInterspersedArgs(),
}
if err := cmder.Execute(context.Background(), hasher, ops...); err != nil {
fmt.Printf("unexpected error occurred: %v", err)
}
// Output:
// 0559406fc9a7b5704464c303ebbba64c
}
func ExampleWithRelaxedFlagParsing() {
// note that shorthand '--al' is permitted for flag '--algo'
args := []string{"--al", "md5", "relaxed-parsing"}
ops := []cmder.ExecuteOption{
cmder.WithArgs(args),
cmder.WithRelaxedFlagParsing(),
}
if err := cmder.Execute(context.Background(), hasher, ops...); err != nil {
fmt.Printf("unexpected error occurred: %v", err)
}
// Output:
// 21db31e27ddc3aef918b031bd978fa78
}
func ExampleWithNamedTemplate() {
args := []string{"-h"}
// override 'section.header' from the default usage template
header := `
{{- define "section.header" -}}
{{- println "hash - hash text input" -}}
{{- println -}}
{{- end -}}
`
// custom template, later named 'section.footer'
footer := `
{{- println -}}
{{- println "This tool is freely licensed under a permissive MIT license." -}}
`
ops := []cmder.ExecuteOption{
cmder.WithArgs(args),
cmder.WithNamedTemplate("welcome", header),
cmder.WithNamedTemplate("section.examples",
`{{- printf "\nExamples:\n" }}{{- printf " hash string-1 -a md5 string-2 -c 10 string-3\n" -}}`),
cmder.WithNamedTemplate("section.footer", footer),
}
err := cmder.Execute(context.Background(), hasher, ops...)
if !errors.Is(err, cmder.ErrShowUsage) {
fmt.Printf("unexpected error occurred: %v", err)
}
// Output:
// hash - hash text input
//
// Usage:
// hash [<str>...] [<flags>...]
//
// Examples:
// hash string-1 -a md5 string-2 -c 10 string-3
//
// Flags:
// -a <string>, --algo=<string> (default md5)
// select hashing algorithm (md5, sha1, sha256)
//
// -h
// show command usage information
//
// --help
// show command help information
//
// -c <uint>, --rounds=<uint> (default 10)
// number of hashing rounds
//
// This tool is freely licensed under a permissive MIT license.
}
const HashDesc = `
'hash' demonstrates how cmder can be configured to parse args with interspersed args and flags. The command generates
and prints a hash of the concatenated command args.
`
const HashExamples = `
# with interspersed args
hash string-1 -a md5 string-2 -c 10 string-3
# without interspersed args
hash -a md5 -c 10 string-1 string-2 string-3
`
var (
hasher = &Hasher{
BaseCommand: cmder.BaseCommand{
CommandName: "hash",
CommandDocumentation: cmder.CommandDocumentation{
Usage: "hash [<str>...] [<flags>...]",
ShortHelp: "Simple demonstration of interspersed arg parsing.",
Help: HashDesc,
Examples: HashExamples,
},
},
algo: "sha256",
rounds: 1,
}
)
type Hasher struct {
cmder.BaseCommand
algo string
rounds uint
}
func (h *Hasher) InitializeFlags(fs *flag.FlagSet) {
fs.StringVar(&h.algo, "algo", h.algo, "select hashing algorithm (md5, sha1, sha256)")
fs.UintVar(&h.rounds, "rounds", h.rounds, "number of hashing rounds")
getopt.Alias(fs, "algo", "a")
getopt.Alias(fs, "rounds", "c")
}
func (h *Hasher) Run(ctx context.Context, args []string) error {
algos := map[string]hash.Hash{
"md5": md5.New(),
"sha1": sha1.New(),
"sha256": sha256.New(),
}
alg, ok := algos[h.algo]
if !ok {
return fmt.Errorf("no such algorithm: %s", h.algo)
}
for range h.rounds {
for _, s := range args {
alg.Write([]byte(s))
}
}
fmt.Printf("%x\n", alg.Sum(nil))
return nil
}