Add check.Blank

This commit is contained in:
rwxrob 2022-02-24 22:25:04 -05:00
parent e4dc12e719
commit 70aec47e6b
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77
4 changed files with 75 additions and 1 deletions

21
check/check.go Normal file
View File

@ -0,0 +1,21 @@
package check
// Blank checks that the passed string, []byte (or slice of either)
// contains something besides an empty string.
func Blank(i interface{}) bool {
switch v := i.(type) {
case nil:
return false
case string:
return v != ""
case []byte:
return string(v) != ""
case []string:
return v != nil && len(v) != 0 && v[0] != ""
case [][]byte:
return v != nil && len(v) != 0 && string(v[0]) != ""
default:
panic("cannot check if type is blank")
}
return false
}

27
check/check_test.go Normal file
View File

@ -0,0 +1,27 @@
package check_test
import (
"fmt"
"github.com/rwxrob/bonzai/check"
)
func ExampleBlank() {
fmt.Println(check.Blank(""))
fmt.Println(check.Blank(nil))
fmt.Println(check.Blank([]string{""}))
fmt.Println(check.Blank([][]byte{}))
// and now for true
fmt.Println(check.Blank("some"))
fmt.Println(check.Blank([]string{"some"}))
fmt.Println(check.Blank([][]byte{{'a'}}))
// Output:
// false
// false
// false
// false
// true
// true
// true
}

25
comp/help.go Normal file
View File

@ -0,0 +1,25 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package comp
import (
"github.com/rwxrob/bonzai/check"
"github.com/rwxrob/bonzai/filter"
)
func Help(x Command, args ...string) []string {
// check for unique first argument command
if check.Blank(args) {
return []string{x.GetName()}
}
// build list of params and other keys
list := []string{}
list = append(list, x.GetParams()...)
// FIXME GetCaller nil check
list = append(list, filter.Keys(x.GetCaller().GetOther())...)
return filter.HasPrefix(list, args[0])
}

View File

@ -4,6 +4,7 @@
package comp package comp
import ( import (
"github.com/rwxrob/bonzai/check"
"github.com/rwxrob/bonzai/filter" "github.com/rwxrob/bonzai/filter"
) )
@ -28,7 +29,7 @@ func Standard(x Command, args ...string) []string {
} }
// not sure we've completed the command name itself yet // not sure we've completed the command name itself yet
if args == nil || len(args) == 0 { if check.Blank(args) {
return []string{x.GetName()} return []string{x.GetName()}
} }