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