From 7bd425992d99c33d9ef035b15fd0ea6fe37f1896 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 11 Nov 2026 13:59:00 -0600 Subject: [PATCH] babycli: enable version flag --- commands.go | 14 +++++++--- commands_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ help.go | 9 +++++++ setup.go | 17 +++++++++++- 4 files changed, 106 insertions(+), 4 deletions(-) diff --git a/commands.go b/commands.go index 0e48fa5..3ed94cb 100644 --- a/commands.go +++ b/commands.go @@ -39,9 +39,9 @@ func (v *values) durationCount(flag string) int { return len(v.durations[flag]) } -func (v *values) helpSet() bool { +func (v *values) boolSet(long, short string) bool { for k, bs := range v.bools { - if k == "help" || k == "h" { + if k == long || k == short { for _, b := range bs { if b { return true @@ -93,6 +93,8 @@ type Component struct { version string + vstring string + context context.Context } @@ -143,12 +145,17 @@ func (c *Component) run(output io.Writer) *result { } } - if c.vals.helpSet() { + if c.vals.boolSet("help", "h") { text := c.help() write(output, text) return &result{code: Success} } + if c.vals.boolSet("version", "v") { + write(output, c.vstring) + return &result{code: Success} + } + if c.Leaf() && c.Function != nil { code := c.Function(c) if code == Usability { @@ -170,6 +177,7 @@ func (c *Component) run(output io.Writer) *result { cmd.args = c.args cmd.vals = c.vals cmd.globals = c.globals + cmd.vstring = c.vstring cmd.context = c.context return cmd.run(output) } diff --git a/commands_test.go b/commands_test.go index 5a4dd1f..e314186 100644 --- a/commands_test.go +++ b/commands_test.go @@ -4,6 +4,7 @@ package babycli import ( + "bytes" "fmt" "strconv" "strings" @@ -452,6 +453,75 @@ func TestHelp_top(t *testing.T) { must.Eq(t, Failure, code) } +func TestRun_version(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + args []string + top *Component + version string + expText string + }{ + { + name: "long flag", + args: []string{"--version"}, + top: &Component{Name: "myapp"}, + version: "v1.2.3", + expText: "myapp v1.2.3\n", + }, + { + name: "short flag", + args: []string{"-v"}, + top: &Component{Name: "myapp"}, + version: "v1.2.3", + expText: "myapp v1.2.3\n", + }, + { + name: "empty version", + args: []string{"--version"}, + top: &Component{Name: "myapp"}, + version: "", + expText: "myapp\n", + }, + { + name: "empty name", + args: []string{"--version"}, + top: &Component{}, + version: "v1.2.3", + expText: "v1.2.3\n", + }, + { + name: "subcommand", + args: []string{"sub", "--version"}, + top: &Component{ + Name: "myapp", + Components: Components{ + {Name: "sub"}, + }, + }, + version: "v1.2.3", + expText: "myapp v1.2.3\n", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + buf := new(bytes.Buffer) + config := &Configuration{ + Arguments: tc.args, + Top: tc.top, + Version: tc.version, + Output: buf, + } + c := New(config) + result := c.Run() + must.Eq(t, Success, result) + must.Eq(t, tc.expText, buf.String()) + }) + } +} + func TestComponent_GetString(t *testing.T) { t.Parallel() diff --git a/help.go b/help.go index 9709f81..3cc8ea1 100644 --- a/help.go +++ b/help.go @@ -17,6 +17,15 @@ var helpFlag = &Flag{ Help: "print help message", } +var versionFlag = &Flag{ + Type: BooleanFlag, + Require: false, + Repeats: false, + Long: "version", + Short: "v", + Help: "print version", +} + const ( tab = " " ) diff --git a/setup.go b/setup.go index 85d00ee..a07025c 100644 --- a/setup.go +++ b/setup.go @@ -9,6 +9,7 @@ import ( "math" "os" "slices" + "strings" "cattlecloud.net/go/stacks" ) @@ -46,6 +47,7 @@ func New(c *Configuration) *Runnable { slices.Reverse(arguments) c.Top.args = stacks.Simple(arguments...) c.Top.version = c.Version + c.Top.vstring = c.vstring() c.Top.globals = c.globals() c.Top.context = c.context() output := c.Output @@ -66,7 +68,20 @@ func (c *Configuration) context() context.Context { } func (c *Configuration) globals() Flags { - return append(c.Globals, helpFlag) + return append(c.Globals, helpFlag, versionFlag) +} + +func (c *Configuration) vstring() string { + name := strings.TrimSpace(c.Top.Name) + version := strings.TrimSpace(c.Version) + switch { + case version == "": + return name + case name == "": + return version + default: + return name + " " + version + } } type Runnable struct {