Class: SystemCall::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/system_call/command.rb

Overview

A class responsible for communication with command line interfaces.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Command

Initializes a SystemCall::Command.

Parameters:

  • args (Array)

    The command line arguments.



18
19
20
# File 'lib/system_call/command.rb', line 18

def initialize(*args)
  @args = args.flatten
end

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/system_call/command.rb', line 11

class Command
  attr_reader :args

  ##
  # Initializes a {Command}.
  #
  # @param args [Array] The command line arguments.
  def initialize(*args)
    @args = args.flatten
  end

  ##
  # Initializes and calls a {Command}, then returns the {Result}.
  #
  # @param args [Array] The command line arguments.
  # @return [Result]
  def self.call(*args)
    new(*args).call
  end

  ##
  # Invokes the {Command} and returns the {Result}.
  #
  # @return [Result]
  def call
    Open3.popen3(*args) do |_, stdout, stderr, wait_thr|
      success_result = readlines(stdout)
      error_result = readlines(stderr)
      exit_status = wait_thr.value
      Result.new(exit_status, success_result, error_result)
    end
  end

  private

  def readlines(io)
    io.readlines.join.chomp
  end
end

Class Method Details

.call(*args) ⇒ Result

Initializes and calls a SystemCall::Command, then returns the Result.

Parameters:

  • args (Array)

    The command line arguments.

Returns:



27
28
29
# File 'lib/system_call/command.rb', line 27

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callResult

Invokes the SystemCall::Command and returns the Result.

Returns:



35
36
37
38
39
40
41
42
# File 'lib/system_call/command.rb', line 35

def call
  Open3.popen3(*args) do |_, stdout, stderr, wait_thr|
    success_result = readlines(stdout)
    error_result = readlines(stderr)
    exit_status = wait_thr.value
    Result.new(exit_status, success_result, error_result)
  end
end