class String

Public Instance Methods

_shellex_escape() click to toggle source
# File lib/shellex.rb, line 130
def _shellex_escape
  if self.empty? or self.strip == ""
    return "''"
  end
  self.split(/'/, -1).map{|e| "'#{e}'"}.join("\\'")
end
shellex(*args) click to toggle source
# File lib/shellex.rb, line 137
def shellex(*args)
  shellex(self, *args)
end
silent_shellex(*args) click to toggle source
# File lib/shellex.rb, line 141
def silent_shellex(*args)
  silent_shellex(self, *args)
end
with_args(*args) click to toggle source
# File lib/shellex.rb, line 100
def with_args(*args)
  escape = proc { |val| val.to_s._shellex_escape }
  ignore_nil = proc { |val| escape.call(val) unless val.nil? }

  gsub(/(\?\&|\?\?|\?\!|\?~|\?)/) do |match|
    val = args.shift

    case match
      when "?~" # Escape question mark
        "?"
      when "?!" # Argument can't be nil
        if val.nil?
          raise ShellArgumentMissing, "Argument marked as required with ?! is nil"
        else
          escape.call(val)
        end
      when "??" # Argument will be omitted if nil
        ignore_nil.call(val)
      when "?" # Argument will be escaped even if nil
        escape.call(val)
      when "?&" # Argument has to be array and each element will be escaped or ommitted if nil
        if val.is_a?(Array)
          val.map(&ignore_nil).compact.join(" ")
        else
          raise ShellArgumentMissing, "If ?& is present in this position, #{val.inspect} should be an Array"
        end
    end
  end.strip
end