module CommandKit::Stdio

Provides access to stdin, stdout, and stderr streams.

## Examples

class MyCmd
  include CommandKit::Stdio

  def main
  end
end

## Testing

Can be initialized with custom stdin, stdout, and stderr streams for testing purposes.

stdin  = StringIO.new
stdout = StringIO.new
stderr = StringIO.new
MyCmd.new(stdin: stdin, stdout: stdout, stderr: stderr)

Public Class Methods

new(stdin: nil, stdout: nil, stderr: nil, **kwargs) click to toggle source

Initializes {#stdin}, {#stdout}, and {#stderr}.

@param [IO] stdin

The stdin input stream. Defaults to `$stdin`.

@param [IO] stdout

The stdout output stream. Defaults to `$stdout`.

@param [IO] stderr

The stderr error output stream. Defaults to `$stderr`.

@api public

Calls superclass method
# File lib/command_kit/stdio.rb, line 39
def initialize(stdin: nil, stdout: nil, stderr: nil, **kwargs)
  @stdin  = stdin
  @stdout = stdout
  @stderr = stderr

  super(**kwargs)
end

Public Instance Methods

abort(message=nil) click to toggle source

Overrides `Kernel.abort` to print to {#stderr}.

@param [String, nil] message

The optional abort message.

@api public

# File lib/command_kit/stdio.rb, line 157
def abort(message=nil)
  stderr.puts(message) if message
  exit(1)
end
gets(*arguments) click to toggle source

Calls `stdin.gets`.

@api public

# File lib/command_kit/stdio.rb, line 88
def gets(*arguments)
  stdin.gets(*arguments)
end
print(*arguments) click to toggle source

Calls `stdout.print`.

@api public

printf(*arguments) click to toggle source

Calls `stdout.printf`.

@api public

# File lib/command_kit/stdio.rb, line 145
def printf(*arguments)
  stdout.printf(*arguments)
end
putc(*arguments) click to toggle source

Calls `stdout.putc`.

@api public

# File lib/command_kit/stdio.rb, line 118
def putc(*arguments)
  stdout.putc(*arguments)
end
puts(*arguments) click to toggle source

Calls `stdout.puts`.

@api public

# File lib/command_kit/stdio.rb, line 127
def puts(*arguments)
  stdout.puts(*arguments)
end
readline(*arguments) click to toggle source

Calls `stdin.readline`.

@api public

# File lib/command_kit/stdio.rb, line 97
def readline(*arguments)
  stdin.readline(*arguments)
end
readlines(*arguments) click to toggle source

Calls `stdin.readlines`.

@api public

# File lib/command_kit/stdio.rb, line 106
def readlines(*arguments)
  stdin.readlines(*arguments)
end
stderr() click to toggle source

Returns the stderr error output stream.

@return [$stderr, IO]

The initialized `@stderr` value or `$stderr`.

@api public

# File lib/command_kit/stdio.rb, line 79
def stderr
  @stderr || $stderr
end
stdin() click to toggle source

Returns the stdin input stream.

@return [$stdin, IO]

The initialized `@stdin` value or `$stdin`.

@api public

# File lib/command_kit/stdio.rb, line 55
def stdin
  @stdin || $stdin
end
stdout() click to toggle source

Returns the stdout output stream.

@return [$stdout, IO]

The initialized `@stdout` value or `$stdout`.

@api public

# File lib/command_kit/stdio.rb, line 67
def stdout
  @stdout || $stdout
end