Class: Toys::StandardMiddleware::HandleUsageErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/standard_middleware/handle_usage_errors.rb

Overview

This middleware handles the case of a usage error. If a usage error, such as an unrecognized flag or an unfulfilled required argument, is detected, this middleware intercepts execution and displays the error along with the short help string, and terminates execution with an error code.

Constant Summary collapse

USAGE_ERROR_EXIT_CODE =

Exit code for usage error. (2 by convention)

Returns:

  • (Integer)
2

Instance Method Summary collapse

Constructor Details

#initialize(exit_code: nil, stream: $stderr, styled_output: nil) ⇒ HandleUsageErrors

Create a HandleUsageErrors middleware.

Parameters:

  • exit_code (Integer) (defaults to: nil)

    The exit code to return if a usage error occurs. Default is USAGE_ERROR_EXIT_CODE.

  • stream (IO) (defaults to: $stderr)

    Output stream to write to. Default is stderr.

  • styled_output (Boolean, nil) (defaults to: nil)

    Cause the tool to display help text with ansi styles. If nil, display styles if the output stream is a tty. Default is nil.



28
29
30
31
32
# File 'lib/toys/standard_middleware/handle_usage_errors.rb', line 28

def initialize(exit_code: nil, stream: $stderr, styled_output: nil)
  @exit_code = exit_code || USAGE_ERROR_EXIT_CODE
  @stream = stream
  @styled_output = styled_output
end