From 59e6151c5bb3277462aa4c484af0e6f853df662d Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 23 Aug 2017 13:16:52 -0700 Subject: Allow restricting completion of flags This PR allows a command to specify that flags should only be completed when a prefix is present. The motivation behind this is to have the initial complation to prefer displaying argument completions and only display flag completions when the user enters "hyphen ". --- complete_test.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'complete_test.go') diff --git a/complete_test.go b/complete_test.go index 1a42da1..768f2c8 100644 --- a/complete_test.go +++ b/complete_test.go @@ -25,6 +25,13 @@ func TestCompleter_Complete(t *testing.T) { }, Args: PredictFiles("*.md"), }, + "sub3": { + Flags: Flags{ + "-flag4": PredictAnything, + "-flag5": PredictNothing, + }, + FlagsRequirePrefix: "-", + }, }, Flags: Flags{ "-o": PredictFiles("*.txt"), @@ -41,7 +48,7 @@ func TestCompleter_Complete(t *testing.T) { }{ { args: "", - want: []string{"sub1", "sub2", "-h", "-global1", "-o"}, + want: []string{"sub1", "sub2", "sub3", "-h", "-global1", "-o"}, }, { args: "-", @@ -49,7 +56,7 @@ func TestCompleter_Complete(t *testing.T) { }, { args: "-h ", - want: []string{"sub1", "sub2", "-h", "-global1", "-o"}, + want: []string{"sub1", "sub2", "sub3", "-h", "-global1", "-o"}, }, { args: "-global1 ", // global1 is known follow flag @@ -57,7 +64,7 @@ func TestCompleter_Complete(t *testing.T) { }, { args: "sub", - want: []string{"sub1", "sub2"}, + want: []string{"sub1", "sub2", "sub3"}, }, { args: "sub1", @@ -67,6 +74,10 @@ func TestCompleter_Complete(t *testing.T) { args: "sub2", want: []string{"sub2"}, }, + { + args: "sub3", + want: []string{"sub3"}, + }, { args: "sub1 ", want: []string{"-flag1", "-flag2", "-h", "-global1"}, @@ -107,13 +118,21 @@ func TestCompleter_Complete(t *testing.T) { args: "sub1 -flag2 ", want: []string{"-flag1", "-flag2", "-h", "-global1"}, }, + { + args: "sub3 ", + want: []string{"-h", "-global1"}, + }, + { + args: "sub3 -", + want: []string{"-flag4", "-flag5", "-h", "-global1"}, + }, { args: "-no-such-flag", want: []string{}, }, { args: "-no-such-flag ", - want: []string{"sub1", "sub2", "-h", "-global1", "-o"}, + want: []string{"sub1", "sub2", "sub3", "-h", "-global1", "-o"}, }, { args: "no-such-command", @@ -121,7 +140,7 @@ func TestCompleter_Complete(t *testing.T) { }, { args: "no-such-command ", - want: []string{"sub1", "sub2", "-h", "-global1", "-o"}, + want: []string{"sub1", "sub2", "sub3", "-h", "-global1", "-o"}, }, { args: "-o ", @@ -149,7 +168,7 @@ func TestCompleter_Complete(t *testing.T) { }, { args: "-o ./readme.md ", - want: []string{"sub1", "sub2", "-h", "-global1", "-o"}, + want: []string{"sub1", "sub2", "sub3", "-h", "-global1", "-o"}, }, { args: "-o sub2 -flag3 ", -- cgit v1.2.3