Install shared reporting flags
This commit is contained in:
@@ -77,8 +77,11 @@ func (session *Session) Parse(argv []string) commandmeta.Result {
|
||||
}
|
||||
|
||||
remaining := append(commandpath.CloneTokens(unconsumed), tail...)
|
||||
interpreted, spellings, interpretationFailure := interpretFlagTokens(remaining, selectedNode.Flags, session.flagAliases)
|
||||
for _, flag := range selectedNode.Flags {
|
||||
localFlags := make([]commandmeta.Flag, 0, len(selectedNode.Flags)+len(selectedNode.CoreFlags))
|
||||
localFlags = append(localFlags, selectedNode.Flags...)
|
||||
localFlags = append(localFlags, selectedNode.CoreFlags...)
|
||||
interpreted, spellings, interpretationFailure := interpretFlagTokens(remaining, localFlags, session.flagAliases)
|
||||
for _, flag := range localFlags {
|
||||
if value := selected.Flags().Lookup(flag.Name); value != nil {
|
||||
value.Value.(*rawValue).queueSpellings(spellings[flag.Name])
|
||||
}
|
||||
@@ -209,6 +212,12 @@ func appendHelpAliases(content string, commandAliases []commandmeta.CommandAlias
|
||||
|
||||
func renderDiscoveryHelp(rootName string, view commandmeta.HelpView) string {
|
||||
lines := []string{"Usage:", " " + displayCommandPath(rootName, view.EnteredPath) + " [command]"}
|
||||
if len(view.CoreFlags) > 0 {
|
||||
lines = append(lines, "", "Reporting Flags:")
|
||||
for _, flag := range view.CoreFlags {
|
||||
lines = append(lines, " --"+flag.Name+" — "+flag.Usage+" (default: "+flag.Default+")")
|
||||
}
|
||||
}
|
||||
if len(view.CommandAliases) > 0 {
|
||||
lines = append(lines, "", "Available Aliases:")
|
||||
for _, alias := range view.CommandAliases {
|
||||
@@ -309,7 +318,7 @@ func buildTree(root *commandmeta.Command, options commandmeta.ParseOptions) (*sp
|
||||
helpFlag := backend.Flags().Lookup("help")
|
||||
helpFlag.NoOptDefVal = "true"
|
||||
helpFlag.Usage = "help for this command (default: false)"
|
||||
for _, declarationFlag := range declaration.Flags {
|
||||
for _, declarationFlag := range append(append([]commandmeta.Flag(nil), declaration.Flags...), declaration.CoreFlags...) {
|
||||
value := &rawValue{boolean: declarationFlag.Boolean}
|
||||
backend.Flags().VarP(value, declarationFlag.Name, declarationFlag.Short, flagUsage(declarationFlag))
|
||||
backendFlag := backend.Flags().Lookup(declarationFlag.Name)
|
||||
|
||||
@@ -77,6 +77,62 @@ func TestParseInterspersedOperandsBooleanFlagsAndTerminator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportingFlagsParseWithoutChangingConsumerFlagIndexes(t *testing.T) {
|
||||
tree := &Command{Name: "app", CoreFlags: commandmeta.ReportingFlags(), Children: []*Command{
|
||||
{Name: "config", CoreFlags: commandmeta.ReportingFlags(), Children: []*Command{
|
||||
{Name: "show", Runnable: true, CoreFlags: commandmeta.ReportingFlags(), Flags: []Flag{{Name: "format"}}},
|
||||
}},
|
||||
}}
|
||||
result := Parse(tree, []string{"config", "show", "--quiet", "-v", "--format=yaml"}, ParseOptions{
|
||||
FlagAliases: []commandmeta.FlagAlias{{Source: "-v", Target: "--verbose"}},
|
||||
})
|
||||
if result.Failure != nil {
|
||||
t.Fatalf("parse failed: %s", result.Failure.Message)
|
||||
}
|
||||
if !reflect.DeepEqual(result.FlagValues, [][]string{{"yaml"}}) {
|
||||
t.Fatalf("consumer binding values = %#v", result.FlagValues)
|
||||
}
|
||||
if !reflect.DeepEqual(result.FlagSpellings, [][]string{{"--format"}}) {
|
||||
t.Fatalf("consumer binding spellings = %#v", result.FlagSpellings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportingFlagsAppearInCanonicalAndDiscoveryHelp(t *testing.T) {
|
||||
flags := commandmeta.ReportingFlags()
|
||||
root := &Command{Name: "app", CoreFlags: flags, Children: []*Command{
|
||||
{Name: "group", CoreFlags: commandmeta.ReportingFlags(), Children: []*Command{
|
||||
{Name: "run", Runnable: true, CoreFlags: commandmeta.ReportingFlags()},
|
||||
}},
|
||||
{Name: "version", Runnable: true, CoreFlags: commandmeta.ReportingFlags()},
|
||||
}}
|
||||
session := Prepare(root, ParseOptions{})
|
||||
for _, path := range [][]string{nil, {"group"}, {"group", "run"}, {"version"}} {
|
||||
help, failure := session.RenderHelp(path)
|
||||
if failure != nil {
|
||||
t.Fatalf("help for %v failed: %s", path, failure.Message)
|
||||
}
|
||||
for _, name := range []string{"quiet", "verbose", "debug", "progress"} {
|
||||
if !strings.Contains(help, "--"+name) {
|
||||
t.Errorf("help for %v omits --%s: %q", path, name, help)
|
||||
}
|
||||
}
|
||||
}
|
||||
discovery, failure := session.RenderHelpView(commandmeta.HelpView{
|
||||
EnteredPath: []string{"shortcut"}, Discovery: true, CoreFlags: commandmeta.ReportingFlags(),
|
||||
})
|
||||
if failure != nil {
|
||||
t.Fatal(failure.Message)
|
||||
}
|
||||
for _, name := range []string{"quiet", "verbose", "debug", "progress"} {
|
||||
if !strings.Contains(discovery, "--"+name) {
|
||||
t.Errorf("discovery help omits --%s: %q", name, discovery)
|
||||
}
|
||||
}
|
||||
if strings.Contains(discovery, "Available Commands:") || len(session.Metadata().Children) != 2 {
|
||||
t.Fatalf("discovery help manufactured canonical command metadata: %q", discovery)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseHelpIsLocalAndFalseDoesNotRequestHelp(t *testing.T) {
|
||||
tree := &Command{Name: "app", Children: []*Command{
|
||||
{Name: "run", Runnable: true, Flags: []Flag{{Name: "required", Required: true}}},
|
||||
|
||||
Reference in New Issue
Block a user