Add shared report format flag

This commit is contained in:
2026-11-07 03:45:12 +00:00
parent 3a6c66f550
commit 924692b541
18 changed files with 206 additions and 101 deletions

View File

@@ -80,10 +80,10 @@ 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"}}},
{Name: "show", Runnable: true, CoreFlags: commandmeta.ReportingFlags(), Flags: []Flag{{Name: "style"}}},
}},
}}
result := Parse(tree, []string{"config", "show", "--quiet", "-v", "--format=yaml"}, ParseOptions{
result := Parse(tree, []string{"config", "show", "--quiet", "-v", "--style=yaml"}, ParseOptions{
FlagAliases: []commandmeta.FlagAlias{{Source: "-v", Target: "--verbose"}},
})
if result.Failure != nil {
@@ -92,7 +92,7 @@ func TestReportingFlagsParseWithoutChangingConsumerFlagIndexes(t *testing.T) {
if !reflect.DeepEqual(result.FlagValues, [][]string{{"yaml"}}) {
t.Fatalf("consumer binding values = %#v", result.FlagValues)
}
if !reflect.DeepEqual(result.FlagSpellings, [][]string{{"--format"}}) {
if !reflect.DeepEqual(result.FlagSpellings, [][]string{{"--style"}}) {
t.Fatalf("consumer binding spellings = %#v", result.FlagSpellings)
}
}

View File

@@ -79,6 +79,7 @@ var reportingFlags = [...]Flag{
{Name: "verbose", Usage: "show warning and informational messages", Default: "false", Boolean: true},
{Name: "debug", Usage: "show debugging messages", Default: "false", Boolean: true},
{Name: "progress", Usage: "show progress messages", Default: "false", Boolean: true},
{Name: "format", Usage: "report format: text, yaml, or json", Completion: CompletionHint{Kind: CompletionChoices, Choices: []CompletionChoice{{Value: "text"}, {Value: "yaml"}, {Value: "json"}}}},
}
// BooleanCompletion returns the ordinary explicit boolean suggestions.
@@ -95,7 +96,11 @@ func HelpFlag() Flag {
func ReportingFlags() []Flag {
flags := append([]Flag(nil), reportingFlags[:]...)
for index := range flags {
flags[index].Completion = BooleanCompletion()
if flags[index].Boolean {
flags[index].Completion = BooleanCompletion()
} else {
flags[index].Completion.Choices = append([]CompletionChoice(nil), flags[index].Completion.Choices...)
}
}
return flags
}
@@ -160,6 +165,7 @@ type ReportingSelection struct {
Mode ReportingMode
Progress bool
ProgressSet bool
Format string
}
// Result contains the selected command and raw input collected by the adapter.

View File

@@ -5,7 +5,7 @@ import "testing"
func TestReportingFlagsAreBooleanAndReturnedIndependently(t *testing.T) {
flags := ReportingFlags()
want := []string{"quiet", "verbose", "debug", "progress"}
if len(flags) != len(want) {
if len(flags) != len(want)+1 {
t.Fatalf("reporting flags = %#v", flags)
}
for index, name := range want {
@@ -16,6 +16,13 @@ func TestReportingFlagsAreBooleanAndReturnedIndependently(t *testing.T) {
t.Errorf("%q is not recognized as a reporting flag", name)
}
}
if flags[4].Name != "format" || flags[4].Boolean || !IsReportingFlag("format") {
t.Fatalf("format metadata = %#v", flags[4])
}
flags[4].Completion.Choices[0].Value = "changed"
if ReportingFlags()[4].Completion.Choices[0].Value != "text" {
t.Fatal("caller mutation changed format choices")
}
flags[0].Name = "changed"
if ReportingFlags()[0].Name != "quiet" {
t.Fatal("caller mutation changed shared reporting metadata")