cmd/option.go

83 lines
1.4 KiB
Go
Raw Normal View History

package cmd
2023-11-11 19:28:58 -05:00
import (
"go.fifitido.net/cmd/opts"
)
type Option func(*Command)
func WithShortDescription(s string) Option {
return func(c *Command) {
c.shortDescription = s
}
}
func WithLongDescription(s string) Option {
return func(c *Command) {
c.longDescription = s
}
}
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-10 15:11:50 -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-11 19:28:58 -05:00
func WithOption(f opts.Option) Option {
return func(c *Command) {
2023-11-11 19:28:58 -05:00
c.opts = append(c.opts, f)
}
}
2023-11-11 19:28:58 -05:00
func WithOptions(os ...opts.Option) Option {
return func(c *Command) {
2023-11-11 19:28:58 -05:00
c.opts = append(c.opts, os...)
}
}
func WithSubcommand(s *Command) Option {
return func(c *Command) {
c.subcommands.Add(c)
2023-11-10 13:17:01 -05:00
s.parent = c
}
}
2023-11-10 15:11:50 -05:00
func WithSubcommands(ss ...*Command) Option {
return func(c *Command) {
c.subcommands.Add(c)
for _, s := range ss {
s.parent = c
}
}
}
2023-11-10 13:17:01 -05:00
func WithParent(p *Command) Option {
return func(c *Command) {
2023-11-10 13:17:01 -05:00
c.parent = p
p.subcommands.Add(c)
}
}
2023-11-10 13:17:01 -05:00
func WithRunFunc(r func(args []string)) Option {
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)
}
}
}
}