Sha256: 494528ec8c9f5c0c598964f98b3247b2f63b7688ac3edb43e04442c3e62146a4

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'escape'

module EventMachine
  class SystemCommand
    class Builder

      def initialize *args
        unless args.length > 0
          raise "You have to provide at least one argument as command"
        end
        @arr = args
      end

      def add opt, val = nil
        if opt.is_a?(Array)
          opt.each do |element|
            add *element
          end
        else
          if val
            @arr << [ opt, val ]
          else
            @arr << opt
          end
        end
        self
      end
      alias :<< :add

      ##
      # Returns the command string
      def to_s
        cmd = ''
        @arr.each do |arg|
          if arg == @arr.first
            cmd << arg
          elsif arg.is_a?(Array)
            param, value = arg.first.to_s, arg.last

            if param =~ /^\-{1,2}(.*)/
              param = $1
            end

            if param.length == 1
              cmd << ' ' << "-#{param} #{Escape.shell_single_word(value)}"
            else
              cmd << ' ' << "--#{param}=#{Escape.shell_single_word(value)}"
            end
          elsif arg.is_a?(Symbol)
            arg = arg.to_s
            if arg.length == 1
              cmd << ' ' << "-#{arg}"
            else
              cmd << ' ' << "--#{arg}"
            end
          elsif arg.strip =~ /^\-/
            cmd << ' ' << arg
          else
            cmd << ' ' << Escape.shell_single_word(arg)
          end
        end
        cmd
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
em-systemcommand-2.0.4 lib/em-systemcommand/builder.rb