diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | lib/shellwords.rb | 7 | ||||
-rw-r--r-- | test/test_shellwords.rb | 16 |
3 files changed, 20 insertions, 8 deletions
@@ -1,3 +1,8 @@ +Sun Dec 13 14:16:09 2015 Akinori MUSHA <[email protected]> + + * lib/shellwords.rb (Shellwords#shellsplit): Document that this + method does not treat shell metacharacters as such. + Sun Dec 13 12:17:43 2015 Eric Wong <[email protected]> * lib/shellwords.rb (shellescape): duplicate frozen literal diff --git a/lib/shellwords.rb b/lib/shellwords.rb index 66092c8999..0030f0784f 100644 --- a/lib/shellwords.rb +++ b/lib/shellwords.rb @@ -64,6 +64,13 @@ module Shellwords # argv = Shellwords.split('here are "two words"') # argv #=> ["here", "are", "two words"] # + # Note, however, that this is not a command line parser. Shell + # metacharacters except for the single and double quotes and + # backslash are not treated as such. + # + # argv = Shellwords.split('ruby my_prog.rb | less') + # argv #=> ["ruby", "my_prog.rb", "|", "less"] + # # String#shellsplit is a shortcut for this function. # # argv = 'here are "two words"'.shellsplit diff --git a/test/test_shellwords.rb b/test/test_shellwords.rb index e70e0d72bf..3ead5f7c49 100644 --- a/test/test_shellwords.rb +++ b/test/test_shellwords.rb @@ -6,15 +6,15 @@ class TestShellwords < Test::Unit::TestCase include Shellwords - def setup - @not_string = Class.new - @cmd = "ruby my_prog.rb | less" - end - + def test_shellwords + cmd1 = "ruby -i'.bak' -pe \"sub /foo/, '\\\\&bar'\" foobar\\ me.txt\n" + assert_equal(['ruby', '-i.bak', '-pe', "sub /foo/, '\\&bar'", "foobar me.txt"], + shellwords(cmd1)) - def test_string - assert_instance_of(Array, shellwords(@cmd)) - assert_equal(4, shellwords(@cmd).length) + # shellwords does not interpret meta-characters + cmd2 = "ruby my_prog.rb | less" + assert_equal(['ruby', 'my_prog.rb', '|', 'less'], + shellwords(cmd2)) end def test_unmatched_double_quote |