summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--command.go8
-rw-r--r--complete_test.go36
2 files changed, 34 insertions, 10 deletions
diff --git a/command.go b/command.go
index 9614269..6de48e9 100644
--- a/command.go
+++ b/command.go
@@ -49,6 +49,14 @@ type Flags map[string]Predictor
// Predict completion of flags names according to command line arguments
func (f Flags) Predict(a Args) (prediction []string) {
for flag := range f {
+ // If the flag starts with a hyphen, we avoid emitting the prediction
+ // unless the last typed arg contains a hyphen as well.
+ flagHyphenStart := len(flag) != 0 && flag[0] == '-'
+ lastHyphenStart := len(a.Last) != 0 && a.Last[0] == '-'
+ if flagHyphenStart && !lastHyphenStart {
+ continue
+ }
+
if match.Prefix(flag, a.Last) {
prediction = append(prediction, flag)
}
diff --git a/complete_test.go b/complete_test.go
index 1a42da1..ba4df4a 100644
--- a/complete_test.go
+++ b/complete_test.go
@@ -41,7 +41,7 @@ func TestCompleter_Complete(t *testing.T) {
}{
{
args: "",
- want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
+ want: []string{"sub1", "sub2"},
},
{
args: "-",
@@ -49,7 +49,7 @@ func TestCompleter_Complete(t *testing.T) {
},
{
args: "-h ",
- want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
+ want: []string{"sub1", "sub2"},
},
{
args: "-global1 ", // global1 is known follow flag
@@ -69,11 +69,15 @@ func TestCompleter_Complete(t *testing.T) {
},
{
args: "sub1 ",
+ want: []string{},
+ },
+ {
+ args: "sub1 -",
want: []string{"-flag1", "-flag2", "-h", "-global1"},
},
{
args: "sub2 ",
- want: []string{"./", "dir/", "outer/", "readme.md", "-flag2", "-flag3", "-h", "-global1"},
+ want: []string{"./", "dir/", "outer/", "readme.md"},
},
{
args: "sub2 ./",
@@ -89,7 +93,7 @@ func TestCompleter_Complete(t *testing.T) {
},
{
args: "sub2 -flag2 ",
- want: []string{"./", "dir/", "outer/", "readme.md", "-flag2", "-flag3", "-h", "-global1"},
+ want: []string{"./", "dir/", "outer/", "readme.md"},
},
{
args: "sub1 -fl",
@@ -104,7 +108,7 @@ func TestCompleter_Complete(t *testing.T) {
want: []string{}, // flag1 is unknown follow flag
},
{
- args: "sub1 -flag2 ",
+ args: "sub1 -flag2 -",
want: []string{"-flag1", "-flag2", "-h", "-global1"},
},
{
@@ -113,7 +117,11 @@ func TestCompleter_Complete(t *testing.T) {
},
{
args: "-no-such-flag ",
- want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
+ want: []string{"sub1", "sub2"},
+ },
+ {
+ args: "-no-such-flag -",
+ want: []string{"-h", "-global1", "-o"},
},
{
args: "no-such-command",
@@ -121,7 +129,7 @@ func TestCompleter_Complete(t *testing.T) {
},
{
args: "no-such-command ",
- want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
+ want: []string{"sub1", "sub2"},
},
{
args: "-o ",
@@ -149,7 +157,7 @@ func TestCompleter_Complete(t *testing.T) {
},
{
args: "-o ./readme.md ",
- want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
+ want: []string{"sub1", "sub2"},
},
{
args: "-o sub2 -flag3 ",
@@ -220,7 +228,7 @@ func TestCompleter_Complete_SharedPrefix(t *testing.T) {
}{
{
args: "",
- want: []string{"status", "job", "-h", "-global1", "-o"},
+ want: []string{"status", "job"},
},
{
args: "-",
@@ -232,10 +240,18 @@ func TestCompleter_Complete_SharedPrefix(t *testing.T) {
},
{
args: "job ",
- want: []string{"-h", "-global1", "status"},
+ want: []string{"status"},
+ },
+ {
+ args: "job -",
+ want: []string{"-h", "-global1"},
},
{
args: "job status ",
+ want: []string{},
+ },
+ {
+ args: "job status -",
want: []string{"-f4", "-h", "-global1"},
},
}