class Toys::StandardMiddleware::AddVerbosityFlags

A middleware that provides flags for editing the verbosity.

This middleware adds `-v`, `–verbose`, `-q`, and `–quiet` flags, if not already defined by the tool. These flags affect the setting of {Toys::Context::Key::VERBOSITY}, and, thus, the logger level.

Constants

DECREMENT_HANDLER
DEFAULT_QUIET_FLAGS

Default quiet flags @return [Array<String>]

DEFAULT_VERBOSE_FLAGS

Default verbose flags @return [Array<String>]

INCREMENT_HANDLER

Public Class Methods

new(verbose_flags: true, quiet_flags: true) click to toggle source

Create a AddVerbosityFlags middleware.

@param verbose_flags [Boolean,Array<String>,Proc] Specify flags

to increase verbosity. The value may be any of the following:

*  An array of flags that increase verbosity.
*  The `true` value to use {DEFAULT_VERBOSE_FLAGS}. (Default)
*  The `false` value to disable verbose flags.
*  A proc that takes a tool and returns any of the above.

@param quiet_flags [Boolean,Array<String>,Proc] Specify flags

to decrease verbosity. The value may be any of the following:

*  An array of flags that decrease verbosity.
*  The `true` value to use {DEFAULT_QUIET_FLAGS}. (Default)
*  The `false` value to disable quiet flags.
*  A proc that takes a tool and returns any of the above.
# File lib/toys/standard_middleware/add_verbosity_flags.rb, line 44
def initialize(verbose_flags: true, quiet_flags: true)
  @verbose_flags = verbose_flags
  @quiet_flags = quiet_flags
end

Public Instance Methods

config(tool, _loader) { || ... } click to toggle source

Configure the tool flags. @private

# File lib/toys/standard_middleware/add_verbosity_flags.rb, line 53
def config(tool, _loader)
  unless tool.argument_parsing_disabled?
    StandardMiddleware.append_common_flag_group(tool)
    add_verbose_flags(tool)
    add_quiet_flags(tool)
  end
  yield
end

Private Instance Methods

add_quiet_flags(tool) click to toggle source
# File lib/toys/standard_middleware/add_verbosity_flags.rb, line 81
def add_quiet_flags(tool)
  quiet_flags = resolve_flags_spec(@quiet_flags, tool, DEFAULT_QUIET_FLAGS)
  unless quiet_flags.empty?
    tool.add_flag(
      Context::Key::VERBOSITY, quiet_flags,
      report_collisions: false,
      handler: DECREMENT_HANDLER,
      desc: "Decrease verbosity",
      long_desc: "Decrease verbosity, causing fewer logging levels to display.",
      group: StandardMiddleware::COMMON_FLAG_GROUP
    )
  end
end
add_verbose_flags(tool) click to toggle source
# File lib/toys/standard_middleware/add_verbosity_flags.rb, line 67
def add_verbose_flags(tool)
  verbose_flags = resolve_flags_spec(@verbose_flags, tool, DEFAULT_VERBOSE_FLAGS)
  unless verbose_flags.empty?
    tool.add_flag(
      Context::Key::VERBOSITY, verbose_flags,
      report_collisions: false,
      handler: INCREMENT_HANDLER,
      desc: "Increase verbosity",
      long_desc: "Increase verbosity, causing additional logging levels to display.",
      group: StandardMiddleware::COMMON_FLAG_GROUP
    )
  end
end
resolve_flags_spec(flags, tool, defaults) click to toggle source
# File lib/toys/standard_middleware/add_verbosity_flags.rb, line 95
def resolve_flags_spec(flags, tool, defaults)
  flags = flags.call(tool) if flags.respond_to?(:call)
  case flags
  when true, :default
    Array(defaults)
  when ::String
    [flags]
  when ::Array
    flags
  else
    []
  end
end