-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
50 lines (45 loc) · 1.05 KB
/
example_test.go
File metadata and controls
50 lines (45 loc) · 1.05 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
package cmdflag_test
import (
"flag"
"fmt"
"github.com/pierrec/cmdflag"
)
func ExampleCommand_Add() {
// Declare the `split` cmdflag.
c := cmdflag.New(flag.CommandLine)
_, _ = c.Add(
cmdflag.Application{
Name: "split",
Descr: "splits files into fixed size chunks",
Args: "[sep ...]",
Help: `split can split multiple files into chunks
e.g. split -size 1M file1 file2
will generate files of 1M or maybe less for the last one, as follow:
file1_0
file1_1
...
file1_x
file2_00
file2_01
...
file2_yy`,
Err: flag.ExitOnError,
Init: func(fs *flag.FlagSet) cmdflag.Handler {
// Declare the cmdflag specific flags.
var s string
fs.StringVar(&s, "s", "", "string to be split")
// Return the handler to be executed when the cmdflag is found.
return func(sep ...string) (int, error) {
i := len(s) / 2
fmt.Printf("%s %v %s", s[:i], sep, s[i:])
return 1, nil
}
},
})
// ./program split -s hello & @
if err := c.Parse("split", "-s", "hello", "&", "@"); err != nil {
panic(err)
}
// Output:
// he [& @] llo
}