summaryrefslogtreecommitdiff
path: root/args.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-11-04 11:20:50 +0200
committerEyal Posener <[email protected]>2017-11-04 11:32:33 +0200
commitaae7e1e39fb3554591e2d20b4c86fea9bf2b15bc (patch)
treef9a8fe2a68d6e28755a3ef8fc6b73f99a06d5574 /args.go
parent88e59760adaddb8276c9b15511302890690e2dae (diff)
Add logic to complete when last flag uses the equal sign
If the last flag is of the form -flag=value, complete the value according to -flag.
Diffstat (limited to 'args.go')
-rw-r--r--args.go43
1 files changed, 35 insertions, 8 deletions
diff --git a/args.go b/args.go
index 73c356d..1ba4d69 100644
--- a/args.go
+++ b/args.go
@@ -3,6 +3,8 @@ package complete
import (
"os"
"path/filepath"
+ "strings"
+ "unicode"
)
// Args describes command line arguments
@@ -37,16 +39,41 @@ func (a Args) Directory() string {
return fixPathForm(a.Last, dir)
}
-func newArgs(line []string) Args {
- completed := removeLast(line[1:])
+func newArgs(line string) Args {
+ var (
+ all []string
+ completed []string
+ )
+ parts := splitFields(line)
+ if len(parts) > 0 {
+ all = parts[1:]
+ completed = removeLast(parts[1:])
+ }
return Args{
- All: line[1:],
+ All: all,
Completed: completed,
- Last: last(line),
+ Last: last(parts),
LastCompleted: last(completed),
}
}
+func splitFields(line string) []string {
+ parts := strings.Fields(line)
+ if len(line) > 0 && unicode.IsSpace(rune(line[len(line)-1])) {
+ parts = append(parts, "")
+ }
+ parts = splitLastEqual(parts)
+ return parts
+}
+
+func splitLastEqual(line []string) []string {
+ if len(line) == 0 {
+ return line
+ }
+ parts := strings.Split(line[len(line)-1], "=")
+ return append(line[:len(line)-1], parts...)
+}
+
func (a Args) from(i int) Args {
if i > len(a.All) {
i = len(a.All)
@@ -67,9 +94,9 @@ func removeLast(a []string) []string {
return a
}
-func last(args []string) (last string) {
- if len(args) > 0 {
- last = args[len(args)-1]
+func last(args []string) string {
+ if len(args) == 0 {
+ return ""
}
- return
+ return args[len(args)-1]
}