summaryrefslogtreecommitdiff
path: root/flags_test.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2019-11-14 06:51:44 +0200
committerEyal Posener <[email protected]>2019-11-18 01:05:47 +0200
commit8724aaf18312e54750540a9578e00d61b1c545d8 (patch)
treed3e736b4fb279975bbcc017ae1bad53e454c5773 /flags_test.go
parent05b68ffc813dd10c420993cb1cf927b346c057b8 (diff)
V2
Diffstat (limited to 'flags_test.go')
-rw-r--r--flags_test.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/flags_test.go b/flags_test.go
new file mode 100644
index 0000000..374a6cc
--- /dev/null
+++ b/flags_test.go
@@ -0,0 +1,57 @@
+package complete
+
+import (
+ "flag"
+ "fmt"
+ "strconv"
+ "testing"
+)
+
+func TestFlags(t *testing.T) {
+ t.Parallel()
+
+ var (
+ tr boolValue = true
+ fl boolValue = false
+ )
+
+ fs := flag.NewFlagSet("test", flag.ExitOnError)
+ fs.Var(&tr, "foo", "")
+ fs.Var(&fl, "bar", "")
+ fs.String("foo-bar", "", "")
+ cmp := FlagSet(fs)
+
+ Test(t, cmp, "", []string{"-foo", "-bar", "-foo-bar", "-h"})
+ Test(t, cmp, "-foo", []string{"-foo", "-foo-bar"})
+ Test(t, cmp, "-foo ", []string{"false"})
+ Test(t, cmp, "-foo=", []string{"false"})
+ Test(t, cmp, "-bar ", []string{"-foo", "-bar", "-foo-bar", "-h"})
+ Test(t, cmp, "-bar=", []string{})
+}
+
+type boolValue bool
+
+func (b *boolValue) Set(s string) error {
+ v, err := strconv.ParseBool(s)
+ if err != nil {
+ return fmt.Errorf("bad value %q for bool flag", s)
+ }
+ *b = boolValue(v)
+ return nil
+}
+
+func (b *boolValue) Get() interface{} { return bool(*b) }
+
+func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
+
+func (b *boolValue) IsBoolFlag() bool { return true }
+
+func (b *boolValue) Predict(_ string) []string {
+ // If false, typing the bool flag is expected to turn it on, so there is nothing to complete
+ // after the flag.
+ if *b == false {
+ return nil
+ }
+ // Otherwise, suggest only to turn it off.
+ return []string{"false"}
+}