Sha256: 72eb12215cf998f0c11ea4b5510270681a8831b26b0a918254e7c320b52b2574

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'shellwords'

module Shellwords

  module_function

  # Escape special characters used in most unix shells
  # to use it, eg. with system().
  #
  # This differs from Ruby's #escape in that it does not
  # escape shell variables, e.g. $0.
  def alt_escape(cmdline)
    cmdline.gsub(/([\\\t\| &`<>)('"])/) { |s| '\\' << s }
  end

  unless method_defined?(:escape)
    def escape(cmdline)
      cmdline.gsub(/([\\\t\| &`<>)('"])/) { |s| '\\' << s }
    end
  end

  # Escape special character used in DOS-based shells.
  #
  # TODO: How to integrate with rest of system?
  # 1. Use platform condition?
  # 2. Use separate dos_xxx methods?
  # 3. Put in separate PowerShellwords module?
  def dos_escape(cmdline)
    '"' + cmdline.gsub(/\\(?=\\*\")/, "\\\\\\").gsub(/\"/, "\\\"").gsub(/\\$/, "\\\\\\").gsub("%", "%%") + '"'
  end

end

class Array

  # Convert an array into command line parameters.
  # The array is accepted in the format of Ruby
  # method arguments --ie. [arg1, arg2, ..., hash]
  #
  def shellwords
    opts, args = *flatten.partition{ |e| Hash === e }
    opts = opts.inject({}){ |m,h| m.update(h); m }
    opts.shellwords + args
  end

  def shelljoin
    Shellwords.shelljoin(shellwords)
  end

end

class Hash

  #
  def shellwords
    argv = []
    each do |f,v|
      m = f.to_s.size == 1 ? '-' : '--'
      case v
      when false, nil
      when Array
        v.each do |e|
          argv << %[#{m}#{f}="#{e}"]
        end
      when true
        argv << %[#{m}#{f}]
      else
        argv << %[#{m}#{f}="#{v}"]
      end
    end
    argv
  end

  #
  def shelljoin
    shellwords.shelljoin
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.9.0 lib/more/facets/shellwords.rb
facets-2.9.0.pre.2 lib/more/facets/shellwords.rb
facets-2.9.0.pre.1 lib/more/facets/shellwords.rb