Class: SysCmd::Command

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

Overview

An executable system command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = {}) ⇒ Command

Returns a new instance of Command.



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/sys_cmd.rb', line 177

def initialize(command, options = {})
  if command.respond_to?(:shell)
    @command = command.command
    @shell = command.shell
  else
    @command = command
    @shell = Shell.new(options)
  end
  @output = nil
  @status = nil
  @error_output = nil
  @error = nil
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



191
192
193
# File 'lib/sys_cmd.rb', line 191

def command
  @command
end

#errorObject (readonly)

Returns the value of attribute error.



191
192
193
# File 'lib/sys_cmd.rb', line 191

def error
  @error
end

#error_outputObject (readonly)

Returns the value of attribute error_output.



191
192
193
# File 'lib/sys_cmd.rb', line 191

def error_output
  @error_output
end

#outputObject (readonly)

Returns the value of attribute output.



191
192
193
# File 'lib/sys_cmd.rb', line 191

def output
  @output
end

#statusObject (readonly)

Returns the value of attribute status.



191
192
193
# File 'lib/sys_cmd.rb', line 191

def status
  @status
end

Instance Method Details

#error?Boolean

did the command execution caused an exception?

Returns:

  • (Boolean)


270
271
272
# File 'lib/sys_cmd.rb', line 270

def error?
  !@error.nil?
end

#run(options = {}) ⇒ Object

Execute the command.

By default the command is executed by a shell. In this case, unquoted arguments are interpreted by the shell, e.g.

SysCmd.command('echo $BASH').run # /bin/bash

When the :direct option is set to true, no shell is used and the command is directly executed; in this case unquoted arguments are not interpreted:

SysCmd.command('echo $BASH').run # $BASH

The exit status of the command is retained in the status attribute (and its numeric value in the status_value attribute).

The standard output of the command is captured and retained in the output attribute.

By default, the standar error output of the command is not captured, so it will be shown on the console unless redirected.

Standard error can be captured and interleaved with the standard output passing the option

error_output: :mix

Error output can be captured and keep separate inthe error_output attribute with this option:

error_output: :separate

The value returned is by defaut, like in Kernel#system, true if the command gives zero exit status, false for non zero exit status, and nil if command execution fails.

The :return option can be used to make this method return other attribute of the executed command.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/sys_cmd.rb', line 232

def run(options = {})
  @output = @status = @error_output = @error = nil
  if options[:direct]
    command = @shell.split(@command)
  else
    command = [@command]
  end
  begin
    case options[:error_output]
    when :mix # mix stderr with stdout
      @output, @status = Open3.capture2e(*command)
    when :separate
      @output, @error_output, @status = Open3.capture3(*command)
    else # :console (do not capture stderr output)
      @output, @status = Open3.capture2(*command)
    end
  rescue => error
    @error = error.dup
  end
  case options[:return]
  when :status
    @status
  when :status_value
    status_value
  when :output
    @output
  when :error_output
    @error_output
  else
    @error ? nil : @status.success? ? true : false
  end
end

#status_valueObject



265
266
267
# File 'lib/sys_cmd.rb', line 265

def status_value
  @status && @status.exitstatus
end

#success?Boolean

did the command execute without error and returned a success status?

Returns:

  • (Boolean)


275
276
277
# File 'lib/sys_cmd.rb', line 275

def success?
  !error? && @status.success?
end

#to_sObject



279
280
281
# File 'lib/sys_cmd.rb', line 279

def to_s
  command
end