cmd/option.go

83 lines
1.4 KiB
Go

package cmd
import (
"go.fifitido.net/cmd/opt"
)
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})
}
}
func WithArguments(args ...*Argument) Option {
return func(c *Command) {
c.Args = append(c.Args, args...)
}
}
func WithOption(f opt.Option) Option {
return func(c *Command) {
c.Opts = append(c.Opts, f)
}
}
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
}
}
func WithSubcommands(ss ...*Command) Option {
return func(c *Command) {
for _, s := range ss {
c.Subcommands.Add(s)
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)
}
}
}
}