module Extras::Shell

Public Instance Methods

escape(str, original: false) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity Escapes a string double checking if it will double escape. rubocop:disable Metrics/PerceivedComplexity Note: This is taken from Ruby 2.3 StdLib.


Calls superclass method
# File lib/extras/shell.rb, line 19
def escape(str, original: false)
  if original
    return super(
      str
    )
  end

  if !str || str.empty? || str == '""' || str == "''"
    return '""'

  elsif str.is_a?(Array)
    str.map do |v|
      escape(v)
    end

  elsif str.is_a?(Hash)
    str.each do |k, v|
      str[k] = escape(
        v
      )
    end

  else
    regexp = /((?:\\)?[^A-Za-z0-9_\-.,:\/@\n])/
    str = str.gsub(regexp) { $1.start_with?("\\") ? $1 : "\\#{$1}" }
    str = str.gsub(/\n/, "'\n'")
    str
  end
end
Also aliased as: shellescape
shellescape(str, original: false)
Alias for: escape