From 70aec47e6b4e68a0638c983bebb295c88482c564 Mon Sep 17 00:00:00 2001 From: rwxrob Date: Thu, 24 Feb 2022 22:25:04 -0500 Subject: [PATCH] Add check.Blank --- check/check.go | 21 +++++++++++++++++++++ check/check_test.go | 27 +++++++++++++++++++++++++++ comp/help.go | 25 +++++++++++++++++++++++++ comp/standard.go | 3 ++- 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 check/check.go create mode 100644 check/check_test.go create mode 100644 comp/help.go diff --git a/check/check.go b/check/check.go new file mode 100644 index 0000000..3492c2a --- /dev/null +++ b/check/check.go @@ -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 +} diff --git a/check/check_test.go b/check/check_test.go new file mode 100644 index 0000000..a557970 --- /dev/null +++ b/check/check_test.go @@ -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 +} diff --git a/comp/help.go b/comp/help.go new file mode 100644 index 0000000..369bff7 --- /dev/null +++ b/comp/help.go @@ -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]) +} diff --git a/comp/standard.go b/comp/standard.go index 08a3959..18deca7 100644 --- a/comp/standard.go +++ b/comp/standard.go @@ -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()} }