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) ⇒ Object
- .shell_quote(val, force: true) ⇒ Object
- .shell_split(val, quote: false, join: false) ⇒ Object
- .single_quote(val) ⇒ Object
- .split_escape(val, char: ',') ⇒ Object
Class Method Details
.double_quote(val) ⇒ Object
49 50 51 |
# File 'lib/squared/common/shell.rb', line 49 def double_quote(val) val.gsub(/(?<!\\)"/, '\\"') end |
.fill_option(val) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/squared/common/shell.rb', line 38 def fill_option(val) return "-#{val}" if val.size == 1 || val =~ /^[a-z]\d+$/i val = "--#{val}" unless val.start_with?('-') shell_escape(val).sub('\\=', '=') end |
.shell_escape(val, quote: false) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/squared/common/shell.rb', line 11 def shell_escape(val, quote: false) val = val.to_s return ::Shellwords.escape(val) unless ::Rake::Win32.windows? quote ? shell_quote(val, force: false) : val end |
.shell_quote(val, force: true) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/squared/common/shell.rb', line 18 def shell_quote(val, force: true) ret = val.to_s.strip return ret if (!force && !ret.include?(' ')) || ret =~ /(?:^|\S=|[^=]\s+)(["']).+\1$/m ::Rake::Win32.windows? ? "\"#{double_quote(ret)}\"" : "'#{single_quote(ret)}'" end |
.shell_split(val, quote: false, join: false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/squared/common/shell.rb', line 25 def shell_split(val, quote: false, join: false) ret = ::Shellwords.split(val).map do |opt| if (data = /^(--?[^= ]+)(=|\s+)?(["'])?(.+?)\3?$/m.match(opt)) next opt unless data[2] && !data[3] data[1] + data[2] + shell_quote(data[4], force: !::Rake::Win32.windows?) else shell_escape(opt, quote: quote) end end join ? ret.join(' ') : ret end |
.single_quote(val) ⇒ Object
45 46 47 |
# File 'lib/squared/common/shell.rb', line 45 def single_quote(val) val.gsub("'", "'\\\\''") end |
.split_escape(val, char: ',') ⇒ Object
53 54 55 |
# File 'lib/squared/common/shell.rb', line 53 def split_escape(val, char: ',') val.split(/\s*(?<!\\)#{char}\s*/o) end |