Skip to content

Commit

Permalink
Add completions for files and directories
Browse files Browse the repository at this point in the history
  • Loading branch information
bdhess committed Jul 14, 2017
1 parent 3d74450 commit f5590c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
14 changes: 11 additions & 3 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ func TestBashCompletionOptions(t *testing.T) {
return []string{"arg-4-opt-1", "arg-4-opt-2"}
}).String()

four := a.Command("four", "")
four.Arg("arg-5", "").ExistingFileOrDir()
four.Arg("arg-6", "").HintOptions("/usr", "/usr/local").ExistingDir()

five := a.Command("five", "")
five.Flag("flag-5", "").ExistingFile()
five.Command("sub-1", "").Flag("flag-6", "").ExistingDir()

cases := []struct {
Args string
ExpectedDir bool
Expand All @@ -270,7 +278,7 @@ func TestBashCompletionOptions(t *testing.T) {
}{
{
Args: "--completion-bash",
ExpectedWords: []string{"help", "one", "three", "two"},
ExpectedWords: []string{"five", "four", "help", "one", "three", "two"},
},
{
Args: "--completion-bash --",
Expand All @@ -283,7 +291,7 @@ func TestBashCompletionOptions(t *testing.T) {
{
// No options available for flag-0, return to cmd completion
Args: "--completion-bash --flag-0",
ExpectedWords: []string{"help", "one", "three", "two"},
ExpectedWords: []string{"five", "four", "help", "one", "three", "two"},
},
{
Args: "--completion-bash --flag-0 --",
Expand All @@ -299,7 +307,7 @@ func TestBashCompletionOptions(t *testing.T) {
},
{
Args: "--completion-bash --flag-1 opt1",
ExpectedWords: []string{"help", "one", "three", "two"},
ExpectedWords: []string{"five", "four", "help", "one", "three", "two"},
},
{
Args: "--completion-bash --flag-1 opt1 --",
Expand Down
4 changes: 4 additions & 0 deletions clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,20 @@ func (c *Clause) BytesVar(target *units.Base2Bytes) {

// ExistingFile sets the parser to one that requires and returns an existing file.
func (c *Clause) ExistingFileVar(target *string) {
c.builtinCompletion.Files = true
c.SetValue(newExistingFileValue(target))
}

// ExistingDir sets the parser to one that requires and returns an existing directory.
func (c *Clause) ExistingDirVar(target *string) {
c.builtinCompletion.Directories = true
c.SetValue(newExistingDirValue(target))
}

// ExistingDir sets the parser to one that requires and returns an existing directory.
func (c *Clause) ExistingFileOrDirVar(target *string) {
c.builtinCompletion.Directories = true
c.builtinCompletion.Files = true
c.SetValue(newExistingFileOrDirValue(target))
}

Expand Down
1 change: 0 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (c *cmdMixin) CmdCompletion(context *ParseContext) Completion {
options.addWords(cmd.name)
}
}

return options
}

Expand Down

0 comments on commit f5590c9

Please sign in to comment.