Sha256: 8de9cbf27bfa5029fe5429b59627fafc02f229804762b1bea40e4d503bf43165
Contents?: true
Size: 1.73 KB
Versions: 1
Compression:
Stored size: 1.73 KB
Contents
require "shell_command/version" require 'open4' module ShellCommand class Exception < StandardError end # # @attr [Process::Status] status # The status of the command. # # @attr [String] stderr # The output written by the command to standard error. # # @attr [String] stdout # The output written by the command to standard output. # class Command < Struct.new(:status, :stdout, :stderr) def method_missing method, *args, &block if status.respond_to?(method) status.send(method, *args, &block) else super end end def respond_to_missing? method, include_private = false status.respond_to?(method, include_private) end end # # @param [String] *args # A command and its arguments(if any) to execute. # # @yieldparam [ShellCommand::Command] command # Yields {ShellCommand::Command} if a block is given. # # @return [ShellCommand::Command] # Returns a {ShellCommand::Command} # def run *args if args.empty? raise ArgumentError, 'Wrong number of arguments (0 for 1)' end pid, stdin, stdout, stderr = Open4.open4(*args) _, status = Process.wait2(pid) command = Command.new(status, stdout.read, stderr.read) if block_given? yield command else command end end module_function :run # # @param # (see #run) # # @raise [ShellCommand::Exception] # If the command is unsuccessful (e.g: > 0 exit status code) # # @return # (see #run) # def run! *args command = run(*args) if command.success? command else raise ShellCommand::Exception, "The command '#{args.join(' ')}' has failed." end end module_function :run! end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
shell_command-0.2.0 | lib/shell_command.rb |