Class: SysCmd::Command
- Inherits:
-
Object
- Object
- SysCmd::Command
- Defined in:
- lib/sys_cmd.rb
Overview
An executable system command
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#error_output ⇒ Object
readonly
Returns the value of attribute error_output.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#error? ⇒ Boolean
did the command execution caused an exception?.
-
#initialize(command, options = {}) ⇒ Command
constructor
A new instance of Command.
-
#run(options = {}) ⇒ Object
Execute the command.
- #status_value ⇒ Object
-
#success? ⇒ Boolean
did the command execute without error and returned a success status?.
- #to_s ⇒ Object
Constructor Details
#initialize(command, options = {}) ⇒ Command
Returns a new instance of Command.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/sys_cmd.rb', line 200 def initialize(command, = {}) if command.respond_to?(:shell) @command = command.command @shell = command.shell @stdin_data = command.stdin_data else @command = command @shell = Shell.new() @stdin_data = [:stdin_data] end @output = nil @status = nil @error_output = nil @error = nil end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
216 217 218 |
# File 'lib/sys_cmd.rb', line 216 def command @command end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
216 217 218 |
# File 'lib/sys_cmd.rb', line 216 def error @error end |
#error_output ⇒ Object (readonly)
Returns the value of attribute error_output.
216 217 218 |
# File 'lib/sys_cmd.rb', line 216 def error_output @error_output end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
216 217 218 |
# File 'lib/sys_cmd.rb', line 216 def output @output end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
216 217 218 |
# File 'lib/sys_cmd.rb', line 216 def status @status end |
Instance Method Details
#error? ⇒ Boolean
did the command execution caused an exception?
304 305 306 |
# File 'lib/sys_cmd.rb', line 304 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.
The :stdin_data option can be used to pass a String as the command’s standar input.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/sys_cmd.rb', line 260 def run( = {}) @output = @status = @error_output = @error = nil if [:direct] command = @shell.split(@command) else command = [@command] end stdin_data = [:stdin_data] || @stdin_data if stdin_data command << { stdin_data: stdin_data } end begin case [: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 [:return] when :status @status when :status_value status_value when :output @output when :error_output @error_output when :command self else @error ? nil : @status.success? ? true : false end end |
#status_value ⇒ Object
299 300 301 |
# File 'lib/sys_cmd.rb', line 299 def status_value @status && @status.exitstatus end |
#success? ⇒ Boolean
did the command execute without error and returned a success status?
309 310 311 |
# File 'lib/sys_cmd.rb', line 309 def success? !error? && @status.success? end |
#to_s ⇒ Object
313 314 315 |
# File 'lib/sys_cmd.rb', line 313 def to_s command end |