SplitCommandAndArgs takes a command string and parses it shell-style into the command and its separate arguments.
(command string)
| 32 | // SplitCommandAndArgs takes a command string and parses it shell-style into the |
| 33 | // command and its separate arguments. |
| 34 | func SplitCommandAndArgs(command string) (cmd string, args []string, err error) { |
| 35 | var parts []string |
| 36 | |
| 37 | if runtimeGoos == osWindows { |
| 38 | parts = parseWindowsCommand(command) // parse it Windows-style |
| 39 | } else { |
| 40 | parts, err = parseUnixCommand(command) // parse it Unix-style |
| 41 | if err != nil { |
| 42 | err = errors.New("error parsing command: " + err.Error()) |
| 43 | return |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if len(parts) == 0 { |
| 48 | err = errors.New("no command contained in '" + command + "'") |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | cmd = parts[0] |
| 53 | if len(parts) > 1 { |
| 54 | args = parts[1:] |
| 55 | } |
| 56 | |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | // parseUnixCommand parses a unix style command line and returns the |
| 61 | // command and its arguments or an error |