Module: ShellCommand

Defined in:
lib/shell_command.rb,
lib/shell_command/version.rb

Defined Under Namespace

Classes: Command, Exception

Constant Summary collapse

VERSION =
"0.2.6"

Class Method Summary collapse

Class Method Details

.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

.run!(*args) ⇒ ShellCommand::Command

Returns a Command

Parameters:

  • *args (String)

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

Returns:

Raises:



73
74
75
76
77
78
79
80
81
82
# File 'lib/shell_command.rb', line 73

def run! *args
  command = run(*args)

  if command.success?
    command
  else
    raise ShellCommand::Exception,
          "The command '#{args.join(' ')}' has failed."
  end
end