Skip to content
New issue

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

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

Already on GitHub? Sign in 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 1 commit
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
16 changes: 16 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 @@ -48,6 +49,9 @@ type completion struct {
// Filename is a string alias which provides filename completion.
type Filename string

// Filepath is a string alias which provides completion for file paths into subdirectories.
type Filepath string

func completionsWithoutDescriptions(items []string) []Completion {
ret := make([]Completion, len(items))

Expand All @@ -65,6 +69,18 @@ func (f *Filename) Complete(match string) []Completion {
return completionsWithoutDescriptions(ret)
}

// Complete returns a list of existing files with the given prefix.
// If it completes to a single directory, the contents of that directory are interrogated.
func (f *Filepath) Complete(match string) []Completion {
ret, _ := filepath.Glob(match + "*")
if len(ret) == 1 {
if info, err := os.Stat(ret[0]); err == nil && info.IsDir() {
ret, _ = filepath.Glob(ret[0] + "/*")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there is only one file that is a dir here too? I think this should just recurse? Since this has been waiting so long, I'll make the change. Thanks for contributing!!

}
}
return completionsWithoutDescriptions(ret)
}

func (c *completion) skipPositional(s *parseState, n int) {
if n >= len(s.positional) {
s.positional = nil
Expand Down
22 changes: 22 additions & 0 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var completionTestOptions struct {
RemoveCommand struct {
Other bool `short:"o"`
File Filename `short:"f" long:"filename"`
Dir Filepath `short:"d" long:"dir"`
} `command:"rm" description:"remove an item"`

RenameCommand struct {
Expand All @@ -84,6 +85,13 @@ func init() {

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

completionTestFilepath := []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 +235,20 @@ func init() {
false,
},

{
// Flag filepath file
[]string{"rm", "-d", path.Join(completionTestSourcedir, "completion")},
completionTestFilename,
false,
},

{
// Flag filepath dir
[]string{"rm", "-d", path.Join(completionTestSourcedir, "examples")},
completionTestFilepath,
false,
},

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