Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,6 +93,8 @@ type Component struct {

version string

vstring string

context context.Context
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
70 changes: 70 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package babycli

import (
"bytes"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 9 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " "
)
Expand Down
17 changes: 16 additions & 1 deletion setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"os"
"slices"
"strings"

"cattlecloud.net/go/stacks"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading