Skip to content

Commit dc13080

Browse files
author
Dennis Ploeger
authored
feat: Add info about optional flags to usage (#416)
1 parent fa9b636 commit dc13080

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

help_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestHelpOptionalArgs(t *testing.T) {
5151
assert.NoError(t, err)
5252
})
5353
assert.True(t, exited)
54-
expected := `Usage: test-app [<one> [<two>]]
54+
expected := `Usage: test-app [<one> [<two>]] [flags]
5555
5656
Arguments:
5757
[<one>] One optional arg.
@@ -105,7 +105,7 @@ func TestHelp(t *testing.T) {
105105
assert.NoError(t, err)
106106
})
107107
assert.True(t, exited)
108-
expected := `Usage: test-app --required <command>
108+
expected := `Usage: test-app --required <command> [flags]
109109
110110
A test app.
111111
@@ -120,13 +120,13 @@ Flags:
120120
-s, --[no-]sort Is sortable or not.
121121
122122
Commands:
123-
one --required
123+
one --required [flags]
124124
A subcommand.
125125
126-
two <three> --required --required-two --required-three
126+
two <three> --required --required-two --required-three [flags]
127127
Sub-sub-arg.
128128
129-
two four --required --required-two
129+
two four --required --required-two [flags]
130130
Sub-sub-command.
131131
132132
Run "test-app <command> --help" for more information on a command.
@@ -144,7 +144,7 @@ Run "test-app <command> --help" for more information on a command.
144144
assert.NoError(t, err)
145145
})
146146
assert.True(t, exited)
147-
expected := `Usage: test-app two <three> --required --required-two --required-three
147+
expected := `Usage: test-app two <three> --required --required-two --required-three [flags]
148148
149149
Sub-sub-arg.
150150
@@ -215,18 +215,18 @@ func TestFlagsLast(t *testing.T) {
215215
assert.NoError(t, err)
216216
})
217217
assert.True(t, exited)
218-
expected := `Usage: test-app --required <command>
218+
expected := `Usage: test-app --required <command> [flags]
219219
220220
A test app.
221221
222222
Commands:
223-
one --required
223+
one --required [flags]
224224
A subcommand.
225225
226-
two <three> --required --required-two --required-three
226+
two <three> --required --required-two --required-three [flags]
227227
Sub-sub-arg.
228228
229-
two four --required --required-two
229+
two four --required --required-two [flags]
230230
Sub-sub-command.
231231
232232
Flags:
@@ -253,7 +253,7 @@ Run "test-app <command> --help" for more information on a command.
253253
assert.NoError(t, err)
254254
})
255255
assert.True(t, exited)
256-
expected := `Usage: test-app two <three> --required --required-two --required-three
256+
expected := `Usage: test-app two <three> --required --required-two --required-three [flags]
257257
258258
Sub-sub-arg.
259259
@@ -320,7 +320,7 @@ func TestHelpTree(t *testing.T) {
320320
assert.NoError(t, err)
321321
})
322322
assert.True(t, exited)
323-
expected := `Usage: test-app <command>
323+
expected := `Usage: test-app <command> [flags]
324324
325325
A test app.
326326
@@ -353,7 +353,7 @@ Run "test-app <command> --help" for more information on a command.
353353
assert.NoError(t, err)
354354
})
355355
assert.True(t, exited)
356-
expected := `Usage: test-app one (un,uno) <command>
356+
expected := `Usage: test-app one (un,uno) <command> [flags]
357357
358358
subcommand one
359359
@@ -414,7 +414,7 @@ func TestHelpCompactNoExpand(t *testing.T) {
414414
assert.NoError(t, err)
415415
})
416416
assert.True(t, exited)
417-
expected := `Usage: test-app <command>
417+
expected := `Usage: test-app <command> [flags]
418418
419419
A test app.
420420
@@ -443,7 +443,7 @@ Run "test-app <command> --help" for more information on a command.
443443
assert.NoError(t, err)
444444
})
445445
assert.True(t, exited)
446-
expected := `Usage: test-app one (un,uno) <command>
446+
expected := `Usage: test-app one (un,uno) <command> [flags]
447447
448448
subcommand one
449449
@@ -607,7 +607,7 @@ func TestAutoGroup(t *testing.T) {
607607
}),
608608
)
609609
_, _ = app.Parse([]string{"--help", "two"})
610-
assert.Equal(t, `Usage: test two
610+
assert.Equal(t, `Usage: test two [flags]
611611
612612
A non grouped subcommand.
613613
@@ -691,7 +691,7 @@ func TestHelpGrouping(t *testing.T) {
691691
assert.True(t, exited)
692692
assert.NoError(t, err)
693693
})
694-
expected := `Usage: test-app <command>
694+
expected := `Usage: test-app <command> [flags]
695695
696696
A test app.
697697
@@ -710,26 +710,26 @@ Group B
710710
--grouped-b-string=STRING A string flag grouped in B.
711711
712712
Commands:
713-
two
713+
two [flags]
714714
A non grouped subcommand.
715715
716716
Group title taken from the kong.ExplicitGroups option
717717
A group header
718718
719-
one thing <arg>
719+
one thing <arg> [flags]
720720
subcommand thing
721721
722-
one <other>
722+
one <other> [flags]
723723
subcommand other
724724
725-
three
725+
three [flags]
726726
Another subcommand grouped in A.
727727
728728
Group B
729-
one <stuff>
729+
one <stuff> [flags]
730730
subcommand stuff
731731
732-
four
732+
four [flags]
733733
Another subcommand grouped in B.
734734
735735
Run "test-app <command> --help" for more information on a command.
@@ -747,7 +747,7 @@ Run "test-app <command> --help" for more information on a command.
747747
assert.NoError(t, err)
748748
assert.True(t, exited)
749749
})
750-
expected := `Usage: test-app two
750+
expected := `Usage: test-app two [flags]
751751
752752
A non grouped subcommand.
753753
@@ -796,7 +796,7 @@ func TestUsageOnError(t *testing.T) {
796796
_, err := p.Parse([]string{})
797797
p.FatalIfErrorf(err)
798798

799-
expected := `Usage: test --flag=STRING
799+
expected := `Usage: test --flag=STRING [flags]
800800
801801
Some description.
802802
@@ -824,7 +824,7 @@ func TestShortUsageOnError(t *testing.T) {
824824
assert.Error(t, err)
825825
p.FatalIfErrorf(err)
826826

827-
expected := `Usage: test --flag=STRING
827+
expected := `Usage: test --flag=STRING [flags]
828828
Run "test --help" for more information.
829829
830830
test: error: missing flags: --flag=STRING

helpwrap1.19_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestCustomWrap(t *testing.T) {
3232

3333
_, err := app.Parse([]string{"--help"})
3434
assert.NoError(t, err)
35-
expected := `Usage: test-app
35+
expected := `Usage: test-app [flags]
3636
3737
A test app.
3838

kong_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ func TestPassthroughCmdOnlyArgs(t *testing.T) {
15761576
} `cmd:"" passthrough:""`
15771577
}
15781578
_, err := kong.New(&cli)
1579-
assert.EqualError(t, err, "<anonymous struct>.Command: passthrough command command [<args> ...] must not have subcommands or flags")
1579+
assert.EqualError(t, err, "<anonymous struct>.Command: passthrough command command [<args> ...] [flags] must not have subcommands or flags")
15801580
}
15811581

15821582
func TestPassthroughCmdOnlyStringArgs(t *testing.T) {

model.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ func (n *Node) Summary() string {
162162
} else if len(n.Children) > 0 {
163163
summary += " <command>"
164164
}
165+
allFlags := n.Flags
166+
if n.Parent != nil {
167+
allFlags = append(allFlags, n.Parent.Flags...)
168+
}
169+
for _, flag := range allFlags {
170+
if !flag.Required {
171+
summary += " [flags]"
172+
break
173+
}
174+
}
165175
return summary
166176
}
167177

0 commit comments

Comments
 (0)