forked from akamensky/argparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argparse_examples_test.go
74 lines (68 loc) · 2.21 KB
/
argparse_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
70
71
72
73
74
package argparse
import "fmt"
func ExampleCommand_Help() {
parser := NewParser("parser", "")
parser.HelpFunc = func(c *Command, msg interface{}) string {
return fmt.Sprintf("Name: %s\n", c.GetName())
}
fmt.Println(parser.Help(nil))
// Output:
// Name: parser
}
func ExampleCommand_Help_subcommandDefaulting() {
parser := NewParser("parser", "")
parser.HelpFunc = func(c *Command, msg interface{}) string {
helpString := fmt.Sprintf("Name: %s\n", c.GetName())
for _, com := range c.GetCommands() {
// Calls parser.HelpFunc, because command.HelpFuncs are nil
helpString += com.Help(nil)
}
return helpString
}
parser.NewCommand("subcommand1", "")
parser.NewCommand("subcommand2", "")
fmt.Println(parser.Help(nil))
// Output:
// Name: parser
// Name: subcommand1
// Name: subcommand2
}
func ExampleCommand_Help_subcommandHelpFuncs() {
parser := NewParser("parser", "")
parser.HelpFunc = func(c *Command, msg interface{}) string {
helpString := fmt.Sprintf("Name: %s\n", c.GetName())
for _, com := range c.GetCommands() {
// Calls command.HelpFunc, because command.HelpFuncs are not nil
helpString += com.Help(nil)
}
return helpString
}
com1 := parser.NewCommand("subcommand1", "Test description")
com1.HelpFunc = func(c *Command, msg interface{}) string {
helpString := fmt.Sprintf("Name: %s, Description: %s\n", c.GetName(), c.GetDescription())
return helpString
}
com2 := parser.NewCommand("subcommand2", "")
com2.String("s", "string", &Options{Required: false})
com2.String("i", "integer", &Options{Required: true})
com2.HelpFunc = func(c *Command, msg interface{}) string {
helpString := fmt.Sprintf("Name: %s\n", c.GetName())
for _, arg := range c.GetArgs() {
helpString += fmt.Sprintf("\tLname: %s, Required: %t\n", arg.GetLname(), arg.GetOpts().Required)
}
return helpString
}
fmt.Print(parser.Help(nil))
fmt.Print(com1.Help(nil))
fmt.Print(com2.Help(nil))
// Output:
// Name: parser
// Name: subcommand1, Description: Test description
// Name: subcommand2
// Lname: string, Required: false
// Lname: integer, Required: true
// Name: subcommand1, Description: Test description
// Name: subcommand2
// Lname: string, Required: false
// Lname: integer, Required: true
}