Implement verbose completion
Add tests for verbose completion and document it
diff --git a/completion.go b/completion.go
index 76a2808..cb7aed6 100644
--- a/completion.go
+++ b/completion.go
@@ -44,7 +44,7 @@
type completion struct {
parser *Parser
- ShowDescriptions bool `short:"d" long:"show-descriptions" description:"Show descriptions next to completion items"`
+ ShowDescriptions bool
}
// Filename is a string alias which provides filename completion.
diff --git a/completion_test.go b/completion_test.go
index 7dc916c..2d5a97f 100644
--- a/completion_test.go
+++ b/completion_test.go
@@ -36,36 +36,37 @@
}
var completionTestOptions struct {
- Verbose bool `short:"v" long:"verbose"`
- Debug bool `short:"d" long:"debug"`
- Version bool `long:"version"`
- Required bool `long:"required" required:"true"`
+ Verbose bool `short:"v" long:"verbose" description:"Verbose messages"`
+ Debug bool `short:"d" long:"debug" description:"Enable debug"`
+ Version bool `long:"version" description:"Show version"`
+ Required bool `long:"required" required:"true" description:"This is required"`
AddCommand struct {
Positional struct {
Filename Filename
} `positional-args:"yes"`
- } `command:"add"`
+ } `command:"add" description:"add an item"`
AddMultiCommand struct {
Positional struct {
Filename []Filename
} `positional-args:"yes"`
- } `command:"add-multi"`
+ } `command:"add-multi" description:"add multiple items"`
RemoveCommand struct {
Other bool `short:"o"`
File Filename `short:"f" long:"filename"`
- } `command:"rm"`
+ } `command:"rm" description:"remove an item"`
RenameCommand struct {
Completed TestComplete `short:"c" long:"completed"`
- } `command:"rename"`
+ } `command:"rename" description:"rename an item"`
}
type completionTest struct {
- Args []string
- Completed []string
+ Args []string
+ Completed []string
+ ShowDescriptions bool
}
var completionTests []completionTest
@@ -81,100 +82,141 @@
// Short names
[]string{"-"},
[]string{"-d", "-v"},
+ false,
},
{
// Short names concatenated
[]string{"-dv"},
[]string{"-dv"},
+ false,
},
{
// Long names
[]string{"--"},
[]string{"--debug", "--required", "--verbose", "--version"},
+ false,
+ },
+
+ {
+ // Long names with descriptions
+ []string{"--"},
+ []string{
+ "--debug # Enable debug",
+ "--required # This is required",
+ "--verbose # Verbose messages",
+ "--version # Show version",
+ },
+ true,
},
{
// Long names partial
[]string{"--ver"},
[]string{"--verbose", "--version"},
+ false,
},
{
// Commands
[]string{""},
[]string{"add", "add-multi", "rename", "rm"},
+ false,
+ },
+
+ {
+ // Commands with descriptions
+ []string{""},
+ []string{
+ "add # add an item",
+ "add-multi # add multiple items",
+ "rename # rename an item",
+ "rm # remove an item",
+ },
+ true,
},
{
// Commands partial
[]string{"r"},
[]string{"rename", "rm"},
+ false,
},
{
// Positional filename
[]string{"add", filepath.Join(completionTestSourcedir, "completion")},
completionTestFilename,
+ false,
},
{
// Multiple positional filename (1 arg)
[]string{"add-multi", filepath.Join(completionTestSourcedir, "completion")},
completionTestFilename,
+ false,
},
{
// Multiple positional filename (2 args)
[]string{"add-multi", filepath.Join(completionTestSourcedir, "completion.go"), filepath.Join(completionTestSourcedir, "completion")},
completionTestFilename,
+ false,
},
{
// Multiple positional filename (3 args)
[]string{"add-multi", filepath.Join(completionTestSourcedir, "completion.go"), filepath.Join(completionTestSourcedir, "completion.go"), filepath.Join(completionTestSourcedir, "completion")},
completionTestFilename,
+ false,
},
{
// Flag filename
[]string{"rm", "-f", path.Join(completionTestSourcedir, "completion")},
completionTestFilename,
+ false,
},
{
// Flag short concat last filename
[]string{"rm", "-of", path.Join(completionTestSourcedir, "completion")},
completionTestFilename,
+ false,
},
{
// Flag concat filename
[]string{"rm", "-f" + path.Join(completionTestSourcedir, "completion")},
[]string{"-f" + completionTestFilename[0], "-f" + completionTestFilename[1]},
+ false,
},
{
// Flag equal concat filename
[]string{"rm", "-f=" + path.Join(completionTestSourcedir, "completion")},
[]string{"-f=" + completionTestFilename[0], "-f=" + completionTestFilename[1]},
+ false,
},
{
// Flag concat long filename
[]string{"rm", "--filename=" + path.Join(completionTestSourcedir, "completion")},
[]string{"--filename=" + completionTestFilename[0], "--filename=" + completionTestFilename[1]},
+ false,
},
{
// Flag long filename
[]string{"rm", "--filename", path.Join(completionTestSourcedir, "completion")},
completionTestFilename,
+ false,
},
{
// Custom completed
[]string{"rename", "-c", "hello un"},
[]string{"hello universe"},
+ false,
},
}
}
@@ -184,6 +226,10 @@
c := &completion{parser: p}
for _, test := range completionTests {
+ if test.ShowDescriptions {
+ continue
+ }
+
ret := c.complete(test.Args)
items := make([]string, len(ret))
@@ -192,15 +238,19 @@
}
if !reflect.DeepEqual(items, test.Completed) {
- t.Errorf("Args: %#v\n Expected: %#v\n Got: %#v", test.Args, test.Completed, items)
+ t.Errorf("Args: %#v, %#v\n Expected: %#v\n Got: %#v", test.Args, test.ShowDescriptions, test.Completed, items)
}
}
}
func TestParserCompletion(t *testing.T) {
- os.Setenv("GO_FLAGS_COMPLETION", "1")
-
for _, test := range completionTests {
+ if test.ShowDescriptions {
+ os.Setenv("GO_FLAGS_COMPLETION", "verbose")
+ } else {
+ os.Setenv("GO_FLAGS_COMPLETION", "1")
+ }
+
tmp := os.Stdout
r, w, _ := os.Pipe()
diff --git a/flags.go b/flags.go
index a425c11..e3e72a3 100644
--- a/flags.go
+++ b/flags.go
@@ -205,7 +205,9 @@
where `completion-example` is the binary, `arg1` and `arg2` are
the current arguments, and `arg3` (the last argument) is the argument
-to be completed.
+to be completed. If the GO_FLAGS_COMPLETION is set to "verbose", then
+descriptions of possible completion items will also be shown, if there
+are more than 1 completion items.
To use this with bash completion, a simple file can be written which
calls the binary which supports go-flags completion:
diff --git a/parser.go b/parser.go
index 0ad010d..5a8ec92 100644
--- a/parser.go
+++ b/parser.go
@@ -148,9 +148,15 @@
p.addHelpGroups(p.showBuiltinHelp)
}
- if len(os.Getenv("GO_FLAGS_COMPLETION")) != 0 {
+ compval := os.Getenv("GO_FLAGS_COMPLETION")
+
+ if len(compval) != 0 {
comp := &completion{parser: p}
+ if compval == "verbose" {
+ comp.ShowDescriptions = true
+ }
+
comp.execute(args)
return nil, nil