How to use nested subcommands
borud opened this issue · 1 comments
borud commented
Let's say I have multiple nested commands and I want to do things like
myprog user add -u foo
I made this structure
type UserCommand struct {
Add struct {
Username string `short:"u" long:"user" description:"Username"`
Fullname string `short:"n" long:"fullname" description:"Full name"`
Email string `short:"e" long:"email" description:"Email address"`
Phone string `short:"p" long:"phone" description:"Phone number"`
} `command:"add" description:"Add a new user"`
Get struct {
Username string `short:"u" long:"user" description:"Username"`
} `command:"get" description:"Get a user"`
Delete struct {
Username string `short:"u" long:"user" description:"Username"`
} `command:"delete" description:"Delete user"`
}
Before adding subcommands the user
command would call
func (r *UserCommand) Execute(args []string) error {...}
What gets called when I use subcommands? I am a bit confused.
eikendev commented
If you create a separate type for your subcommands, then everything should work fine.
Try something along the lines of this (not tested):
type AddCmd struct {
Username string `short:"u" long:"user" description:"Username"`
Fullname string `short:"n" long:"fullname" description:"Full name"`
Email string `short:"e" long:"email" description:"Email address"`
Phone string `short:"p" long:"phone" description:"Phone number"`
}
func (r *AddCmd) Execute(args []string) error {...}
type UserCommand struct {
Add AddCmd `command:"add" description:"Add a new user"`
}