Module: CommandKit::Stdio

Included in:
Colors, Command, Commands, Help::Man, Interactive, Pager, Printing, Terminal
Defined in:
lib/command_kit/stdio.rb

Overview

Provides access to stdin, stdout, and stderr streams.

Examples

class MyCmd
  include CommandKit::Stdio

  def run
    print 'Name: '
    name = gets

    puts "Hello #{name}!"
  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)

Instance Method Summary collapse

Instance Method Details

#abort(message = nil) ⇒ Object

Overrides Kernel.abort to print to #stderr.

Parameters:

  • message (String, nil) (defaults to: nil)

    The optional abort message.



161
162
163
164
# File 'lib/command_kit/stdio.rb', line 161

def abort(message=nil)
  stderr.puts(message) if message
  exit(1)
end

#gets(*arguments) ⇒ Object

Calls stdin.gets.



92
93
94
# File 'lib/command_kit/stdio.rb', line 92

def gets(*arguments)
  stdin.gets(*arguments)
end

#initialize(stdin: nil, stdout: nil, stderr: nil, **kwargs) ⇒ Object

Initializes #stdin, #stdout, and #stderr.

Parameters:

  • stdin (IO) (defaults to: nil)

    The stdin input stream. Defaults to $stdin.

  • stdout (IO) (defaults to: nil)

    The stdout output stream. Defaults to $stdout.

  • stderr (IO) (defaults to: nil)

    The stderr error output stream. Defaults to $stderr.



43
44
45
46
47
48
49
# File 'lib/command_kit/stdio.rb', line 43

def initialize(stdin: nil, stdout: nil, stderr: nil, **kwargs)
  @stdin  = stdin
  @stdout = stdout
  @stderr = stderr

  super(**kwargs)
end

Calls stdout.print.



140
141
142
# File 'lib/command_kit/stdio.rb', line 140

def print(*arguments)
  stdout.print(*arguments)
end

#printf(*arguments) ⇒ Object

Calls stdout.printf.



149
150
151
# File 'lib/command_kit/stdio.rb', line 149

def printf(*arguments)
  stdout.printf(*arguments)
end

#putc(*arguments) ⇒ Object

Calls stdout.putc.



122
123
124
# File 'lib/command_kit/stdio.rb', line 122

def putc(*arguments)
  stdout.putc(*arguments)
end

#puts(*arguments) ⇒ Object

Calls stdout.puts.



131
132
133
# File 'lib/command_kit/stdio.rb', line 131

def puts(*arguments)
  stdout.puts(*arguments)
end

#readline(*arguments) ⇒ Object

Calls stdin.readline.



101
102
103
# File 'lib/command_kit/stdio.rb', line 101

def readline(*arguments)
  stdin.readline(*arguments)
end

#readlines(*arguments) ⇒ Object

Calls stdin.readlines.



110
111
112
# File 'lib/command_kit/stdio.rb', line 110

def readlines(*arguments)
  stdin.readlines(*arguments)
end

#stderr$stderr, IO

Returns the stderr error output stream.

Returns:

  • ($stderr, IO)

    The initialized @stderr value or $stderr.



83
84
85
# File 'lib/command_kit/stdio.rb', line 83

def stderr
  @stderr || $stderr
end

#stdin$stdin, IO

Returns the stdin input stream.

Returns:

  • ($stdin, IO)

    The initialized @stdin value or $stdin.



59
60
61
# File 'lib/command_kit/stdio.rb', line 59

def stdin
  @stdin || $stdin
end

#stdout$stdout, IO

Returns the stdout output stream.

Returns:

  • ($stdout, IO)

    The initialized @stdout value or $stdout.



71
72
73
# File 'lib/command_kit/stdio.rb', line 71

def stdout
  @stdout || $stdout
end