func (n *CobraNamespace) Namespace(cmd *cobra.Command) error {
	if !cmd.HasParent() {
		return errors.New("cmdns: command requires a parent")
	}
	if !cmd.HasAvailableSubCommands() {
		return nil
	}
	if n.OverrideUsageFunc {
		cmd.SetUsageFunc(n.UsageFunc())
	}
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() {
			continue
		}
		nc := *c
		nc.Use = cmd.Name() + DefaultNamespaceSeparator + c.Use
		c.Parent().Parent().AddCommand(&nc)
		c.Hidden = true
		n.commands = append(n.commands, &nc)
	}
	n.cmd = cmd
	return nil
}
Namespace enables namespacing for a sub-commmand and its immediated children. It returns an error if the command does not have a parent.
package cmdns
import (
	"errors"
	"fmt"
	"github.com/spf13/cobra"
)
var (
	DefaultNamespacer = New()
	DefaultNamespaceSeparator = ":"
)
func SetOverrideUsageFunc(v bool) *CobraNamespacer {
	return DefaultNamespacer.SetOverrideUsageFunc(v)
}
func Namespace(cmd *cobra.Command) error {
	return DefaultNamespacer.Namespace(cmd)
}
type CobraNamespacer struct {
	Namespaces        []*CobraNamespace
	OverrideUsageFunc bool
}
func New() *CobraNamespacer {
	return &CobraNamespacer{
		Namespaces:        make([]*CobraNamespace, 0),
		OverrideUsageFunc: true,
	}
}
func (c *CobraNamespacer) SetOverrideUsageFunc(v bool) *CobraNamespacer {
	c.OverrideUsageFunc = v
	return c
}
func (c *CobraNamespacer) Namespace(cmd *cobra.Command) error {
	if cmd == nil {
		return errors.New("cmdns: cmd cannot be nil")
	}
	for _, child := range cmd.Commands() {
		n := NewCobraNamespace()
		n.OverrideUsageFunc = c.OverrideUsageFunc
		c.Namespaces = append(c.Namespaces, n)
		if err := n.Namespace(child); err != nil {
			return err
		}
	}
	return nil
}
type CobraNamespace struct {
	OverrideUsageFunc bool
	cmd      *cobra.Command
	commands []*cobra.Command
}
func NewCobraNamespace() *CobraNamespace {
	return &CobraNamespace{
		OverrideUsageFunc: true,
		commands:          make([]*cobra.Command, 0),
	}
}
func (n *CobraNamespace) AvailableCommands() []*cobra.Command {
	return n.commands
}
func (n *CobraNamespace) Command() *cobra.Command {
	return n.cmd
}