Skip to content

Commit

Permalink
fixup + mid-line bash complete
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanndickson committed Jul 4, 2024
1 parent d910fec commit 707afa6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 31 deletions.
21 changes: 0 additions & 21 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,27 +563,6 @@ func findArg(want string, args []string, fs *pflag.FlagSet) (int, error) {
return -1, xerrors.Errorf("arg %s not found", want)
}

// // findArgByPos returns the index of first full word before the given cursor position in the arguments
// // list. If the cursor is at the end of the line, the last word is returned.
// func findArgByPos(pos int, args []string) int {
// if pos == 0 {
// return -1
// }
// if len(args) == 0 {
// return -1
// }
// curChar := 0
// for i, arg := range args {
// next := curChar + len(arg)
// if pos <= next {
// return i
// }
// curChar = next + 1
// }
// // Otherwise, must be the last word
// return len(args)
// }

// Run executes the command.
// If two command share a flag name, the first command wins.
//
Expand Down
4 changes: 2 additions & 2 deletions completion/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

const bashCompletionTemplate = `
_generate_{{.Name}}_completions() {
# Capture the full command line as an array, excluding the first element (the command itself)
local args=("${COMP_WORDS[@]:1}")
# Capture the line excluding the command, and everything after the current word
local args=("${COMP_WORDS[@]:1:COMP_CWORD}")
# Set COMPLETION_MODE and call the command with the arguments, capturing the output
local completions=$(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}")
Expand Down
15 changes: 7 additions & 8 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,18 @@ func (optSet *OptionSet) SetDefaults() error {

// ByName returns the Option with the given name, or nil if no such option
// exists.
func (optSet *OptionSet) ByName(name string) *Option {
for i := range *optSet {
opt := &(*optSet)[i]
if opt.Name == name {
return opt
func (optSet OptionSet) ByName(name string) *Option {
for i := range optSet {
if optSet[i].Name == name {
return &optSet[i]
}
}
return nil
}

func (optSet *OptionSet) ByFlag(flag string) *Option {
for i := range *optSet {
opt := &(*optSet)[i]
func (optSet OptionSet) ByFlag(flag string) *Option {
for i := range optSet {
opt := &optSet[i]
if opt.Flag == flag {
return opt
}
Expand Down

0 comments on commit 707afa6

Please # to comment.