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, multiple: nil, detail: nil, hidden: nil, important: nil, tag: nil, &callback) ⇒ Object
- #add_item(item) ⇒ 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.
86 87 88 |
# File 'lib/benry/cmdopt.rb', line 86 def initialize() @items = [] end |
Instance Method Details
#add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, multiple: nil, detail: nil, hidden: nil, important: nil, tag: nil, &callback) ⇒ Object
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 144 |
# File 'lib/benry/cmdopt.rb', line 107 def add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, multiple: nil, detail: nil, hidden: nil, important: 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, multiple: multiple, detail: detail, hidden: hidden, important: important, tag: tag, &callback) add_item(item) item end |
#add_item(item) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/benry/cmdopt.rb', line 146 def add_item(item) #; [!qyjp9] raises SchemaError if invalid item added. errmsg = _validate_item(item) errmsg == nil or raise SchemaError.new(errmsg) #; [!a693h] adds option item into current schema. @items << item self end |
#copy_from(other, except: []) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/benry/cmdopt.rb', line 97 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
243 244 245 246 247 248 249 |
# File 'lib/benry/cmdopt.rb', line 243 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
90 91 92 93 94 95 |
# File 'lib/benry/cmdopt.rb', line 90 def dup() #; [!lxb0o] copies self object. other = self.class.new other.instance_variable_set(:@items, @items.dup) return other end |
#each(&block) ⇒ Object
:nodoc:
225 226 227 228 |
# File 'lib/benry/cmdopt.rb', line 225 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
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/benry/cmdopt.rb', line 211 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
230 231 232 233 234 235 |
# File 'lib/benry/cmdopt.rb', line 230 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
257 258 259 260 261 |
# File 'lib/benry/cmdopt.rb', line 257 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
251 252 253 254 255 |
# File 'lib/benry/cmdopt.rb', line 251 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
237 238 239 240 241 |
# File 'lib/benry/cmdopt.rb', line 237 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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/benry/cmdopt.rb', line 173 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; color_p = $stdout.tty? @items.each do |item| next if ! all && item.hidden? #; [!jrwb6] decorates help message according to `important:` value of option. #; [!9nlfb] not decorate help message when stdout is not a tty. s = format % [item.optdef, item.desc || ""] s = _decorate_str(s, item.important?) if color_p sb << s << "\n" #; [!848rm] supports multi-lines help message. detail = item.detail if detail width ||= (format % ['', '']).length indent ||= ' ' * width sb << detail.gsub(/^/, indent) sb << "\n" unless detail.end_with?("\n") end end return sb.join() end |