开发者

Is there an equivalent to the Ruby Shellwords module for the Windows shell?

开发者 https://www.devze.com 2023-02-25 13:32 出处:网络
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

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 a开发者_如何学Gon array to a commandline string?


It appears to me there is in fact no Windows analogue to Shellwords unfortunately.


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"


This seems to be a version of shellwords with windows support: https://github.com/larskanis/shellwords

Not upstream yet as far as I see.

0

精彩评论

暂无评论...
验证码 换一张
取 消