Class: Benry::CmdOpt::Schema
- Inherits:
-
Object
- Object
- Benry::CmdOpt::Schema
- Defined in:
- lib/benry/cmdopt.rb
Instance Method Summary collapse
- #add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, detail: nil, tag: nil, &callback) ⇒ Object
- #copy_from(other, except: []) ⇒ Object
- #delete(key) ⇒ Object
- #dup ⇒ Object
-
#each(&block) ⇒ Object
:nodoc:.
- #each_option_and_desc(all: false, &block) ⇒ Object (also: #each_option_help)
- #empty?(all: true) ⇒ Boolean
- #find_long_option(long) ⇒ Object
- #find_short_option(short) ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
- #option_help(width_or_format = nil, all: false) ⇒ Object (also: #to_s)
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
85 86 87 |
# File 'lib/benry/cmdopt.rb', line 85 def initialize() @items = [] end |
Instance Method Details
#add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, detail: nil, tag: nil, &callback) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/benry/cmdopt.rb', line 106 def add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, detail: nil, tag: nil, &callback) rexp ||= pattern # for backward compatibility #; [!kuhf9] type, rexp, enum, and range are can be passed as positional args as well as keyword args. rest.each do |x| case x when Class ; type ||= x when Regexp ; rexp ||= x when Array, Set ; enum ||= x when Range ; range ||= x else #; [!e3emy] raises error when positional arg is not one of class, regexp, array, nor range. raise _error("#{x.inspect}: Expected one of class, regexp, array or range, but got #{x.class.name}.") end end #; [!rhhji] raises SchemaError when key is not a Symbol. key.nil? || key.is_a?(Symbol) or raise _error("add(#{key.inspect}, #{optdef.inspect}): The first arg should be a Symbol as an option key.") #; [!vq6eq] raises SchemaError when help message is missing." desc.nil? || desc.is_a?(String) or raise _error("add(#{key.inspect}, #{optdef.inspect}): Help message required as 3rd argument.") #; [!7hi2d] takes command option definition string. short, long, param, required = parse_optdef(optdef) #; [!p9924] option key is omittable only when long option specified. #; [!jtp7z] raises SchemaError when key is nil and no long option. key || long or raise _error("add(#{key.inspect}, #{optdef.inspect}): Long option required when option key (1st arg) not specified.") #; [!rpl98] when long option is 'foo-bar' then key name is ':foo_bar'. key ||= long.gsub(/-/, '_').intern #; [!97sn0] raises SchemaError when ',' is missing between short and long options. if long.nil? && param =~ /\A--/ raise _error("add(#{key.inspect}, #{optdef.inspect}): Missing ',' between short option and long options.") end #; [!yht0v] keeps command option definitions. item = SchemaItem.new(key, optdef, desc, short, long, param, required, type: type, rexp: rexp, enum: enum, range: range, value: value, detail: detail, tag: tag, &callback) @items << item item end |
#copy_from(other, except: []) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/benry/cmdopt.rb', line 96 def copy_from(other, except: []) #; [!6six3] copy schema items from others. #; [!vt88s] copy schema items except items specified by 'except:' kwarg. except = [except].flatten() other.each do |item| @items << item unless except.include?(item.key) end self end |
#delete(key) ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/benry/cmdopt.rb', line 209 def delete(key) #; [!l86rb] deletes option item corresponding to key. #; [!rq0aa] returns deleted item. item = get(key) @items.delete_if {|item| item.key == key } return item end |
#dup ⇒ Object
89 90 91 92 93 94 |
# File 'lib/benry/cmdopt.rb', line 89 def dup() #; [!lxb0o] copies self object. other = self.class.new other.instance_variable_set(:@items, @items.dup) return other end |
#each(&block) ⇒ Object
:nodoc:
191 192 193 194 |
# File 'lib/benry/cmdopt.rb', line 191 def each(&block) # :nodoc: #; [!y4k1c] yields each option item. @items.each(&block) end |
#each_option_and_desc(all: false, &block) ⇒ Object Also known as: each_option_help
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/benry/cmdopt.rb', line 177 def each_option_and_desc(all: false, &block) #; [!03sux] returns enumerator object if block not given. return to_enum(:each_option_and_desc, all: all) unless block_given?() #; [!4b911] yields each optin definition str and help message. @items.each do |item| #; [!cl8zy] when 'all' flag is false, not yield hidden items. #; [!tc4bk] when 'all' flag is true, yields even hidden items. yield item.optdef, item.desc, item.detail if all || ! item.hidden? end #; [!zbxyv] returns self. self end |
#empty?(all: true) ⇒ Boolean
196 197 198 199 200 201 |
# File 'lib/benry/cmdopt.rb', line 196 def empty?(all: true) #; [!um8am] returns false if any item exists, else returns true. #; [!icvm1] ignores hidden items if 'all: false' kwarg specified. @items.each {|item| return false if all || ! item.hidden? } return true end |
#find_long_option(long) ⇒ Object
223 224 225 226 227 |
# File 'lib/benry/cmdopt.rb', line 223 def find_long_option(long) #; [!atmf9] returns option definition matched to long name. #; [!6haoo] returns nil when nothing found. return @items.find {|item| item.long == long } end |
#find_short_option(short) ⇒ Object
217 218 219 220 221 |
# File 'lib/benry/cmdopt.rb', line 217 def find_short_option(short) #; [!b4js1] returns option definition matched to short name. #; [!s4d1y] returns nil when nothing found. return @items.find {|item| item.short == short } end |
#get(key) ⇒ Object
203 204 205 206 207 |
# File 'lib/benry/cmdopt.rb', line 203 def get(key) #; [!3wjfp] finds option item object by key. #; [!0spll] returns nil if key not found. return @items.find {|item| item.key == key } end |
#option_help(width_or_format = nil, all: false) ⇒ Object Also known as: to_s
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/benry/cmdopt.rb', line 145 def option_help(width_or_format=nil, all: false) #; [!0aq0i] can take integer as width. #; [!pcsah] can take format string. #; [!dndpd] detects option width automatically when nothing specified. case width_or_format when nil ; format = _default_format() when Integer; format = " %-#{width_or_format}s : %s" when String ; format = width_or_format else raise ArgumentError.new("#{width_or_format.inspect}: Width (integer) or format (string) expected.") end #; [!v7z4x] skips option help if help message is not specified. #; [!to1th] includes all option help when `all` is true. #; [!a4qe4] option should not be hidden if description is empty string. sb = [] width = nil; indent = nil each_option_and_desc(all: all) do |opt, desc, detail| sb << format % [opt, desc || ""] << "\n" #; [!848rm] supports multi-lines help message. if detail width ||= (format % ['', '']).length indent ||= ' ' * width sb << detail.gsub(/^/, indent) sb << "\n" unless detail.end_with?("\n") end end return sb.join() end |