Merge pull request #47 from dadgar/f-hide-flags

Allow restricting completion of flags
This commit is contained in:
Eyal Posener 2017-08-25 09:44:15 +03:00 committed by GitHub
commit 2100d1b06c
2 changed files with 34 additions and 10 deletions

View File

@ -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)
}

View File

@ -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"},
},
}