Class: Ame::Flag
- Inherits:
-
Object
- Object
- Ame::Flag
- 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
Instance Attribute Summary collapse
-
#default ⇒ Boolean
readonly
The default value of the receiver.
-
#description ⇒ String
readonly
The description of the receiver.
-
#long ⇒ String
readonly
The long name of the receiver.
-
#short ⇒ String
readonly
The short name of the receiver.
Instance Method Summary collapse
-
#ignored? ⇒ Boolean
True if the receiver shouldn’t be included in the Hash of option names and their values.
-
#initialize(short, long, default, description) {|?| ... } ⇒ Flag
constructor
A new instance of Flag.
-
#name ⇒ String
The long or short name of the option.
-
#names ⇒ Array<String>
The long and/or short name of the option.
-
#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).
-
#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.
Constructor Details
#initialize(short, long, default, description) {|?| ... } ⇒ Flag
Returns a new instance of Flag.
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
#default ⇒ Boolean (readonly)
Returns The default value of the receiver.
83 84 85 |
# File 'lib/ame-1.0/flag.rb', line 83 def default @default end |
#description ⇒ String (readonly)
Returns The description of the receiver.
86 87 88 |
# File 'lib/ame-1.0/flag.rb', line 86 def description @description end |
#long ⇒ String (readonly)
Returns The long name of the receiver.
80 81 82 |
# File 'lib/ame-1.0/flag.rb', line 80 def long @long end |
#short ⇒ String (readonly)
Returns 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.
72 73 74 |
# File 'lib/ame-1.0/flag.rb', line 72 def ignored? default.nil? end |
#name ⇒ String
Returns 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 |
#names ⇒ Array<String>
Returns 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).
41 42 43 44 45 |
# File 'lib/ame-1.0/flag.rb', line 41 def process(, arguments, name, explicit) @validate.call(, 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.
56 57 58 |
# File 'lib/ame-1.0/flag.rb', line 56 def process_combined(, arguments, name, remainder) [process(, arguments, name, nil), remainder] end |