β app cobra add create
create created at /Users/fanux/work/src/app/cmd/create.go
β app cobra add user -p create
user created at /Users/fanux/work/src/app/cmd/user.go
β app tree
.
βββ LICENSE
βββ cmd
βΒ Β βββ create.go
βΒ Β βββ root.go
βΒ Β βββ user.go
βββ main.go
cobra generates subcommand user in the same dir of its parent dir, it not very nice.
If I add a command user this user command is not create subcommand:
cobra add user
Error: /Users/fanux/work/src/app/cmd/user.go already exists
So I think, create subcommand in a sub dir is better, like this:
.
βββ LICENSE
βββ cmd
βΒ Β βββ create.go
βΒ Β βββ root.go
| |---create
βΒ Β βββ user.go
βββ main.go
This will not conflict
Hi @fanux @jharshman , I am interested in this issue if that would be okay but I think I need to ask some clarification questions. Suppose that we are still discussing the example of creating a user subcommand under create command like the one mentioned above. Does it mean that:
user.go and other subcommands under create folder will be have a package name of create? e.g. package create on the top of the code.create command in the upper level package of cmd and user subcommand in the lower level package of create live in different packages means that we need to make the createCmd at the cmd/create.go to be exported (CreateCmd) so that we can import it at cmd/create/user.go to add the userCmd command under create command? import (
"github.com/joshuabezaleel/test-cobra/cmd"
)
var userCmd
func init() {
cmd.CreateCmd.AddCommand(userCmd)
}
Looking forward to hearing your thoughts! Thank you very much :slightly_smiling_face:
For exampleοΌmy app command line like this:
app user create xxx
app group create xxx
So I want generate codes:
cobra add user
cobra add group
cobra add create -p user
cobra add create -p group
If all create.go in cmd dir, it will failed, and mess...
A better way is cmd/user/create.go and cmd/group/create.go
Other example:
app create user
app delete user
cobra add create
cobra add delete
cobra add user -p create
cobra add user -p delete
For exampleοΌmy app command line like this:
app user create xxx app group create xxxSo I want generate codes:
cobra add user cobra add group cobra add create -p user cobra add create -p groupIf all create.go in cmd dir, it will failed, and mess...
A better way iscmd/user/create.goandcmd/group/create.go
@fanux
Yes I got this so far. But by having cmd/user/create.go it means that
create.go file package is using package user andcmd/user/create.go needs to import package cmd to add userCmd (cmd/user.go) so that we can use cmd.UserCmd.AddCommand(createCmd) or it works otherwise, wherecmd/user.go needs to import package cmd/user so that we can use userCmd.AddCommand(user.createCmd) because we need to register createCmd as the subcommand from userCmd right?So I think only subcommand rely on parent command, can we regist subcommand in cmd/user/create.go? Set userCmd as public. So in cmd/user/create.go we can regist subcommand like this: UserCmd.AddCommand(createCmd), cmd/user.go no need import any more.
I'm also interested in this capability. To model after the kubernetes style of verb noun for commands.
This issue is being marked as stale due to a long period of inactivity
I'm interested in this. In the meantime, I will try just creating separate files for each subcommand.
Most helpful comment
I'm also interested in this capability. To model after the kubernetes style of
verb nounfor commands.