shellSplit tokenizes a command line in a POSIX-ish way: whitespace separates tokens, double-quotes group, single-quotes group (no expansion inside), backslash escapes the next rune outside single quotes. Stops at the first unquoted pipeline boundary (`|`, `;`, `&`, `>`, `<`) so the validator only ch
(s string)
| 157 | // to be runnable invocations of one CLI command (possibly piped to |
| 158 | // another tool — that other tool is the user's shell, not pad's). |
| 159 | func shellSplit(s string) ([]string, error) { |
| 160 | var tokens []string |
| 161 | var cur strings.Builder |
| 162 | var inDQuote, inSQuote, escape bool |
| 163 | flush := func() { |
| 164 | if cur.Len() > 0 { |
| 165 | tokens = append(tokens, cur.String()) |
| 166 | cur.Reset() |
| 167 | } |
| 168 | } |
| 169 | for _, r := range s { |
| 170 | switch { |
| 171 | case escape: |
| 172 | cur.WriteRune(r) |
| 173 | escape = false |
| 174 | case r == '\\' && !inSQuote: |
| 175 | escape = true |
| 176 | case r == '"' && !inSQuote: |
| 177 | inDQuote = !inDQuote |
| 178 | case r == '\'' && !inDQuote: |
| 179 | inSQuote = !inSQuote |
| 180 | case (r == ' ' || r == '\t') && !inDQuote && !inSQuote: |
| 181 | flush() |
| 182 | case (r == '|' || r == ';' || r == '&' || r == '>' || r == '<') && !inDQuote && !inSQuote: |
| 183 | // Pipeline boundary — return what we have so far. The rest |
| 184 | // of the string belongs to a different command (or the |
| 185 | // shell), which is out of scope for cmdhelp validation. |
| 186 | flush() |
| 187 | return tokens, nil |
| 188 | default: |
| 189 | cur.WriteRune(r) |
| 190 | } |
| 191 | } |
| 192 | if inDQuote || inSQuote { |
| 193 | return nil, fmt.Errorf("unterminated quote") |
| 194 | } |
| 195 | if escape { |
| 196 | return nil, fmt.Errorf("trailing backslash") |
| 197 | } |
| 198 | flush() |
| 199 | return tokens, nil |
| 200 | } |
| 201 | |
| 202 | // ValidateBoolArity asserts that every bool-typed flag in doc obeys |
| 203 | // spec §5.3: bool flags MUST be presence switches. They MUST NOT |