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) ⇒ 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) ⇒ Command
Returns a new instance of Command.
176 177 178 179 180 181 182 |
# File 'lib/sys_cmd.rb', line 176 def initialize(command) @command = command @output = nil @status = nil @error_output = nil @error = nil end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
184 185 186 |
# File 'lib/sys_cmd.rb', line 184 def command @command end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
184 185 186 |
# File 'lib/sys_cmd.rb', line 184 def error @error end |
#error_output ⇒ Object (readonly)
Returns the value of attribute error_output.
184 185 186 |
# File 'lib/sys_cmd.rb', line 184 def error_output @error_output end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
184 185 186 |
# File 'lib/sys_cmd.rb', line 184 def output @output end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
184 185 186 |
# File 'lib/sys_cmd.rb', line 184 def status @status end |
Instance Method Details
#error? ⇒ Boolean
did the command execution caused an exception?
247 248 249 |
# File 'lib/sys_cmd.rb', line 247 def error? !@error.nil? end |
#run(options = {}) ⇒ Object
Execute the command.
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.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/sys_cmd.rb', line 214 def run( = {}) @output = @status = @error_output = @error = nil 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 else @error ? nil : @status.success? ? true : false end end |
#status_value ⇒ Object
242 243 244 |
# File 'lib/sys_cmd.rb', line 242 def status_value @status && @status.exitstatus end |
#success? ⇒ Boolean
did the command execute without error and returned a success status?
252 253 254 |
# File 'lib/sys_cmd.rb', line 252 def success? !error? && @status.success? end |
#to_s ⇒ Object
256 257 258 |
# File 'lib/sys_cmd.rb', line 256 def to_s command end |