-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_options.go
More file actions
93 lines (77 loc) · 2.76 KB
/
Copy pathcommand_options.go
File metadata and controls
93 lines (77 loc) · 2.76 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
package cli
import (
"sort"
"github.com/broothie/option"
)
const versionFlagName = "version"
// SetVersion sets the version of the command.
func SetVersion(version string) option.Func[*Command] {
return func(command *Command) (*Command, error) {
command.version = version
return command, nil
}
}
// AddAlias adds an alias to the command.
func AddAlias(alias string) option.Func[*Command] {
return func(command *Command) (*Command, error) {
command.aliases = append(command.aliases, alias)
return command, nil
}
}
// SetHandler sets the handler of the command.
func SetHandler(handler Handler) option.Func[*Command] {
return func(command *Command) (*Command, error) {
command.handler = handler
return command, nil
}
}
// AddSubCmd adds a subcommand to the command.
func AddSubCmd(name, description string, options ...option.Option[*Command]) option.Func[*Command] {
return func(command *Command) (*Command, error) {
subCommand, err := NewCommand(name, description, options...)
if err != nil {
return nil, err
}
return MountSubCmd(subCommand).Apply(command)
}
}
// MountSubCmd mounds an existing *Command as a sub-command.
func MountSubCmd(subCommand *Command) option.Func[*Command] {
return func(command *Command) (*Command, error) {
subCommand.parent = command
command.subCommands = append(command.subCommands, subCommand)
return command, nil
}
}
// AddFlag adds a flag to the command.
func AddFlag(name, description string, options ...option.Option[*Flag]) option.Func[*Command] {
return func(command *Command) (*Command, error) {
flag, err := newFlag(name, description, options...)
if err != nil {
return nil, err
}
command.flags = append(command.flags, flag)
return command, nil
}
}
// AddArg adds an argument to the command.
func AddArg(name, description string, options ...option.Option[*Argument]) option.Func[*Command] {
return func(command *Command) (*Command, error) {
argument, err := newArgument(name, description, options...)
if err != nil {
return nil, err
}
command.arguments = append(command.arguments, argument)
sort.SliceStable(command.arguments, func(i, j int) bool { return command.arguments[i].isRequired() && command.arguments[j].isOptional() })
return command, nil
}
}
// AddHelpFlag adds a help flag to the command.
func AddHelpFlag(options ...option.Option[*Flag]) option.Func[*Command] {
defaultOptions := option.NewOptions(setFlagIsHelp(true), SetFlagDefault(false))
return AddFlag(helpFlagName, "Print help.", append(defaultOptions, options...)...)
}
func AddVersionFlag(options ...option.Option[*Flag]) option.Func[*Command] {
defaultOptions := option.NewOptions(setFlagIsVersion(true), SetFlagDefault(false))
return AddFlag(versionFlagName, "Print version.", append(defaultOptions, options...)...)
}