Golang Command.SetUsageFunc Examples

Golang Command.SetUsageFunc - 5 examples found. These are the top rated real world Golang examples of github.com/spf13/cobra.Command.SetUsageFunc extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func UseOptionsTemplates(cmd *cobra.Command) {
	cmd.SetHelpTemplate(OptionsHelpTemplate())
	templater := &templater{
		UsageTemplate: OptionsUsageTemplate(),
	}
	cmd.SetUsageFunc(templater.UsageFunc())
}
Example #2
0
// 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
}
Example #3
0
func ActsAsRootCommand(cmd *cobra.Command, groups ...CommandGroup) FlagExposer {
	if cmd == nil {
		panic("nil root command")
	}
	cmd.SetHelpTemplate(MainHelpTemplate())
	templater := &templater{
		RootCmd:       cmd,
		UsageTemplate: MainUsageTemplate(),
		CommandGroups: groups,
	}
	cmd.SetUsageFunc(templater.UsageFunc())
	return templater
}
Example #4
0
func (templater *templater) ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer {
	cmd.SetUsageFunc(templater.UsageFunc(flags...))
	return templater
}
Example #5
0
// Enable enables monocle on a cobra command
func (m *Monocle) Enable(c *cobra.Command) {
	m.Command = c
	c.SetUsageFunc(m.UsageFunc())
}