-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow subcommands declaration/addition in subdirectories (child packages) of cmd/ #1197
Comments
Line 495 in 8cfa4b4
Lines 1351 to 1367 in 8cfa4b4
Lines 1341 to 1344 in 8cfa4b4
So this package main
import (
"github.com/spf13/cobra"
)
func NewCmd() *cobra.Command {
cmd := &cobra.Command{Use: "app"}
cmd2 := &cobra.Command{Use: "add", Run: func(cmd *cobra.Command, args []string) {}}
cmd3 := &cobra.Command{Use: "delete"}
cmd4 := &cobra.Command{Use: "list"}
cmd.AddCommand(cmd2, cmd3, cmd4)
return cmd
}
func main() {
NewCmd().Execute()
}
But this package main
import (
"github.com/spf13/cobra"
)
func NewCmd() *cobra.Command {
cmd := &cobra.Command{Use: "app"}
cmd2 := &cobra.Command{Use: "add"}
cmd3 := &cobra.Command{Use: "delete"}
cmd4 := &cobra.Command{Use: "list"}
cmd.AddCommand(cmd2, cmd3, cmd4)
return cmd
}
func main() {
NewCmd().Execute()
}
|
Oh I see now, even though your example is not exactly the same directory structure. In general, having at least one of command in the chain of commands from the root commands with a Thanks a lot @bukowa |
Just a false positive, closing the issue now. |
Currently cobra does not fully supports having all the subcommands declared in the same package as the root (top level) command. On the contrary, full support is only available if you have the strict directory layout of declaring all the subcommands in the same package (same directory).
But having subcommands declared in subdirectories of cmd lends itself naturally to a cleaner code structure specially when the subcommands have subcommands and the nesting level is > 2, due to namespacing/encapsulation of the variables used as flags.
Currently cobra still "works" in the nested subdirectories (nested packages) case, but the
Usage
andAvailable Commands
sections of the root command are empty or not there at all.As a minimal example in this directory structure:
Where
root.go
:And bor.go (same code as bar.go, but with s/bor/bar')
When executing the binary with no arguments, instead of seeing something like this (using the recomended directory layout):
We get this:
which is less than ideal.
Is there any chance we can be a bit more flexible in the directory structure supported?
I noticed the closed issue #641 and in fact decide to follow the recommendation followed there in: #641 (comment) and in doing so I noticed the current state (cobra@1.0.0)
FYI the project mentioned in the suggestion (https://github.com/ohsu-comp-bio/funnel/) it is able to show the Usage and Available commands, but it uses cobra@0.0.5. 😢
The text was updated successfully, but these errors were encountered: