~rjarry/go-opt

c8dfe344895563cb82cfad019a9fbd6f0949e8a1 — Robin Jarry 2 months ago 476a718 main v2.0.1
completion: return descriptions from callback values

When an option has a completion callback, include the option description
along with the items returned by the callback.

Signed-off-by: Robin Jarry <robin@jarry.cc>
2 files changed, 21 insertions(+), 18 deletions(-)

M complete.go
M complete_test.go
M complete.go => complete.go +4 -1
@@ 58,7 58,10 @@ func (s *optSpec) getCompletions(arg string) []Completion {
		if res, ok := out[0].Interface().([]string); ok {
			var completions []Completion
			for _, value := range res {
				completions = append(completions, Completion{Value: value})
				completions = append(completions, Completion{
					Value:       value,
					Description: s.description,
				})
			}
			return completions
		}

M complete_test.go => complete_test.go +17 -17
@@ 9,7 9,7 @@ import (
)

type CompleteStruct struct {
	Name    string   `opt:"-n,--name" required:"true" complete:"CompleteName"`
	Name    string   `opt:"-n,--name" required:"true" complete:"CompleteName" desc:"Ninja turtle name."`
	Delay   float64  `opt:"--delay"`
	Zero    bool     `opt:"-z" description:"Print zero values"`
	Backoff bool     `opt:"-B,--backoff" desc:"Increase delay on error"`


@@ 52,42 52,42 @@ func TestComplete(t *testing.T) {
	}{
		{
			"foo --delay 33..33.3 -n",
			[]opt.Completion{{Value: "-n "}},
			[]opt.Completion{{Value: "-n ", Description: "Ninja turtle name."}},
			"foo --delay 33..33.3 ",
		},
		{
			"foo --delay 33..33.3 -n ",
			[]opt.Completion{
				{Value: "leonardo"},
				{Value: "michelangelo"},
				{Value: "rafaelo"},
				{Value: "donatello"},
				{Value: "leonardo", Description: "Ninja turtle name."},
				{Value: "michelangelo", Description: "Ninja turtle name."},
				{Value: "rafaelo", Description: "Ninja turtle name."},
				{Value: "donatello", Description: "Ninja turtle name."},
			},
			"foo --delay 33..33.3 -n ",
		},
		{
			"foo --delay 33..33.3 -n don",
			[]opt.Completion{{Value: "donatello"}},
			[]opt.Completion{{Value: "donatello", Description: "Ninja turtle name."}},
			"foo --delay 33..33.3 -n ",
		},
		{
			"foo --delay 33..33.3 --name=",
			[]opt.Completion{
				{Value: "leonardo"},
				{Value: "michelangelo"},
				{Value: "rafaelo"},
				{Value: "donatello"},
				{Value: "leonardo", Description: "Ninja turtle name."},
				{Value: "michelangelo", Description: "Ninja turtle name."},
				{Value: "rafaelo", Description: "Ninja turtle name."},
				{Value: "donatello", Description: "Ninja turtle name."},
			},
			"foo --delay 33..33.3 --name=",
		},
		{
			"foo --delay 33..33.3 --name=leo",
			[]opt.Completion{{Value: "leonardo"}},
			[]opt.Completion{{Value: "leonardo", Description: "Ninja turtle name."}},
			"foo --delay 33..33.3 --name=",
		},
		{
			"foo --nam",
			[]opt.Completion{{Value: "--name "}},
			[]opt.Completion{{Value: "--name ", Description: "Ninja turtle name."}},
			"foo ",
		},
		{


@@ 103,8 103,8 @@ func TestComplete(t *testing.T) {
				{Value: "-important"},
				{Value: "-inbox"},
				{Value: "-trash"},
				{Value: "-n "},
				{Value: "--name "},
				{Value: "-n ", Description: "Ninja turtle name."},
				{Value: "--name ", Description: "Ninja turtle name."},
				{Value: "-z ", Description: "Print zero values"},
				{Value: "-B ", Description: "Increase delay on error"},
				{Value: "--backoff ", Description: "Increase delay on error"},


@@ 119,8 119,8 @@ func TestComplete(t *testing.T) {
				{Value: "important"},
				{Value: "inbox"},
				{Value: "trash"},
				{Value: "-n "},
				{Value: "--name "},
				{Value: "-n ", Description: "Ninja turtle name."},
				{Value: "--name ", Description: "Ninja turtle name."},
				{Value: "-z ", Description: "Print zero values"},
				{Value: "-B ", Description: "Increase delay on error"},
				{Value: "--backoff ", Description: "Increase delay on error"},