Install shared reporting flags

This commit is contained in:
2026-10-26 02:50:54 +00:00
parent 6ba9338c0a
commit f3bd6dc2ae
10 changed files with 226 additions and 10 deletions

View File

@@ -18,8 +18,10 @@ type Command struct {
Description string
Runnable bool
Flags []Flag
Arguments []Argument
Children []*Command
// CoreFlags are parsed by CommandKit but do not occupy consumer binding indexes.
CoreFlags []Flag
Arguments []Argument
Children []*Command
}
// Argument describes one positional input for usage rendering.
@@ -41,6 +43,26 @@ type Flag struct {
Required bool
}
var reportingFlags = [...]Flag{
{Name: "quiet", Usage: "suppress substantive output and optional messages; keep failures visible", Default: "false", Boolean: true},
{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},
}
// ReportingFlags returns the flags shared by CommandKit's invocation reporter.
func ReportingFlags() []Flag { return append([]Flag(nil), reportingFlags[:]...) }
// IsReportingFlag reports whether name is reserved for a shared reporting flag.
func IsReportingFlag(name string) bool {
for _, flag := range reportingFlags {
if flag.Name == name {
return true
}
}
return false
}
// FlagAlias maps one option spelling to a canonical flag spelling.
type FlagAlias struct {
Source string
@@ -58,6 +80,7 @@ type HelpView struct {
EnteredPath []string
CanonicalPath []string
Discovery bool
CoreFlags []Flag
CommandAliases []CommandAlias
FlagAliases []FlagAlias
}

View File

@@ -0,0 +1,28 @@
package commandmeta
import "testing"
func TestReportingFlagsAreBooleanAndReturnedIndependently(t *testing.T) {
flags := ReportingFlags()
want := []string{"quiet", "verbose", "debug", "progress"}
if len(flags) != len(want) {
t.Fatalf("reporting flags = %#v", flags)
}
for index, name := range want {
if flags[index].Name != name || !flags[index].Boolean || flags[index].Short != "" || flags[index].Default != "false" {
t.Errorf("reporting flag %d = %#v", index, flags[index])
}
if !IsReportingFlag(name) {
t.Errorf("%q is not recognized as a reporting flag", name)
}
}
flags[0].Name = "changed"
if ReportingFlags()[0].Name != "quiet" {
t.Fatal("caller mutation changed shared reporting metadata")
}
for _, name := range []string{"help", "quietly", ""} {
if IsReportingFlag(name) {
t.Errorf("%q was incorrectly recognized as a reporting flag", name)
}
}
}