...

Source file src/k8s.io/kubectl/pkg/cmd/help/help.go

Documentation: k8s.io/kubectl/pkg/cmd/help

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package help
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	"k8s.io/kubectl/pkg/util/i18n"
    25  	"k8s.io/kubectl/pkg/util/templates"
    26  )
    27  
    28  var helpLong = templates.LongDesc(i18n.T(`
    29  	Help provides help for any command in the application.
    30  	Simply type kubectl help [path to command] for full details.`))
    31  
    32  // NewCmdHelp returns the help Cobra command
    33  func NewCmdHelp() *cobra.Command {
    34  	cmd := &cobra.Command{
    35  		Use:                   "help [command] | STRING_TO_SEARCH",
    36  		DisableFlagsInUseLine: true,
    37  		Short:                 i18n.T("Help about any command"),
    38  		Long:                  helpLong,
    39  
    40  		Run: RunHelp,
    41  	}
    42  
    43  	return cmd
    44  }
    45  
    46  // RunHelp checks given arguments and executes command
    47  func RunHelp(cmd *cobra.Command, args []string) {
    48  	foundCmd, _, err := cmd.Root().Find(args)
    49  
    50  	// NOTE(andreykurilin): actually, I did not find any cases when foundCmd can be nil,
    51  	//   but let's make this check since it is included in original code of initHelpCmd
    52  	//   from github.com/spf13/cobra
    53  	if foundCmd == nil {
    54  		cmd.Printf("Unknown help topic %#q.\n", args)
    55  		cmd.Root().Usage()
    56  	} else if err != nil {
    57  		// print error message at first, since it can contain suggestions
    58  		cmd.Println(err)
    59  
    60  		argsString := strings.Join(args, " ")
    61  		var matchedMsgIsPrinted = false
    62  		for _, foundCmd := range foundCmd.Commands() {
    63  			if strings.Contains(foundCmd.Short, argsString) {
    64  				if !matchedMsgIsPrinted {
    65  					cmd.Printf("Matchers of string '%s' in short descriptions of commands: \n", argsString)
    66  					matchedMsgIsPrinted = true
    67  				}
    68  				cmd.Printf("  %-14s %s\n", foundCmd.Name(), foundCmd.Short)
    69  			}
    70  		}
    71  
    72  		if !matchedMsgIsPrinted {
    73  			// if nothing is found, just print usage
    74  			cmd.Root().Usage()
    75  		}
    76  	} else {
    77  		if len(args) == 0 {
    78  			// help message for help command :)
    79  			foundCmd = cmd
    80  		}
    81  		helpFunc := foundCmd.HelpFunc()
    82  		helpFunc(foundCmd, args)
    83  	}
    84  }
    85  

View as plain text