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

Method InitDefaultHelpCmd

command.go:1263–1314  ·  view source on GitHub ↗

InitDefaultHelpCmd adds default help command to c. It is called automatically by executing the c or by calling help and usage. If c already has help command or c has no subcommands, it will do nothing.

()

Source from the content-addressed store, hash-verified

1261// It is called automatically by executing the c or by calling help and usage.
1262// If c already has help command or c has no subcommands, it will do nothing.
1263func (c *Command) InitDefaultHelpCmd() {
1264 if !c.HasSubCommands() {
1265 return
1266 }
1267
1268 if c.helpCommand == nil {
1269 c.helpCommand = &Command{
1270 Use: "help [command]",
1271 Short: "Help about any command",
1272 Long: `Help provides help for any command in the application.
1273Simply type ` + c.DisplayName() + ` help [path to command] for full details.`,
1274 ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) {
1275 var completions []Completion
1276 cmd, _, e := c.Root().Find(args)
1277 if e != nil {
1278 return nil, ShellCompDirectiveNoFileComp
1279 }
1280 if cmd == nil {
1281 // Root help command.
1282 cmd = c.Root()
1283 }
1284 for _, subCmd := range cmd.Commands() {
1285 if subCmd.IsAvailableCommand() || subCmd == cmd.helpCommand {
1286 if strings.HasPrefix(subCmd.Name(), toComplete) {
1287 completions = append(completions, CompletionWithDesc(subCmd.Name(), subCmd.Short))
1288 }
1289 }
1290 }
1291 return completions, ShellCompDirectiveNoFileComp
1292 },
1293 Run: func(c *Command, args []string) {
1294 cmd, _, e := c.Root().Find(args)
1295 if cmd == nil || e != nil {
1296 c.Printf("Unknown help topic %#q\n", args)
1297 CheckErr(c.Root().Usage())
1298 } else {
1299 // FLow the context down to be used in help text
1300 if cmd.ctx == nil {
1301 cmd.ctx = c.ctx
1302 }
1303
1304 cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
1305 cmd.InitDefaultVersionFlag() // make possible 'version' flag to be shown
1306 CheckErr(cmd.Help())
1307 }
1308 },
1309 GroupID: c.helpCommandGroupID,
1310 }
1311 }
1312 c.RemoveCommand(c.helpCommand)
1313 c.AddCommand(c.helpCommand)
1314}
1315
1316// ResetCommands delete parent, subcommand and help command from c.
1317func (c *Command) ResetCommands() {

Callers 5

ExecuteCMethod · 0.95
genManFunction · 0.80
GenYamlCustomFunction · 0.80
GenReSTCustomFunction · 0.80
GenMarkdownCustomFunction · 0.80

Calls 15

HasSubCommandsMethod · 0.95
DisplayNameMethod · 0.95
RootMethod · 0.95
PrintfMethod · 0.95
RemoveCommandMethod · 0.95
AddCommandMethod · 0.95
CompletionWithDescFunction · 0.85
CheckErrFunction · 0.85
FindMethod · 0.80
CommandsMethod · 0.80
IsAvailableCommandMethod · 0.80
NameMethod · 0.80

Tested by

no test coverage detected