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

Completion on short option names allows for exploration of short and long option names #228

Merged
merged 3 commits into from
Jul 20, 2017
Merged
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
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 @@ -244,7 +260,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 @@ -257,9 +273,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 @@ -82,7 +83,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