Sha256: 3e2dff9169ee003a5da1fa6224cc99830a046eea0cb77d377978cd65cc887d56

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

module CLAide

  require 'claide/informative_error.rb'

  # The exception class that is raised to indicate a help banner should be
  # shown while running {Command.run}.
  #
  class Help < StandardError
    include InformativeError

    # @return [String] The banner containing the usage instructions of the
    # command to show in the help.
    #
    attr_reader :banner

    # @return [String] An optional error message that will be shown before the
    #         help banner.
    #
    attr_reader :error_message

    # @return [Bool] Whether the error message should be colorized.
    #
    attr_reader  :colorize
    alias_method :colorize?, :colorize

    # @param [String] banner @see banner
    # @param [String] error_message @see error_message
    #
    # @note  If an error message is provided, the exit status, used to
    #        terminate the program with, will be set to `1`, otherwise a {Help}
    #        exception is treated as not being a real error and exits with `0`.
    #
    def initialize(banner, error_message = nil, colorize = false)
      @banner = banner
      @error_message = error_message
      @colorize = colorize
      @exit_status = @error_message.nil? ? 0 : 1
    end

    # @return [String] The optional error message, colored in red if
    #         {Command.colorize_output} is set to `true`.
    #
    def formatted_error_message
      if error_message
        message = "[!] #{error_message}"
        colorize? ? message.red : message
      end
    end

    # @return [String] The optional error message, combined with the help
    #         banner of the command.
    #
    def message
      [formatted_error_message, banner].compact.join("\n\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
claide-0.4.0 lib/claide/help.rb
claide-0.3.2 lib/claide/help.rb
claide-0.3.1 lib/claide/help.rb