cmd/option.go

83 lines
1.4 KiB
Go
Raw Permalink Normal View History

package cmd
2023-11-11 19:28:58 -05:00
import (
2023-11-12 17:19:34 -05:00
"go.fifitido.net/cmd/opt"
2023-11-11 19:28:58 -05:00
)
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) {
c.Args = append(c.Args, &Argument{name, required})
}
}
2023-11-10 15:11:50 -05:00
func WithArguments(args ...*Argument) Option {
return func(c *Command) {
c.Args = append(c.Args, args...)
}
}
2023-11-12 17:19:34 -05:00
func WithOption(f opt.Option) Option {
return func(c *Command) {
c.Opts = append(c.Opts, f)
}
}
2023-11-12 17:19:34 -05:00
func WithOptions(os ...opt.Option) Option {
return func(c *Command) {
c.Opts = append(c.Opts, os...)
}
}
func WithSubcommand(s *Command) Option {
return func(c *Command) {
c.Subcommands.Add(s)
s.Parent = c
}
}
2023-11-10 15:11:50 -05:00
func WithSubcommands(ss ...*Command) Option {
return func(c *Command) {
for _, s := range ss {
c.Subcommands.Add(s)
s.Parent = c
}
}
}
2023-11-10 13:17:01 -05:00
func WithParent(p *Command) Option {
return func(c *Command) {
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)
}
}
}
}