Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Split a command-line string respecting quotes and escape characters
ResourceFunction["CommandLineSplit"][str] splits the string str respecting quotes and escape characters. |
| "TokenDelimiters" | {" ", "\t"} | sets the delimiter between tokens |
| "EscapeCharacter" | "\\" | sets the escape character |
CommandLineSplit will break up a string into constituent argv elements keeping strings as a single unit:
| In[1]:= |
| Out[1]= |
Escape characters within strings are supported:
| In[2]:= |
| Out[2]= |
Multiple commands are parsed separately with the separators being retained as separate elements:
| In[3]:= |
| Out[3]= |
Double-quoted substrings are kept together as one token:
| In[4]:= |
| Out[4]= |
Escaped command separators are ignored:
| In[5]:= |
| Out[5]= |
Extra whitespace is ignored:
| In[6]:= |
| Out[6]= |
Empty strings are preserved:
| In[7]:= |
| Out[7]= |
An equals sign can be treated as an additional delimiter using the "TokenDelimiters" option:
| In[8]:= | ![]() |
| Out[8]= |
A different escape character can be specified with "EscapeCharacter":
| In[9]:= |
| Out[9]= |
Split a simple command into a form suitable for further processing:
| In[10]:= |
| Out[10]= |
Wolfram Language 12.3 (May 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License