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