Sha256: 55911042a2aff3977ff844d8c341d39016aae2da9d9351636599d8f4cdfc6511

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 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.shift
        @arr.each do |arg|
          if arg.is_a?(Array)
            param = arg.shift
            param = param.to_s if param.is_a?(Symbol)
            if param =~ /^\-{1,2}(.*)/
              param = $1
            end
            value = arg.shift
            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

8 entries across 8 versions & 1 rubygems

Version Path
em-systemcommand-2.0.2 lib/em-systemcommand/builder.rb
em-systemcommand-2.0.1 lib/em-systemcommand/builder.rb
em-systemcommand-2.0.0 lib/em-systemcommand/builder.rb
em-systemcommand-1.2.0 lib/em-systemcommand/builder.rb
em-systemcommand-1.1.2 lib/em-systemcommand/builder.rb
em-systemcommand-1.0.2 lib/em-systemcommand/builder.rb
em-systemcommand-1.0.1 lib/em-systemcommand/builder.rb
em-systemcommand-1.0.0 lib/em-systemcommand/builder.rb