Debian Codegraph @

args.go

raw symbols SCIP
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2023 Robin Jarry
3
4 package opt
5
6 import (
7 "errors"
8 "strings"
9 )
10
11 // Shell command line with interpreted arguments.
12 // Allows access to individual arguments and to preserve shell quoting.
13 type Args struct {
14 raw []rune
15 infos []argInfo
16 }
17
18 // Interpret a shell command line into multiple arguments.
19 func LexArgs(cmd string) *Args {
20 raw := []rune(cmd)
21 infos := lexCmdline(raw)
22 return &Args{raw: raw, infos: infos}
23 }
24
25 // Shortcut for LexArgs(cmd).Args()
26 func SplitArgs(cmd string) []string {
27 args := LexArgs(cmd)
28 return args.Args()
29 }
30
31 // Build a shell command from multiple arguments.
32 func QuoteArgs(args ...string) *Args {
33 quoted := make([]string, len(args))
34 for i, arg := range args {
35 quoted[i] = QuoteArg(arg)
36 }
37 return LexArgs(strings.Join(quoted, " "))
38 }
39
40 // Wrap a single argument with appropriate quoting so that it can be used
41 // in a shell command.
42 func QuoteArg(arg string) string {
43 if strings.ContainsAny(arg, " '\"|?&!#$;[](){}<>*\n\t") {
44 // "foo bar" --> "'foo bar'"
45 // "foo'bar" --> "'foo'"'"'bar'"
46 arg = "'" + strings.ReplaceAll(arg, "'", `'"'"'`) + "'"
47 }
48 return arg
49 }
50
51 // Get the number of arguments after interpreting shell quotes.
52 func (a *Args) Count() int {
53 return len(a.infos)
54 }
55
56 func (a *Args) LeadingSpace() string {
57 if len(a.infos) > 0 {
58 first := &a.infos[0]
59 if first.start < len(a.raw) {
60 return string(a.raw[:first.start])
61 }
62 }
63 return ""
64 }
65
66 func (a *Args) TrailingSpace() string {
67 if len(a.infos) > 0 {
68 last := &a.infos[len(a.infos)-1]
69 if last.end < len(a.raw) {
70 return string(a.raw[last.end:])
71 }
72 }
73 return ""
74 }
75
76 var ErrArgIndex = errors.New("argument index out of bounds")
77
78 // Remove n arguments from the beginning of the command line.
79 // Same semantics as the `shift` built-in shell command.
80 // Will fail if shifting an invalid number of arguments.
81 func (a *Args) ShiftSafe(n int) ([]string, error) {
82 var shifted []string
83 switch {
84 case n == 0:
85 shifted = []string{}
86 case n > 0 && n < len(a.infos):
87 for i := 0; i < n; i++ {
88 shifted = append(shifted, a.infos[i].unquoted)
89 }
90 a.infos = a.infos[n:]
91 start := a.infos[0].start
92 a.raw = a.raw[start:]
93 for i := range a.infos {
94 a.infos[i].start -= start
95 a.infos[i].end -= start
96 }
97 case n == len(a.infos):
98 for i := 0; i < n; i++ {
99 shifted = append(shifted, a.infos[i].unquoted)
100 }
101 a.raw = nil
102 a.infos = nil
103 default:
104 return nil, ErrArgIndex
105 }
106 return shifted, nil
107 }
108
109 // Same as ShiftSafe but cannot fail.
110 func (a *Args) Shift(n int) []string {
111 if n < 0 {
112 n = 0
113 } else if n > len(a.infos) {
114 n = len(a.infos)
115 }
116 shifted, _ := a.ShiftSafe(n)
117 return shifted
118 }
119
120 // Remove n arguments from the end of the command line.
121 // Will fail if cutting an invalid number of arguments.
122 func (a *Args) CutSafe(n int) ([]string, error) {
123 var cut []string
124 switch {
125 case n == 0:
126 cut = []string{}
127 case n > 0 && n < len(a.infos):
128 for i := len(a.infos) - n; i < len(a.infos); i++ {
129 cut = append(cut, a.infos[i].unquoted)
130 }
131 a.infos = a.infos[:len(a.infos)-n]
132 a.raw = a.raw[:a.infos[len(a.infos)-1].end]
133 case n == len(a.infos):
134 for i := 0; i < n; i++ {
135 cut = append(cut, a.infos[i].unquoted)
136 }
137 a.raw = nil
138 a.infos = nil
139 default:
140 return nil, ErrArgIndex
141 }
142 return cut, nil
143 }
144
145 // Same as CutSafe but cannot fail.
146 func (a *Args) Cut(n int) []string {
147 if n < 0 {
148 n = 0
149 } else if n > len(a.infos) {
150 n = len(a.infos)
151 }
152 cut, _ := a.CutSafe(n)
153 return cut
154 }
155
156 // Insert the specified prefix at the beginning of the command line.
157 func (a *Args) Prepend(cmd string) {
158 prefix := []rune(cmd)
159 infos := lexCmdline(prefix)
160 if len(infos) > 0 && infos[len(infos)-1].end == len(prefix) {
161 // No trailing white space to the added command line.
162 // Add one space manually.
163 prefix = append(prefix, ' ')
164 }
165 for i := range a.infos {
166 a.infos[i].start += len(prefix)
167 a.infos[i].end += len(prefix)
168 }
169 a.raw = append(prefix, a.raw...)
170 a.infos = append(infos, a.infos...)
171 }
172
173 // Extend the command line with more arguments.
174 func (a *Args) Extend(cmd string) {
175 suffix := []rune(cmd)
176 infos := lexCmdline(suffix)
177 if len(infos) > 0 && infos[0].start == 0 {
178 // No leading white space to the added command line.
179 // Add one space manually.
180 a.raw = append(a.raw, ' ')
181 }
182 for i := range infos {
183 infos[i].start += len(a.raw)
184 infos[i].end += len(a.raw)
185 }
186 a.raw = append(a.raw, suffix...)
187 a.infos = append(a.infos, infos...)
188 }
189
190 // Get the nth argument after interpreting shell quotes.
191 func (a *Args) ArgSafe(n int) (string, error) {
192 if n < 0 || n >= len(a.infos) {
193 return "", ErrArgIndex
194 }
195 return a.infos[n].unquoted, nil
196 }
197
198 // Get the nth argument after interpreting shell quotes.
199 // Will panic if the argument index does not exist.
200 func (a *Args) Arg(n int) string {
201 return a.infos[n].unquoted
202 }
203
204 // Get all arguments after interpreting shell quotes.
205 func (a *Args) Args() []string {
206 args := make([]string, 0, len(a.infos))
207 for i := range a.infos {
208 args = append(args, a.infos[i].unquoted)
209 }
210 return args
211 }
212
213 // Get the raw command line, with uninterpreted shell quotes.
214 func (a *Args) String() string {
215 return string(a.raw)
216 }
217
218 // Make a deep copy of an Args object.
219 func (a *Args) Clone() *Args {
220 infos := make([]argInfo, len(a.infos))
221 copy(infos, a.infos)
222 raw := make([]rune, len(a.raw))
223 copy(raw, a.raw)
224 return &Args{raw: raw, infos: infos}
225 }