2023-11-09 23:34:37 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import "go.fifitido.net/cmd/flags"
|
|
|
|
|
|
|
|
type Option func(*Command)
|
|
|
|
|
2023-11-10 00:49:44 -05:00
|
|
|
func WithShortDescription(s string) Option {
|
|
|
|
return func(c *Command) {
|
|
|
|
c.ShortDescription = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLongDescription(s string) Option {
|
|
|
|
return func(c *Command) {
|
|
|
|
c.LongDescription = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 23:34:37 -05:00
|
|
|
func WithArgument(name string, required bool) Option {
|
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.arguments = append(c.arguments, &Argument{name, required})
|
2023-11-09 23:34:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithArguments(args []*Argument) Option {
|
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.arguments = append(c.arguments, args...)
|
2023-11-09 23:34:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithFlag(f flags.Flag) Option {
|
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.flags = append(c.flags, f)
|
2023-11-09 23:34:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithFlags(fs []flags.Flag) Option {
|
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.flags = append(c.flags, fs...)
|
2023-11-09 23:34:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSubcommand(s *Command) Option {
|
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.subcommands = append(c.subcommands, s)
|
|
|
|
s.parent = c
|
2023-11-09 23:34:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSubcommands(ss []*Command) Option {
|
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.subcommands = append(c.subcommands, ss...)
|
2023-11-09 23:34:37 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 00:49:44 -05:00
|
|
|
|
2023-11-10 13:17:01 -05:00
|
|
|
func WithParent(p *Command) Option {
|
2023-11-10 00:49:44 -05:00
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.parent = p
|
|
|
|
p.subcommands = append(p.subcommands, c)
|
2023-11-10 00:49:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 13:17:01 -05:00
|
|
|
func WithRunFunc(r func(args []string)) Option {
|
2023-11-10 00:49:44 -05:00
|
|
|
return func(c *Command) {
|
2023-11-10 13:17:01 -05:00
|
|
|
c.run = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithRunErrFunc(r func(args []string) error) Option {
|
|
|
|
return func(c *Command) {
|
|
|
|
c.run = func(args []string) {
|
|
|
|
if err := r(args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 00:49:44 -05:00
|
|
|
}
|
|
|
|
}
|