Skip to content

Commit

Permalink
SuggestedValuesFn wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Feb 27, 2025
1 parent 7bf4f25 commit e5cc37c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
14 changes: 14 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ ARGS_LOOP:
}
}
}
if lastOpt.SuggestedValuesFn != nil {
for _, e := range lastOpt.SuggestedValuesFn(completionMode, strings.SplitN(iterator.Value(), "=", 2)[1]) {
c := fmt.Sprintf("--%s=%s", k, e)
if strings.HasPrefix(c, iterator.Value()) {
// NOTE: Bash completions have = as a special char and results should be trimmed form the = on.
if completionMode == "bash" {
tc := strings.SplitN(c, "=", 2)[1]
completions = append(completions, tc)
} else {
completions = append(completions, c)
}
}
}
}
}
}
sort.Strings(completions)
Expand Down
6 changes: 6 additions & 0 deletions examples/complex/complete/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package complete

import (
"context"
"fmt"
"io"
"log"
"os"
"strings"

"github.com/DavidGamba/go-getoptions"
Expand All @@ -15,6 +17,10 @@ var Logger = log.New(io.Discard, "log ", log.LstdFlags)
func NewCommand(parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("complete", "Example completions")
opt.SetCommandFn(Run)
opt.String("completeme", "", opt.SuggestedValuesFn(func(target, partial string) []string {
fmt.Fprintf(os.Stderr, "\npartial: %v\n", partial)
return []string{"complete", "completeme", "completeme2"}
}))
opt.ArgCompletions("dev-east", "dev-west", "staging-east", "prod-east", "prod-west", "prod-south")
opt.ArgCompletionsFns(func(target string, prev []string, s string) []string {
if len(prev) == 0 {
Expand Down
9 changes: 7 additions & 2 deletions internal/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var ErrorMissingRequiredOption = errors.New("")
// Handler - Signature for the function that handles saving to the option.
type Handler func(optName string, argument string, usedAlias string) error

// ValueCompletionsFn - Function receiver for custom completions.
// The `target` argument indicates "bash" or "zsh" for the completion targets.
type ValueCompletionsFn func(target string, partialCompletion string) []string

// Type - Indicates the type of option.
type Type int

Expand Down Expand Up @@ -72,8 +76,9 @@ type Option struct {

// SuggestedValues used for completions, suggestions don't necessarily limit
// the values you are able to use
SuggestedValues []string
ValidValues []string // ValidValues that can be passed to Save
SuggestedValues []string
ValidValues []string // ValidValues that can be passed to Save
SuggestedValuesFn ValueCompletionsFn

// Help
DefaultStr string // String representation of default value
Expand Down
7 changes: 7 additions & 0 deletions user_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ func (gopt *GetOpt) SuggestedValues(values ...string) ModifyFn {
}
}

// SuggestedValuesFn - adds a dynamic list of suggestions to the autocompletion for the option.
func (gopt *GetOpt) SuggestedValuesFn(fn option.ValueCompletionsFn) ModifyFn {
return func(parent *GetOpt, opt *option.Option) {
opt.SuggestedValuesFn = fn
}
}

// Called - Indicates if the option was passed on the command line.
// If the `name` is an option that wasn't declared it will return false.
func (gopt *GetOpt) Called(name string) bool {
Expand Down

0 comments on commit e5cc37c

Please sign in to comment.