Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/shellex.rb

Instance Method Summary collapse

Instance Method Details

#_shellex_escapeObject



130
131
132
133
134
135
# 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) ⇒ Object



137
138
139
# File 'lib/shellex.rb', line 137

def shellex(*args)
  shellex(self, *args)
end

#silent_shellex(*args) ⇒ Object



141
142
143
# File 'lib/shellex.rb', line 141

def silent_shellex(*args)
  silent_shellex(self, *args)
end

#with_args(*args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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