1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
package prep
// initializes logging and command line options
import (
"fmt"
"os"
"strings"
"go.wit.com/log"
)
func parseArgv(argname string) *Auto {
pb := new(Auto)
pb.Argname = argname
if len(os.Args) == 0 {
return pb
}
if len(os.Args) > 1 && os.Args[1] == "--bash" {
pb.SetupAuto = true
return pb
}
// HACK: set debug flag if --autodebug is passed
for _, s := range os.Args {
if s == "--autodebug" {
pb.Debug = true
}
}
// "complete -C /usr/bin/argv forge" bash ENV values:
// * `COMP_LINE`: A string containing the entire current command line (forge first second third forth ). This is what you are looking for.
// * `COMP_POINT`: A number indicating the cursor's position (index) within the COMP_LINE.
// * `COMP_WORDS`: An array in Bash (seen differently by Go) containing each individual word on the command line.
// * `COMP_CWORD`: A number indicating the index of the word the cursor is currently on within the COMP_WORDS array.
if len(os.Args) > 1 && os.Args[1] == pb.Argname {
pb.IsAuto = true
parts := strings.Split(os.Getenv("COMP_LINE"), " ")
pb.Debug = true
log.Fprintf(os.Stderr, "\n")
pb.Debugf("MATCH Partial os.Args=%v COMP_LINE=%v", os.Args, os.Getenv("COMP_LINE"))
// log.Fprintf(os.Stdout, "jcarr")
if len(parts) > 0 {
pb.Arg0 = parts[0]
}
pb.Arg1 = os.Args[1]
os.Exit(0)
}
if len(os.Args) > 1 && os.Args[1] == "--version" {
if myAuto.buildtime != nil {
// if binary defined buildtime() then process standard version output here
doVersion(pb)
os.Exit(0)
}
}
if len(os.Args) == 0 {
return pb
}
if len(os.Args) == 1 {
pb.Arg0 = os.Args[0]
return pb
}
if os.Args[1] != "--auto-complete" {
pb.Cmd = os.Args[1]
return pb
}
// should we do auto complete here?
if len(os.Args) > 1 && os.Args[1] == "--auto-complete" {
pb.IsAuto = true
pb.Arg0 = os.Args[0]
pb.Arg1 = os.Args[1]
pb.Partial = os.Args[2]
pb.Arg3 = os.Args[3]
if len(os.Args) < 5 {
// the user is doing autocomplete on the command itself
pb.Partial = ""
pb.Cmd = ""
pb.Argv = []string{""}
return pb
}
if pb.Partial == "''" {
pb.Partial = ""
}
// pb.Argv = os.Args[4:]
for _, s := range os.Args[4:] {
if s == "--autodebug" {
continue
}
tmp := strings.Trim(pb.Partial, "'")
if tmp == s {
// don't put pb.Partial into Argv
continue
}
pb.Argv = append(pb.Argv, s)
}
// set pb.Cmd to the first thing that doesn't have a '-' arg
for _, s := range pb.Argv {
if strings.HasPrefix(s, "-") {
continue
}
pb.Cmd = s
break
}
if pb.Partial == "'"+pb.Cmd+"'" {
// not really a command, it's just a partially inputed string from the user
pb.Cmd = ""
}
// try to figure out what the last argv is
for _, s := range pb.Argv {
p := fmt.Sprintf("'%s'", s)
if pb.Partial == p {
pb.Debugf("DEBUG: Last argv MATCHES Partial %s %s", s, p)
continue
} else {
pb.Debugf("DEBUG: Last argv DOES NOT MATCH Partial %s %s", s, p)
}
pb.Last = s
if strings.HasPrefix(s, "-") {
// skip args like -test --verbose when sending subcommands to go-args for help text
continue
}
pb.Goargs = append(pb.Goargs, s)
}
// if pb.Cmd == "" {
// pb.Cmd = strings.Join(pb.Argv, "BLAH")
// }
}
return pb
}
|