lib/more/facets/shellwords.rb in facets-2.7.0 vs lib/more/facets/shellwords.rb in facets-2.8.0

- old
+ new

@@ -1,26 +1,5 @@ -# = Shellwords Extended -# -# Adds extensions to Shellwords, namely #escape. -# -# == Authors -# -# * Thomas Sawuer -# -# == Copying -# -# Copyright (c) 2007 Thomas Sawyer -# -# Ruby License -# -# This module is free software. You may use, modify, and/or redistribute this -# software under the same terms as Ruby. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. - require 'shellwords' module Shellwords module_function @@ -32,58 +11,52 @@ cmdline.gsub(/([\\\t\| &`<>)('"])/) { |s| '\\' << s } end end - class Array # Convert an array into command line parameters. # The array is accepted in the format of Ruby # method arguments --ie. [arg1, arg2, ..., hash] - - def to_shellwords - flags = (Hash===last ? pop : {}) - flags = flags.to_shellwords - flags + ' ' + self #join(" ") + # + def shellwords + opts, args = *flatten.partition{ |e| Hash === e } + opts = opts.inject({}){ |m,h| m.update(h); m } + opts.shellwords + args end - def to_shell - to_shellwords.join(' ') + def shelljoin + Shellwords.shelljoin(shellwords) end - # Original name. - alias_method :to_console, :to_shell end - class Hash - # Convert an array into command line parameters. - # The array is accepted in the format of Ruby - # method arguments --ie. [arg1, arg2, ..., hash] - - def to_shellwords - flags = collect do |f,v| + # + def shellwords + argv = [] + each do |f,v| m = f.to_s.size == 1 ? '-' : '--' case v + when false, nil when Array - v.collect{ |e| "#{m}#{f}='#{e}'" }.join(' ') + v.each do |e| + argv << %[#{m}#{f}="#{e}"] + end when true - "#{m}#{f}" - when false, nil - '' + argv << %[#{m}#{f}] else - "#{m}#{f}='#{v}'" + argv << %[#{m}#{f}="#{v}"] end end - flags #.join(" ") + argv end - def to_shell - to_shellwords.join(' ') + # + def shelljoin + shellwords.shelljoin end - # Original name. - alias_method :to_console, :to_shell end