1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/shellwords.rb
nobu 7145ec7bb2 * lib/shellwords.rb: fix for blank but not empty string.
fixed: [ruby-dev:27663]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9521 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-11-10 12:05:57 +00:00

45 lines
1 KiB
Ruby

#
# shellwords.rb: Split text into an array of tokens a la UNIX shell
#
#
# This module is originally a port of shellwords.pl, but modified to
# conform to POSIX / SUSv3 (IEEE Std 1003.1-2001).
#
# Examples:
#
# require 'shellwords'
# words = Shellwords.shellwords(line)
#
# or
#
# require 'shellwords'
# include Shellwords
# words = shellwords(line)
#
module Shellwords
#
# Split text into an array of tokens in the same way the UNIX Bourne
# shell does.
#
# See the +Shellwords+ module documentation for an example.
#
def shellwords(line)
words = []
field = ''
word = sq = dq = esc = garbage = sep = nil
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
module_function :shellwords
end