MCPcopy
hub / github.com/spf13/cobra / defaultUsageFunc

Function defaultUsageFunc

command.go:1974–2040  ·  view source on GitHub ↗

defaultUsageFunc is equivalent to executing defaultUsageTemplate. The two should be changed in sync.

(w io.Writer, in interface{})

Source from the content-addressed store, hash-verified

1972
1973// defaultUsageFunc is equivalent to executing defaultUsageTemplate. The two should be changed in sync.
1974func defaultUsageFunc(w io.Writer, in interface{}) error {
1975 c := in.(*Command)
1976 fmt.Fprint(w, "Usage:")
1977 if c.Runnable() {
1978 fmt.Fprintf(w, "\n %s", c.UseLine())
1979 }
1980 if c.HasAvailableSubCommands() {
1981 fmt.Fprintf(w, "\n %s [command]", c.CommandPath())
1982 }
1983 if len(c.Aliases) > 0 {
1984 fmt.Fprintf(w, "\n\nAliases:\n")
1985 fmt.Fprintf(w, " %s", c.NameAndAliases())
1986 }
1987 if c.HasExample() {
1988 fmt.Fprintf(w, "\n\nExamples:\n")
1989 fmt.Fprintf(w, "%s", c.Example)
1990 }
1991 if c.HasAvailableSubCommands() {
1992 cmds := c.Commands()
1993 if len(c.Groups()) == 0 {
1994 fmt.Fprintf(w, "\n\nAvailable Commands:")
1995 for _, subcmd := range cmds {
1996 if subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName {
1997 fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
1998 }
1999 }
2000 } else {
2001 for _, group := range c.Groups() {
2002 fmt.Fprintf(w, "\n\n%s", group.Title)
2003 for _, subcmd := range cmds {
2004 if subcmd.GroupID == group.ID && (subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName) {
2005 fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
2006 }
2007 }
2008 }
2009 if !c.AllChildCommandsHaveGroup() {
2010 fmt.Fprintf(w, "\n\nAdditional Commands:")
2011 for _, subcmd := range cmds {
2012 if subcmd.GroupID == "" && (subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName) {
2013 fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
2014 }
2015 }
2016 }
2017 }
2018 }
2019 if c.HasAvailableLocalFlags() {
2020 fmt.Fprintf(w, "\n\nFlags:\n")
2021 fmt.Fprint(w, trimRightSpace(c.LocalFlags().FlagUsages()))
2022 }
2023 if c.HasAvailableInheritedFlags() {
2024 fmt.Fprintf(w, "\n\nGlobal Flags:\n")
2025 fmt.Fprint(w, trimRightSpace(c.InheritedFlags().FlagUsages()))
2026 }
2027 if c.HasHelpSubCommands() {
2028 fmt.Fprintf(w, "\n\nAdditional help topics:")
2029 for _, subcmd := range c.Commands() {
2030 if subcmd.IsAdditionalHelpTopicCommand() {
2031 fmt.Fprintf(w, "\n %s %s", rpad(subcmd.CommandPath(), subcmd.CommandPathPadding()), subcmd.Short)

Callers

nothing calls this directly

Calls 15

rpadFunction · 0.85
trimRightSpaceFunction · 0.85
RunnableMethod · 0.80
UseLineMethod · 0.80
CommandPathMethod · 0.80
NameAndAliasesMethod · 0.80
HasExampleMethod · 0.80
CommandsMethod · 0.80
GroupsMethod · 0.80
IsAvailableCommandMethod · 0.80
NameMethod · 0.80

Tested by

no test coverage detected