Class: PVN::BaseOption
- Inherits:
-
Object
- Object
- PVN::BaseOption
- Includes:
- Loggable
- Defined in:
- lib/synoption/base_option.rb
Direct Known Subclasses
Constant Summary collapse
- NO_CMDLINE_OPTION =
for as_cmdline_option:
Object.new
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#negate ⇒ Object
readonly
Returns the value of attribute negate.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #exact_match?(arg) ⇒ Boolean
-
#initialize(name, tag, description, default, options = Hash.new) ⇒ BaseOption
constructor
A new instance of BaseOption.
- #negative_match?(arg) ⇒ Boolean
- #next_argument(args) ⇒ Object
- #post_process(option_set, unprocessed) ⇒ Object
- #process(args) ⇒ Object
- #regexp_match?(arg) ⇒ Boolean
- #resolve_value(option_set, unprocessed) ⇒ Object
- #set_value(val) ⇒ Object
- #takes_value? ⇒ Boolean
- #to_command_line ⇒ Object
- #to_doc(io) ⇒ Object
- #to_s ⇒ Object
- #unset ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(name, tag, description, default, options = Hash.new) ⇒ BaseOption
Returns a new instance of BaseOption.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/synoption/base_option.rb', line 25 def initialize name, tag, description, default, = Hash.new @name = name @tag = tag @description = description @value = @default = default @matchers = Matchers.new @tag, @name, [:negate], [:regexp] @negate = [:negate] @regexp = [:regexp] if .include? :as_cmdline_option if [:as_cmdline_option].nil? @as_cmdline_option = NO_CMDLINE_OPTION else @as_cmdline_option = [:as_cmdline_option] end else @as_cmdline_option = nil end @unsets = [:unsets] end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
20 21 22 |
# File 'lib/synoption/base_option.rb', line 20 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
19 20 21 |
# File 'lib/synoption/base_option.rb', line 19 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/synoption/base_option.rb', line 17 def name @name end |
#negate ⇒ Object (readonly)
Returns the value of attribute negate.
22 23 24 |
# File 'lib/synoption/base_option.rb', line 22 def negate @negate end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
23 24 25 |
# File 'lib/synoption/base_option.rb', line 23 def regexp @regexp end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
18 19 20 |
# File 'lib/synoption/base_option.rb', line 18 def tag @tag end |
Instance Method Details
#exact_match?(arg) ⇒ Boolean
68 69 70 |
# File 'lib/synoption/base_option.rb', line 68 def exact_match? arg @matchers.exact.match? arg end |
#negative_match?(arg) ⇒ Boolean
72 73 74 |
# File 'lib/synoption/base_option.rb', line 72 def negative_match? arg @matchers.negative and @matchers.negative.match? arg end |
#next_argument(args) ⇒ Object
97 98 99 100 |
# File 'lib/synoption/base_option.rb', line 97 def next_argument args raise "ERROR: option #{name} expecting following argument" if args.empty? args.shift end |
#post_process(option_set, unprocessed) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/synoption/base_option.rb', line 121 def post_process option_set, unprocessed resolve_value option_set, unprocessed if @unsets option_set.unset @unsets end end |
#process(args) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/synoption/base_option.rb', line 102 def process args if @matchers.exact.match? args[0] args.shift val = takes_value? ? next_argument(args) : true set_value val true elsif @matchers.negative && @matchers.negative.match?(args[0]) arg = args.shift set_value false true elsif @matchers.regexp && (md = @matchers.regexp.match?(args[0])) arg = args.shift set_value md[0] true else false end end |
#regexp_match?(arg) ⇒ Boolean
76 77 78 |
# File 'lib/synoption/base_option.rb', line 76 def regexp_match? arg @matchers.regexp and @matchers.regexp.match? arg end |
#resolve_value(option_set, unprocessed) ⇒ Object
129 130 |
# File 'lib/synoption/base_option.rb', line 129 def resolve_value option_set, unprocessed end |
#set_value(val) ⇒ Object
84 85 86 |
# File 'lib/synoption/base_option.rb', line 84 def set_value val @value = val end |
#takes_value? ⇒ Boolean
50 51 52 |
# File 'lib/synoption/base_option.rb', line 50 def takes_value? true end |
#to_command_line ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/synoption/base_option.rb', line 54 def to_command_line return nil unless value if @as_cmdline_option @as_cmdline_option == NO_CMDLINE_OPTION ? nil : @as_cmdline_option else [ tag, value ] end end |
#to_doc(io) ⇒ Object
92 93 94 95 |
# File 'lib/synoption/base_option.rb', line 92 def to_doc io doc = Doc.new self doc.to_doc io end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/synoption/base_option.rb', line 64 def to_s [ @name, @tag ].join(", ") end |
#unset ⇒ Object
80 81 82 |
# File 'lib/synoption/base_option.rb', line 80 def unset @value = nil end |
#value ⇒ Object
88 89 90 |
# File 'lib/synoption/base_option.rb', line 88 def value @value end |