Module: Squared::Common::Shell
- Defined in:
- lib/squared/common/shell.rb
Class Method Summary collapse
- .double_quote(val) ⇒ Object
- .fill_option(val) ⇒ Object
- .shell_escape(val, quote: false, force: false) ⇒ Object
- .shell_option(flag, val = nil, quote: false, escape: true) ⇒ Object
- .shell_quote(val, force: true) ⇒ Object
- .shell_split(val, escape: true, quote: false, join: nil) ⇒ Object
- .single_quote(val) ⇒ Object
- .split_escape(val, char: ',') ⇒ Object
Class Method Details
.double_quote(val) ⇒ Object
63 64 65 |
# File 'lib/squared/common/shell.rb', line 63 def double_quote(val) val.gsub(/(?<!\\)"/, '\\"') end |
.fill_option(val) ⇒ Object
53 54 55 56 57 |
# File 'lib/squared/common/shell.rb', line 53 def fill_option(val) return "-#{val}" if val =~ /\A[a-z](?:\d*)\z/i shell_escape(val.start_with?('-') ? val : "--#{val}") end |
.shell_escape(val, quote: false, force: false) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/squared/common/shell.rb', line 11 def shell_escape(val, quote: false, force: false) if (data = /\A(--?[^= ]+)((=|\s+)(["'])?(.+?)(["'])?)?\z/m.match(val = val.to_s)) return val if !data[2] || (!data[4] && !data[5].match?(/\s/)) join = ->(opt) { data[1] + data[3] + shell_quote(opt) } if data[4] == data[6] data[4] ? val : join.(data[5]) else join.("#{data[4]}#{data[5]}#{data[6]}") end elsif Rake::Win32.windows? quote ? shell_quote(val, force: force) : val else val.empty? ? '' : Shellwords.escape(val) end end |
.shell_option(flag, val = nil, quote: false, escape: true) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/squared/common/shell.rb', line 43 def shell_option(flag, val = nil, quote: false, escape: true) "--#{flag}#{if val "=#{if escape shell_escape(val, quote: quote) else quote ? shell_quote(val) : val end}" end}" end |
.shell_quote(val, force: true) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/squared/common/shell.rb', line 28 def shell_quote(val, force: true) val = val.to_s return val if (!force && !val.include?(' ')) || val =~ /(?:^|\S=|[^=]\s+)(["']).+\1\z/m Rake::Win32.windows? ? "\"#{double_quote(val)}\"" : "'#{single_quote(val)}'" end |
.shell_split(val, escape: true, quote: false, join: nil) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/squared/common/shell.rb', line 35 def shell_split(val, escape: true, quote: false, join: nil) val = Shellwords.split(val) val = val.map { |opt| shell_escape(opt, quote: quote) } if escape return val unless join val.join(join.is_a?(::String) ? join : ' ') end |
.single_quote(val) ⇒ Object
59 60 61 |
# File 'lib/squared/common/shell.rb', line 59 def single_quote(val) val.gsub("'", "'\\\\''") end |
.split_escape(val, char: ',') ⇒ Object
67 68 69 |
# File 'lib/squared/common/shell.rb', line 67 def split_escape(val, char: ',') val.split(/\s*(?<!\\)#{char}\s*/) end |