Method: Shellwords.shellsplit
- Defined in:
- lib/rubysl/shellwords/shellwords.rb
.shellsplit(line) ⇒ Object Also known as: split
Splits a string into an array of tokens in the same way the UNIX Bourne shell does.
argv = Shellwords.split('here are "two words"')
argv #=> ["here", "are", "two words"]
String#shellsplit is a shorthand for this function.
argv = 'here are "two words"'.shellsplit
argv #=> ["here", "are", "two words"]
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rubysl/shellwords/shellwords.rb', line 32 def shellsplit(line) words = [] field = '' line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do |word, sq, dq, esc, garbage, sep| raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage field << (word || sq || (dq || esc).gsub(/\\(?=.)/, '')) if sep words << field field = '' end end words end |