Class: Ame::Flag

Inherits:
Object
  • Object
show all
Defined in:
lib/ame-1.0/flag.rb

Overview

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.

Direct Known Subclasses

Switch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(short, long, default, description) {|?| ... } ⇒ Flag

Returns a new instance of Flag.

Parameters:

  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Boolean)

Raises:

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



20
21
22
23
24
25
26
27
28
# File 'lib/ame-1.0/flag.rb', line 20

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

Instance Attribute Details

#defaultBoolean (readonly)

Returns The default value of the receiver.

Returns:

  • (Boolean)

    The default value of the receiver



83
84
85
# File 'lib/ame-1.0/flag.rb', line 83

def default
  @default
end

#descriptionString (readonly)

Returns The description of the receiver.

Returns:

  • (String)

    The description of the receiver



86
87
88
# File 'lib/ame-1.0/flag.rb', line 86

def description
  @description
end

#longString (readonly)

Returns The long name of the receiver.

Returns:

  • (String)

    The long name of the receiver



80
81
82
# File 'lib/ame-1.0/flag.rb', line 80

def long
  @long
end

#shortString (readonly)

Returns The short name of the receiver.

Returns:

  • (String)

    The short name of the receiver



77
78
79
# File 'lib/ame-1.0/flag.rb', line 77

def short
  @short
end

Instance Method Details

#ignored?Boolean

Returns True if the receiver shouldn’t be included in the Hash of option names and their values.

Returns:

  • (Boolean)

    True if the receiver shouldn’t be included in the Hash of option names and their values



72
73
74
# File 'lib/ame-1.0/flag.rb', line 72

def ignored?
  default.nil?
end

#nameString

Returns The long or short name of the option.

Returns:

  • (String)

    The long or short name of the option



61
62
63
# File 'lib/ame-1.0/flag.rb', line 61

def name
  @name ||= names.first
end

#namesArray<String>

Returns The long and/or short name of the option.

Returns:

  • (Array<String>)

    The long and/or short name of the option



66
67
68
# File 'lib/ame-1.0/flag.rb', line 66

def names
  @names ||= [long, short].reject{ |e| e.nil? }
end

#process(options, arguments, name, explicit) ⇒ Boolean

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).

Parameters:

  • options (Hash<String, Object>)
  • arguments (Array<String>)
  • name (String)

Returns:

  • (Boolean)

Raises:



41
42
43
44
45
# File 'lib/ame-1.0/flag.rb', line 41

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) ⇒ [Boolean, String]

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.

Parameters:

  • remainder (String)
  • options (Hash<String, Object>)
  • arguments (Array<String>)
  • name (String)

Returns:

  • ([Boolean, String])


56
57
58
# File 'lib/ame-1.0/flag.rb', line 56

def process_combined(options, arguments, name, remainder)
  [process(options, arguments, name, nil), remainder]
end