github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/common/internal/help_display.go (about)

     1  package internal
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/actor/sharedaction"
     9  	"code.cloudfoundry.org/cli/util/configv3"
    10  	"code.cloudfoundry.org/cli/util/sorting"
    11  )
    12  
    13  type HelpCategory struct {
    14  	CategoryName string
    15  	CommandList  [][]string
    16  }
    17  
    18  func ConvertPluginToCommandInfo(plugin configv3.PluginCommand) sharedaction.CommandInfo {
    19  	commandInfo := sharedaction.CommandInfo{
    20  		Name:        plugin.Name,
    21  		Description: plugin.HelpText,
    22  		Alias:       plugin.Alias,
    23  		Usage:       plugin.UsageDetails.Usage,
    24  		Flags:       []sharedaction.CommandFlag{},
    25  	}
    26  
    27  	flagNames := make([]string, 0, len(plugin.UsageDetails.Options))
    28  	for flag := range plugin.UsageDetails.Options {
    29  		flagNames = append(flagNames, flag)
    30  	}
    31  	sort.Slice(flagNames, sorting.SortAlphabeticFunc(flagNames))
    32  
    33  	for _, flag := range flagNames {
    34  		description := plugin.UsageDetails.Options[flag]
    35  		strippedFlag := strings.Trim(flag, "-")
    36  		switch len(flag) {
    37  		case 1:
    38  			commandInfo.Flags = append(commandInfo.Flags,
    39  				sharedaction.CommandFlag{
    40  					Short:       strippedFlag,
    41  					Description: description,
    42  				})
    43  		default:
    44  			commandInfo.Flags = append(commandInfo.Flags,
    45  				sharedaction.CommandFlag{
    46  					Long:        strippedFlag,
    47  					Description: description,
    48  				})
    49  		}
    50  	}
    51  
    52  	return commandInfo
    53  }
    54  
    55  func LongestCommandName(cmds map[string]sharedaction.CommandInfo, pluginCmds []configv3.PluginCommand) int {
    56  	longest := 0
    57  	for name, _ := range cmds {
    58  		if len(name) > longest {
    59  			longest = len(name)
    60  		}
    61  	}
    62  	for _, command := range pluginCmds {
    63  		if len(command.Name) > longest {
    64  			longest = len(command.Name)
    65  		}
    66  	}
    67  	return longest
    68  }
    69  
    70  func LongestFlagWidth(flags []sharedaction.CommandFlag) int {
    71  	longest := 0
    72  	for _, flag := range flags {
    73  		var name string
    74  		if flag.Short != "" && flag.Long != "" {
    75  			name = fmt.Sprintf("--%s, -%s", flag.Long, flag.Short)
    76  		} else if flag.Short != "" {
    77  			name = "-" + flag.Short
    78  		} else {
    79  			name = "--" + flag.Long
    80  		}
    81  		if len(name) > longest {
    82  			longest = len(name)
    83  		}
    84  	}
    85  	return longest
    86  }