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.
()
| 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. |
| 1263 | func (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. |
| 1273 | Simply 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. |
| 1317 | func (c *Command) ResetCommands() { |
no test coverage detected