mirror of
https://github.com/rwxrob/bonzai
synced 2024-11-18 15:25:45 +00:00
Add check.Blank
This commit is contained in:
parent
e4dc12e719
commit
70aec47e6b
21
check/check.go
Normal file
21
check/check.go
Normal 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
27
check/check_test.go
Normal 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
25
comp/help.go
Normal 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])
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
package comp
|
||||
|
||||
import (
|
||||
"github.com/rwxrob/bonzai/check"
|
||||
"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
|
||||
if args == nil || len(args) == 0 {
|
||||
if check.Blank(args) {
|
||||
return []string{x.GetName()}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user