【问题标题】:Is there an equivalent to the Ruby Shellwords module for the Windows shell?是否有与 Windows shell 的 Ruby Shellwords 模块等效的模块?
【发布时间】:2011-08-03 22:37:53
【问题描述】:

我需要从 Ruby 中的数组构造 Windows shell 命令行。如果我使用 Bash,我可以使用标准的 Shellwords 模块。是否有与 Windows shell 的 Shellwords 等效的工具,可以安全地将数组转换为命令行字符串?

【问题讨论】:

    标签: ruby windows shell command-line-arguments


    【解决方案1】:

    不幸的是,在我看来,实际上没有 Windows 类似 Shellwords。

    【讨论】:

      【解决方案2】:

      我已经解决了以下问题:

      require 'os'
      class String
        def ~
          if OS.windows?
            return '"' + self.gsub('"', '""') + '"'
          else
            return self.shellescape
          end
        end
      end
      

      这允许我通过做shellescape任何字符串

      ~"some string with cruft&! in it"
      

      【讨论】:

      【解决方案3】:

      这似乎是 Windows 支持的 shellwords 版本: https://github.com/larskanis/shellwords

      据我所知,还不是上游。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 2011-05-05
        • 1970-01-01
        • 2011-01-21
        • 2023-03-13
        相关资源
        最近更新 更多