Method: ShellCommand.run

Defined in:
lib/shell_command.rb

.run(*args) {|command| ... } ⇒ ShellCommand::Command

Returns a Command

Parameters:

  • *args (String)

    A command and its arguments(if any) to execute.

Yield Parameters:

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shell_command.rb', line 45

def run *args
  if args.empty?
    raise ArgumentError, 'Wrong number of arguments (0 for 1+)'
  end
  
  pid, stdin, stdout, stderr = Open4.open4(*args)
  _, status = Process.wait2(pid)
  command = Command.new(status, stdout.read, stderr.read)
  [stdin, stdout, stderr].map(&:close)
  
  if block_given?
    yield command
  else
    command
  end
end