class Ame::Flag

Represents a boolean option to a {Method} that doesn’t take an argument, though an argument is actually allowed if it’s made explicit by following the option name with a ‘=’ character and the argument value. Does the potential processing of the argument or simply returns the inverse {#default} value of the flag. @api developer

Attributes

default[R]

@return [Boolean] The default value of the receiver

description[R]

@return [String] The description of the receiver

long[R]

@return [String] The long name of the receiver

short[R]

@return [String] The short name of the receiver

Public Class Methods

new(short, long, default, description, &validate) click to toggle source

@api internal @param [String] short @param [String] long @param [Boolean] default @param [String] description @yield [?] @yieldparam [Hash<String, Object>] options @yieldparam [Boolean] value @raise [ArgumentError] If SHORT and LONG are strip#empty? @raise [ArgumentError] If SHORT#strip#length > 1

# File lib/ame-1.0/flag.rb, line 19
def initialize(short, long, default, description, &validate)
  @short, @long, @default, @description, @validate =
    (s = short.strip).empty? ? nil : s, (l = long.strip).empty? ? nil : l,
    default, description, validate || proc{ |_, a| a }
  raise ArgumentError, 'both short and long can’t be empty' if
    @short.nil? and @long.nil?
  raise ArgumentError, 'short can’t be longer than 1: %s' % @short if
    @short and @short.length > 1
end

Public Instance Methods

ignored?() click to toggle source

@return True if the receiver shouldn’t be included in the Hash of option

names and their values
# File lib/ame-1.0/flag.rb, line 71
def ignored?
  default.nil?
end
name() click to toggle source

@return [String] The long or short name of the option

# File lib/ame-1.0/flag.rb, line 60
def name
  @name ||= names.first
end
names() click to toggle source

@return [Array<String>] The long and/or short name of the option

# File lib/ame-1.0/flag.rb, line 65
def names
  @names ||= [long, short].reject{ |e| e.nil? }
end
process(options, arguments, name, explicit) click to toggle source

Invokes the optional block passed to the receiver when it was created for additional validation and parsing after optionally parsing one or more of the ARGUMENTS passed to it (see subclasses’ {#parse} methods for their behaviour). @api internal @param [Hash<String, Object>] options @param [Array<String>] arguments @param [String] name @raise [MissingArgument] If a required argument to an option is missing @raise [MalformedArgument] If an argument to an option can’t be parsed @return [Boolean]

# File lib/ame-1.0/flag.rb, line 40
def process(options, arguments, name, explicit)
  @validate.call(options, parse(arguments, explicit))
rescue Ame::MalformedArgument, ArgumentError, TypeError => e
  raise Ame::MalformedArgument, '%s: %s' % [name, e]
end
process_combined(options, arguments, name, remainder) click to toggle source

Invokes {#process} with arguments depending on whether REMAINDER, which is any content following a short option, should be seen as an argument to the receiver or not (see subclasses’ {#process_combined} methods for their behaviour), returning the result of {#process} and REMAINDER if it was used, or an empty String if it was. @api internal @param (see process) @param [String] remainder @return [[Boolean, String]]

# File lib/ame-1.0/flag.rb, line 55
def process_combined(options, arguments, name, remainder)
  [process(options, arguments, name, nil), remainder]
end

Private Instance Methods

parse(arguments, explicit) click to toggle source

Returns the parsed value of EXPLICIT, or the inverse of {#default} if nil. Should be overridden by subclasses that want different behaviour for missing arguments. @api internal @param [Array<String>] arguments @param [String, nil] explicit @return [Boolean] @raise [MalformedArgument] If EXPLICIT is non-nil and can’t be parsed

# File lib/ame-1.0/flag.rb, line 97
def parse(arguments, explicit)
  explicit ? Ame::Types[TrueClass].parse(explicit) : !default
end