Sha256: 359d227d871477a9184dafee45a37cdd9431be30bad2a514f4c80de89f448b6c

Contents?: true

Size: 1.7 KB

Versions: 12

Compression:

Stored size: 1.7 KB

Contents

# = 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

  # Escape special characters used in most
  # unix shells to use it, eg. with system().

  def escape(cmdline)
    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(" ")
  end

  def to_shell
    to_shellwords.join(' ')
  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|
      m = f.to_s.size == 1 ? '-' : '--'
      case v
      when Array
        v.collect{ |e| "#{m}#{f}='#{e}'" }.join(' ')
      when true
        "#{m}#{f}"
      when false, nil
        ''
      else
        "#{m}#{f}='#{v}'"
      end
    end
    flags #.join(" ")
  end

  def to_shell
    to_shellwords.join(' ')
  end

  # Original name.
  alias_method :to_console, :to_shell
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
facets-2.7.0 lib/more/facets/shellwords.rb
facets-2.6.0 lib/lore/facets/shellwords.rb
facets-2.4.0 lib/facets/shellwords.rb
facets-2.4.1 lib/facets/shellwords.rb
facets-2.4.4 lib/lore/facets/shellwords.rb
facets-2.4.2 lib/lore/facets/shellwords.rb
facets-2.4.3 lib/lore/facets/shellwords.rb
facets-2.4.5 lib/lore/facets/shellwords.rb
facets-2.5.0 lib/lore/facets/shellwords.rb
facets-2.5.1 lib/lore/facets/shellwords.rb
facets-2.5.2 lib/lore/facets/shellwords.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/lore/facets/shellwords.rb