Skip to content

Commit

Permalink
Merge pull request #228 from nickwei84/master
Browse files Browse the repository at this point in the history
Completion on short option names allows for exploration of short and long option names
  • Loading branch information
jessevdk committed Jul 20, 2017
2 parents 3153d14 + 7ba6ebf commit 96dc062
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
60 changes: 38 additions & 22 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,51 @@ func (c *completion) skipPositional(s *parseState, n int) {
}
}

func (c *completion) completeOptionNames(names map[string]*Option, prefix string, match string) []Completion {
n := make([]Completion, 0, len(names))
func (c *completion) completeOptionNames(s *parseState, prefix string, match string, short bool) []Completion {
if short && len(match) != 0 {
return []Completion{
Completion{
Item: prefix + match,
},
}
}

for k, opt := range names {
if strings.HasPrefix(k, match) && !opt.Hidden {
n = append(n, Completion{
Item: prefix + k,
var results []Completion
repeats := map[string]bool{}

for name, opt := range s.lookup.longNames {
if strings.HasPrefix(name, match) && !opt.Hidden {
results = append(results, Completion{
Item: defaultLongOptDelimiter + name,
Description: opt.Description,
})

if short {
repeats[string(opt.ShortName)] = true
}
}
}

return n
}
if short {
for name, opt := range s.lookup.shortNames {
if _, exist := repeats[name]; !exist && strings.HasPrefix(name, match) && !opt.Hidden {
results = append(results, Completion{
Item: string(defaultShortOptDelimiter) + name,
Description: opt.Description,
})
}
}
}

func (c *completion) completeLongNames(s *parseState, prefix string, match string) []Completion {
return c.completeOptionNames(s.lookup.longNames, prefix, match)
return results
}

func (c *completion) completeShortNames(s *parseState, prefix string, match string) []Completion {
if len(match) != 0 {
return []Completion{
Completion{
Item: prefix + match,
},
}
}
func (c *completion) completeNamesForLongPrefix(s *parseState, prefix string, match string) []Completion {
return c.completeOptionNames(s, prefix, match, false)
}

return c.completeOptionNames(s.lookup.shortNames, prefix, match)
func (c *completion) completeNamesForShortPrefix(s *parseState, prefix string, match string) []Completion {
return c.completeOptionNames(s, prefix, match, true)
}

func (c *completion) completeCommands(s *parseState, match string) []Completion {
Expand Down Expand Up @@ -237,7 +253,7 @@ func (c *completion) complete(args []string) []Completion {
if opt := s.lookup.shortNames[sname]; opt != nil && opt.canArgument() {
ret = c.completeValue(opt.value, prefix+sname, optname[n:])
} else {
ret = c.completeShortNames(s, prefix, optname)
ret = c.completeNamesForShortPrefix(s, prefix, optname)
}
} else if argument != nil {
if islong {
Expand All @@ -250,9 +266,9 @@ func (c *completion) complete(args []string) []Completion {
ret = c.completeValue(opt.value, prefix+optname+split, *argument)
}
} else if islong {
ret = c.completeLongNames(s, prefix, optname)
ret = c.completeNamesForLongPrefix(s, prefix, optname)
} else {
ret = c.completeShortNames(s, prefix, optname)
ret = c.completeNamesForShortPrefix(s, prefix, optname)
}
} else if len(s.positional) > 0 {
// Complete for positional argument
Expand Down
10 changes: 9 additions & 1 deletion completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (t *TestComplete) Complete(match string) []Completion {
var completionTestOptions struct {
Verbose bool `short:"v" long:"verbose" description:"Verbose messages"`
Debug bool `short:"d" long:"debug" description:"Enable debug"`
Info bool `short:"i" description:"Display info"`
Version bool `long:"version" description:"Show version"`
Required bool `long:"required" required:"true" description:"This is required"`
Hidden bool `long:"hidden" hidden:"true" description:"This is hidden"`
Expand Down Expand Up @@ -87,7 +88,14 @@ func init() {
{
// Short names
[]string{"-"},
[]string{"-d", "-v"},
[]string{"--debug", "--required", "--verbose", "--version", "-i"},
false,
},

{
// Short names full
[]string{"-i"},
[]string{"-i"},
false,
},

Expand Down

0 comments on commit 96dc062

Please sign in to comment.