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.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#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(options = {}) ⇒ Object
Constructor Details
#initialize(command, options = {}) ⇒ Command
Returns a new instance of Command.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/sys_cmd.rb', line 207 def initialize(command, = {}) if command.respond_to?(:shell) @command = command.command @shell = command.shell @input = command.stdin_data else @command = command @shell = Shell.new() @input = [: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.
223 224 225 |
# File 'lib/sys_cmd.rb', line 223 def command @command end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
223 224 225 |
# File 'lib/sys_cmd.rb', line 223 def error @error end |
#error_output ⇒ Object (readonly)
Returns the value of attribute error_output.
223 224 225 |
# File 'lib/sys_cmd.rb', line 223 def error_output @error_output end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
223 224 225 |
# File 'lib/sys_cmd.rb', line 223 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
223 224 225 |
# File 'lib/sys_cmd.rb', line 223 def output @output end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
223 224 225 |
# File 'lib/sys_cmd.rb', line 223 def status @status end |
Instance Method Details
#error? ⇒ Boolean
did the command execution caused an exception?
311 312 313 |
# File 'lib/sys_cmd.rb', line 311 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.
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 298 299 300 301 302 303 304 |
# File 'lib/sys_cmd.rb', line 267 def run( = {}) @output = @status = @error_output = @error = nil if [:direct] command = @shell.split(@command) else command = [@command] end stdin_data = [:stdin_data] || @input 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
306 307 308 |
# File 'lib/sys_cmd.rb', line 306 def status_value @status && @status.exitstatus end |
#success? ⇒ Boolean
did the command execute without error and returned a success status?
316 317 318 |
# File 'lib/sys_cmd.rb', line 316 def success? !error? && @status.success? end |
#to_s(options = {}) ⇒ Object
320 321 322 323 324 325 326 |
# File 'lib/sys_cmd.rb', line 320 def to_s( = {}) if @input && [:with_input] "#{command}#{@shell.here_doc(@input)}" else command end end |