// Namespace enables namespacing for a sub-commmand and its immediated children. It returns an error if the command does not have a parent. func (n *CobraNamespace) Namespace(cmd *cobra.Command) error { if !cmd.HasParent() { return errors.New("cmdns: command requires a parent") } // Do not bind if there are not available sub commands if !cmd.HasAvailableSubCommands() { return nil } if n.OverrideUsageFunc { cmd.SetUsageFunc(n.UsageFunc()) } for _, c := range cmd.Commands() { if !c.IsAvailableCommand() { continue } // copy the command add it to the root command with a prefix of its parent. nc := *c nc.Use = cmd.Name() + DefaultNamespaceSeparator + c.Use // add this command to the root and hide it so it does not show in available commands list c.Parent().Parent().AddCommand(&nc) c.Hidden = true n.commands = append(n.commands, &nc) } n.cmd = cmd return nil }