Skip to content

Commit

Permalink
Hide hidden sub-commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Mar 29, 2019
1 parent 42ea64b commit 4e98780
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -12,7 +12,7 @@ jobs:
command: |
go get -v github.com/jstemmer/go-junit-report
go get -v -t -d ./...
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s v1.10.2
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s v1.15.0
mkdir ~/report
when: always
- run:
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Expand Up @@ -9,6 +9,7 @@ linters:
disable:
- maligned
- lll
- gochecknoglobals

linters-settings:
govet:
Expand Down
29 changes: 20 additions & 9 deletions help.go
Expand Up @@ -142,6 +142,9 @@ func printNodeDetail(w *helpWriter, node *Node, hide bool) {

func writeCommandList(cmds []*Node, iw *helpWriter) {
for i, cmd := range cmds {
if cmd.Hidden {
continue
}
printCommandSummary(iw, cmd)
if i != len(cmds)-1 {
iw.Print("")
Expand All @@ -152,29 +155,37 @@ func writeCommandList(cmds []*Node, iw *helpWriter) {
func writeCompactCommandList(cmds []*Node, iw *helpWriter) {
rows := [][2]string{}
for _, cmd := range cmds {
if cmd.Hidden {
continue
}
rows = append(rows, [2]string{cmd.Path(), cmd.Help})
}
writeTwoColumns(iw, defaultColumnPadding, rows)
writeTwoColumns(iw, rows)
}

func writeCommandTree(w *helpWriter, node *Node) {
iw := w.Indent()
rows := make([][2]string, 0, len(node.Children)*2)
for i, cmd := range node.Children {
if cmd.Hidden {
continue
}
rows = append(rows, w.commandTree(cmd, "")...)
if i != len(node.Children)-1 {
rows = append(rows, [2]string{"", ""})
}
}
writeTwoColumns(iw, defaultColumnPadding, rows)
writeTwoColumns(iw, rows)
}

// nolint: unused
type helpCommandGroup struct {
Name string
Commands []*Node
}

func collectCommandGroups(nodes []*Node) []helpCommandGroup { // nolint: deadcode
// nolint: unused, deadcode
func collectCommandGroups(nodes []*Node) []helpCommandGroup {
groups := map[string][]*Node{}
for _, node := range nodes {
groups[node.Group] = append(groups[node.Group], node)
Expand Down Expand Up @@ -253,7 +264,7 @@ func writePositionals(w *helpWriter, args []*Positional) {
for _, arg := range args {
rows = append(rows, [2]string{arg.Summary(), arg.Help})
}
writeTwoColumns(w, defaultColumnPadding, rows)
writeTwoColumns(w, rows)
}

func writeFlags(w *helpWriter, groups [][]*Flag) {
Expand All @@ -277,10 +288,10 @@ func writeFlags(w *helpWriter, groups [][]*Flag) {
}
}
}
writeTwoColumns(w, defaultColumnPadding, rows)
writeTwoColumns(w, rows)
}

func writeTwoColumns(w *helpWriter, padding int, rows [][2]string) {
func writeTwoColumns(w *helpWriter, rows [][2]string) {
maxLeft := 375 * w.width / 1000
if maxLeft < 30 {
maxLeft = 30
Expand All @@ -293,16 +304,16 @@ func writeTwoColumns(w *helpWriter, padding int, rows [][2]string) {
}
}

offsetStr := strings.Repeat(" ", leftSize+padding)
offsetStr := strings.Repeat(" ", leftSize+defaultColumnPadding)

for _, row := range rows {
buf := bytes.NewBuffer(nil)
doc.ToText(buf, row[1], "", strings.Repeat(" ", defaultIndent), w.width-leftSize-padding)
doc.ToText(buf, row[1], "", strings.Repeat(" ", defaultIndent), w.width-leftSize-defaultColumnPadding)
lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")

line := fmt.Sprintf("%-*s", leftSize, row[0])
if len(row[0]) < maxLeft {
line += fmt.Sprintf("%*s%s", padding, "", lines[0])
line += fmt.Sprintf("%*s%s", defaultColumnPadding, "", lines[0])
lines = lines[1:]
}
w.Print(line)
Expand Down
1 change: 1 addition & 0 deletions kong_test.go
Expand Up @@ -409,6 +409,7 @@ func TestHooks(t *testing.T) {
for _, test := range tests {
*ctx = hookContext{}
cli.One = hookCmd{}
// nolint: scopelint
t.Run(test.name, func(t *testing.T) {
_, err := p.Parse(strings.Split(test.input, " "))
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion util.go
Expand Up @@ -13,7 +13,7 @@ type ConfigFlag string
// BeforeResolve adds a resolver.
func (c ConfigFlag) BeforeResolve(kong *Kong, ctx *Context, trace *Path) error {
if kong.loader == nil {
return fmt.Errorf("Kong must be configured with kong.Configuration(...)")
return fmt.Errorf("kong must be configured with kong.Configuration(...)")
}
path := string(ctx.FlagValue(trace.Flag).(ConfigFlag))
resolver, err := kong.LoadConfig(path)
Expand Down

0 comments on commit 4e98780

Please sign in to comment.