|
1 | 1 | package shellshape |
2 | 2 |
|
| 3 | +import "strings" |
| 4 | + |
3 | 5 | // handlerFunc processes tokens after the executable name. |
4 | 6 | // subcommand is the subcommand token (empty if none). |
5 | 7 | type handlerFunc func(subcommand string, tokens []string) []string |
6 | 8 |
|
| 9 | +// flagCategory pairs a set of flag names with the placeholder to use for their values. |
| 10 | +type flagCategory struct { |
| 11 | + flags map[string]bool |
| 12 | + placeholder string |
| 13 | +} |
| 14 | + |
| 15 | +// consumeFlagArg appends a flag and its next-token value (collapsed to placeholder) |
| 16 | +// to result. Subshell tokens are preserved verbatim. |
| 17 | +func consumeFlagArg(tok string, args []string, i int, result []string, placeholder string) ([]string, int) { |
| 18 | + result = append(result, tok) |
| 19 | + i++ |
| 20 | + if i < len(args) { |
| 21 | + if isSubshellToken(args[i]) { |
| 22 | + result = append(result, args[i]) |
| 23 | + } else { |
| 24 | + result = append(result, placeholder) |
| 25 | + } |
| 26 | + i++ |
| 27 | + } |
| 28 | + return result, i |
| 29 | +} |
| 30 | + |
| 31 | +// matchFlagCategory checks tok against a list of flag categories and returns |
| 32 | +// the matching placeholder. Returns ("", false) if no category matches. |
| 33 | +func matchFlagCategory(tok string, categories []flagCategory) (string, bool) { |
| 34 | + for _, cat := range categories { |
| 35 | + if cat.flags[tok] { |
| 36 | + return cat.placeholder, true |
| 37 | + } |
| 38 | + } |
| 39 | + return "", false |
| 40 | +} |
| 41 | + |
| 42 | +// consumeFusedFlag handles --flag=value syntax against a list of flag categories. |
| 43 | +// Returns the normalized "flag=<placeholder>" string and true if matched, |
| 44 | +// or ("", false) if the token doesn't contain '=' or doesn't match any category. |
| 45 | +func consumeFusedFlag(tok string, categories []flagCategory) (string, bool) { |
| 46 | + eqIdx := strings.IndexByte(tok, '=') |
| 47 | + if eqIdx < 0 { |
| 48 | + return "", false |
| 49 | + } |
| 50 | + key := tok[:eqIdx] |
| 51 | + for _, cat := range categories { |
| 52 | + if cat.flags[key] { |
| 53 | + return key + "=" + cat.placeholder, true |
| 54 | + } |
| 55 | + } |
| 56 | + return "", false |
| 57 | +} |
| 58 | + |
7 | 59 | var redirectConsumeNext = map[string]bool{ |
8 | 60 | ">": true, ">>": true, "<": true, |
9 | 61 | "&>": true, "&>>": true, |
|
0 commit comments