Skip to content

Commit

Permalink
Add IsCobraInternalCommand function (#5)
Browse files Browse the repository at this point in the history
to check if a command is internal to cobra, e.g. help, __completion,. etc,.
  • Loading branch information
praveenrewar committed Sep 7, 2022
1 parent c28a9f2 commit da5ee3a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,22 @@ func ShowHelp(cmd *cobra.Command, args []string) error {
cmd.Help()
return fmt.Errorf("Invalid command - see available commands/subcommands above")
}

func IsCobraInternalCommand(args []string) bool {
if len(args) > 1 {
cmdPathPieces := args[1:]

var cmdName string // first "non-flag" arguments
for _, arg := range cmdPathPieces {
if !strings.HasPrefix(arg, "-") {
cmdName = arg
break
}
}
switch cmdName {
case "help", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
return true
}
}
return false
}

0 comments on commit da5ee3a

Please sign in to comment.