feat: Add info about optional flags to usage (#416)
This commit is contained in:
+26
-26
@@ -51,7 +51,7 @@ func TestHelpOptionalArgs(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app [<one> [<two>]]
|
expected := `Usage: test-app [<one> [<two>]] [flags]
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
[<one>] One optional arg.
|
[<one>] One optional arg.
|
||||||
@@ -105,7 +105,7 @@ func TestHelp(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app --required <command>
|
expected := `Usage: test-app --required <command> [flags]
|
||||||
|
|
||||||
A test app.
|
A test app.
|
||||||
|
|
||||||
@@ -120,13 +120,13 @@ Flags:
|
|||||||
-s, --[no-]sort Is sortable or not.
|
-s, --[no-]sort Is sortable or not.
|
||||||
|
|
||||||
Commands:
|
Commands:
|
||||||
one --required
|
one --required [flags]
|
||||||
A subcommand.
|
A subcommand.
|
||||||
|
|
||||||
two <three> --required --required-two --required-three
|
two <three> --required --required-two --required-three [flags]
|
||||||
Sub-sub-arg.
|
Sub-sub-arg.
|
||||||
|
|
||||||
two four --required --required-two
|
two four --required --required-two [flags]
|
||||||
Sub-sub-command.
|
Sub-sub-command.
|
||||||
|
|
||||||
Run "test-app <command> --help" for more information on a command.
|
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.
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app two <three> --required --required-two --required-three
|
expected := `Usage: test-app two <three> --required --required-two --required-three [flags]
|
||||||
|
|
||||||
Sub-sub-arg.
|
Sub-sub-arg.
|
||||||
|
|
||||||
@@ -215,18 +215,18 @@ func TestFlagsLast(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app --required <command>
|
expected := `Usage: test-app --required <command> [flags]
|
||||||
|
|
||||||
A test app.
|
A test app.
|
||||||
|
|
||||||
Commands:
|
Commands:
|
||||||
one --required
|
one --required [flags]
|
||||||
A subcommand.
|
A subcommand.
|
||||||
|
|
||||||
two <three> --required --required-two --required-three
|
two <three> --required --required-two --required-three [flags]
|
||||||
Sub-sub-arg.
|
Sub-sub-arg.
|
||||||
|
|
||||||
two four --required --required-two
|
two four --required --required-two [flags]
|
||||||
Sub-sub-command.
|
Sub-sub-command.
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
@@ -253,7 +253,7 @@ Run "test-app <command> --help" for more information on a command.
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app two <three> --required --required-two --required-three
|
expected := `Usage: test-app two <three> --required --required-two --required-three [flags]
|
||||||
|
|
||||||
Sub-sub-arg.
|
Sub-sub-arg.
|
||||||
|
|
||||||
@@ -320,7 +320,7 @@ func TestHelpTree(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app <command>
|
expected := `Usage: test-app <command> [flags]
|
||||||
|
|
||||||
A test app.
|
A test app.
|
||||||
|
|
||||||
@@ -353,7 +353,7 @@ Run "test-app <command> --help" for more information on a command.
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app one (un,uno) <command>
|
expected := `Usage: test-app one (un,uno) <command> [flags]
|
||||||
|
|
||||||
subcommand one
|
subcommand one
|
||||||
|
|
||||||
@@ -414,7 +414,7 @@ func TestHelpCompactNoExpand(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app <command>
|
expected := `Usage: test-app <command> [flags]
|
||||||
|
|
||||||
A test app.
|
A test app.
|
||||||
|
|
||||||
@@ -443,7 +443,7 @@ Run "test-app <command> --help" for more information on a command.
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
expected := `Usage: test-app one (un,uno) <command>
|
expected := `Usage: test-app one (un,uno) <command> [flags]
|
||||||
|
|
||||||
subcommand one
|
subcommand one
|
||||||
|
|
||||||
@@ -607,7 +607,7 @@ func TestAutoGroup(t *testing.T) {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
_, _ = app.Parse([]string{"--help", "two"})
|
_, _ = app.Parse([]string{"--help", "two"})
|
||||||
assert.Equal(t, `Usage: test two
|
assert.Equal(t, `Usage: test two [flags]
|
||||||
|
|
||||||
A non grouped subcommand.
|
A non grouped subcommand.
|
||||||
|
|
||||||
@@ -691,7 +691,7 @@ func TestHelpGrouping(t *testing.T) {
|
|||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
expected := `Usage: test-app <command>
|
expected := `Usage: test-app <command> [flags]
|
||||||
|
|
||||||
A test app.
|
A test app.
|
||||||
|
|
||||||
@@ -710,26 +710,26 @@ Group B
|
|||||||
--grouped-b-string=STRING A string flag grouped in B.
|
--grouped-b-string=STRING A string flag grouped in B.
|
||||||
|
|
||||||
Commands:
|
Commands:
|
||||||
two
|
two [flags]
|
||||||
A non grouped subcommand.
|
A non grouped subcommand.
|
||||||
|
|
||||||
Group title taken from the kong.ExplicitGroups option
|
Group title taken from the kong.ExplicitGroups option
|
||||||
A group header
|
A group header
|
||||||
|
|
||||||
one thing <arg>
|
one thing <arg> [flags]
|
||||||
subcommand thing
|
subcommand thing
|
||||||
|
|
||||||
one <other>
|
one <other> [flags]
|
||||||
subcommand other
|
subcommand other
|
||||||
|
|
||||||
three
|
three [flags]
|
||||||
Another subcommand grouped in A.
|
Another subcommand grouped in A.
|
||||||
|
|
||||||
Group B
|
Group B
|
||||||
one <stuff>
|
one <stuff> [flags]
|
||||||
subcommand stuff
|
subcommand stuff
|
||||||
|
|
||||||
four
|
four [flags]
|
||||||
Another subcommand grouped in B.
|
Another subcommand grouped in B.
|
||||||
|
|
||||||
Run "test-app <command> --help" for more information on a command.
|
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.
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.True(t, exited)
|
assert.True(t, exited)
|
||||||
})
|
})
|
||||||
expected := `Usage: test-app two
|
expected := `Usage: test-app two [flags]
|
||||||
|
|
||||||
A non grouped subcommand.
|
A non grouped subcommand.
|
||||||
|
|
||||||
@@ -796,7 +796,7 @@ func TestUsageOnError(t *testing.T) {
|
|||||||
_, err := p.Parse([]string{})
|
_, err := p.Parse([]string{})
|
||||||
p.FatalIfErrorf(err)
|
p.FatalIfErrorf(err)
|
||||||
|
|
||||||
expected := `Usage: test --flag=STRING
|
expected := `Usage: test --flag=STRING [flags]
|
||||||
|
|
||||||
Some description.
|
Some description.
|
||||||
|
|
||||||
@@ -824,7 +824,7 @@ func TestShortUsageOnError(t *testing.T) {
|
|||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
p.FatalIfErrorf(err)
|
p.FatalIfErrorf(err)
|
||||||
|
|
||||||
expected := `Usage: test --flag=STRING
|
expected := `Usage: test --flag=STRING [flags]
|
||||||
Run "test --help" for more information.
|
Run "test --help" for more information.
|
||||||
|
|
||||||
test: error: missing flags: --flag=STRING
|
test: error: missing flags: --flag=STRING
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func TestCustomWrap(t *testing.T) {
|
|||||||
|
|
||||||
_, err := app.Parse([]string{"--help"})
|
_, err := app.Parse([]string{"--help"})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
expected := `Usage: test-app
|
expected := `Usage: test-app [flags]
|
||||||
|
|
||||||
A test app.
|
A test app.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1576,7 +1576,7 @@ func TestPassthroughCmdOnlyArgs(t *testing.T) {
|
|||||||
} `cmd:"" passthrough:""`
|
} `cmd:"" passthrough:""`
|
||||||
}
|
}
|
||||||
_, err := kong.New(&cli)
|
_, err := kong.New(&cli)
|
||||||
assert.EqualError(t, err, "<anonymous struct>.Command: passthrough command command [<args> ...] must not have subcommands or flags")
|
assert.EqualError(t, err, "<anonymous struct>.Command: passthrough command command [<args> ...] [flags] must not have subcommands or flags")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPassthroughCmdOnlyStringArgs(t *testing.T) {
|
func TestPassthroughCmdOnlyStringArgs(t *testing.T) {
|
||||||
|
|||||||
@@ -162,6 +162,16 @@ func (n *Node) Summary() string {
|
|||||||
} else if len(n.Children) > 0 {
|
} else if len(n.Children) > 0 {
|
||||||
summary += " <command>"
|
summary += " <command>"
|
||||||
}
|
}
|
||||||
|
allFlags := n.Flags
|
||||||
|
if n.Parent != nil {
|
||||||
|
allFlags = append(allFlags, n.Parent.Flags...)
|
||||||
|
}
|
||||||
|
for _, flag := range allFlags {
|
||||||
|
if !flag.Required {
|
||||||
|
summary += " [flags]"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
return summary
|
return summary
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user