I need to construct Windows shell commandlines from arrays in Ruby. If I were using Bash, I could use the standard Shellwords module. Is there an equivalent to Shellwords for the Windows shell, which can safely transform an array to a commandline string?
Asked
Active
Viewed 749 times
3 Answers
2
I've settled on the following:
require 'os'
class String
def ~
if OS.windows?
return '"' + self.gsub('"', '""') + '"'
else
return self.shellescape
end
end
end
which allows me to shellescape any string by doing
~"some string with cruft&! in it"

retorquere
- 1,496
- 1
- 14
- 27
-
should be noted that `os` comes from the os gem, which can be found here: https://github.com/rdp/os - while not a huge problem, it does add a dependency. There is this Question over here that discusses windows detection: http://stackoverflow.com/questions/4871309/what-is-the-correct-way-to-detect-if-ruby-is-running-on-windows – amenthes Mar 24 '15 at 22:01
2
This seems to be a version of shellwords with windows support: https://github.com/larskanis/shellwords
Not upstream yet as far as I see.

akostadinov
- 17,364
- 6
- 77
- 85
2
It appears to me there is in fact no Windows analogue to Shellwords unfortunately.

aknuds1
- 65,625
- 67
- 195
- 317