Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add filepath as an equivalent to filename which completes into subdirs. #251

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flags

import (
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
Expand Down Expand Up @@ -62,6 +63,11 @@ func completionsWithoutDescriptions(items []string) []Completion {
// prefix.
func (f *Filename) Complete(match string) []Completion {
ret, _ := filepath.Glob(match + "*")
if len(ret) == 1 {
if info, err := os.Stat(ret[0]); err == nil && info.IsDir() {
return f.Complete(ret[0] + "/")
}
}
return completionsWithoutDescriptions(ret)
}

Expand Down
14 changes: 14 additions & 0 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func init() {

completionTestFilename := []string{filepath.Join(completionTestSourcedir, "completion.go"), filepath.Join(completionTestSourcedir, "completion_test.go")}

completionTestSubdir := []string{
filepath.Join(completionTestSourcedir, "examples/add.go"),
filepath.Join(completionTestSourcedir, "examples/bash-completion"),
filepath.Join(completionTestSourcedir, "examples/main.go"),
filepath.Join(completionTestSourcedir, "examples/rm.go"),
}

completionTests = []completionTest{
{
// Short names
Expand Down Expand Up @@ -227,6 +234,13 @@ func init() {
false,
},

{
// Subdirectory
[]string{"rm", "--filename", path.Join(completionTestSourcedir, "examples")},
completionTestSubdir,
false,
},

{
// Custom completed
[]string{"rename", "-c", "hello un"},
Expand Down