2 # shellwords.rb: Split text into an array of tokens a la UNIX shell
6 # This module is originally a port of shellwords.pl, but modified to
7 # conform to POSIX / SUSv3 (IEEE Std 1003.1-2001).
11 # require 'shellwords'
12 # words = Shellwords.shellwords(line)
16 # require 'shellwords'
18 # words = shellwords(line)
23 # Split text into an array of tokens in the same way the UNIX Bourne
26 # See the +Shellwords+ module documentation for an example.
29 line = String.new(line) rescue
30 raise(ArgumentError, "Argument must be a string")
36 if line.sub!(/\A"(([^"\\]|\\.)*)"/, '') then
37 snippet = $1.gsub(/\\(.)/, '\1')
38 elsif line =~ /\A"/ then
39 raise ArgumentError, "Unmatched double quote: #{line}"
40 elsif line.sub!(/\A'([^']*)'/, '') then
42 elsif line =~ /\A'/ then
43 raise ArgumentError, "Unmatched single quote: #{line}"
44 elsif line.sub!(/\A\\(.)?/, '') then
46 elsif line.sub!(/\A([^\s\\'"]+)/, '') then
59 module_function :shellwords