Class: Toys::StandardMiddleware::HandleUsageErrors

Inherits:
Object
  • Object
show all
Includes:
Middleware
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

Methods included from Middleware

#config

Constructor Details

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

Create a HandleUsageErrors middleware.

Parameters:

  • exit_code (Integer)

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

  • stream (IO)

    Output stream to write to. Default is stderr.

  • styled_output (Boolean, 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.



51
52
53
54
55
# File 'lib/toys/standard_middleware/handle_usage_errors.rb', line 51

def initialize(exit_code: nil, stream: $stderr, styled_output: nil)
  require "toys/utils/terminal"
  @exit_code = exit_code || USAGE_ERROR_EXIT_CODE
  @terminal = Utils::Terminal.new(output: stream, styled: styled_output)
end