cmd/option.go

83 lines
1.4 KiB
Go

package cmd
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) {
c.arguments = append(c.arguments, &Argument{name, required})
}
}
func WithArguments(args ...*Argument) Option {
return func(c *Command) {
c.arguments = append(c.arguments, args...)
}
}
func WithOption(f opts.Option) Option {
return func(c *Command) {
c.opts = append(c.opts, f)
}
}
func WithOptions(os ...opts.Option) Option {
return func(c *Command) {
c.opts = append(c.opts, os...)
}
}
func WithSubcommand(s *Command) Option {
return func(c *Command) {
c.subcommands.Add(c)
s.parent = c
}
}
func WithSubcommands(ss ...*Command) Option {
return func(c *Command) {
c.subcommands.Add(c)
for _, s := range ss {
s.parent = c
}
}
}
func WithParent(p *Command) Option {
return func(c *Command) {
c.parent = p
p.subcommands.Add(c)
}
}
func WithRunFunc(r func(args []string)) Option {
return func(c *Command) {
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)
}
}
}
}