Class: ArgsHelper
- Inherits:
-
Object
- Object
- ArgsHelper
- Defined in:
- lib/argshelper.rb
Overview
Instance Attribute Summary collapse
-
#BiHash ⇒ Object
readonly
Returns the value of attribute BiHash.
Instance Method Summary collapse
-
#add_keys(skey = nil, lkey = nil, kval = nil, kdescription = nil) ⇒ Object
Add argument keys.
-
#add_static_flag_opts(sflag = nil, lflag = nil, opts = []) ⇒ Object
Define specific options available for a certain flag.
-
#get_value(flag) ⇒ String?
Returns the value of a given flag.
-
#has_arg?(flag) ⇒ Boolean
Returns whether a flag has been found.
-
#initialize(args) ⇒ ArgsHelper
constructor
Initialize the helper.
-
#parse_args ⇒ Object
Main processing of argument flags Handles errors in flags and flag values.
-
#set_no_value(sflag = nil, lflag = nil) ⇒ Object
Flag doesn’t have a corresponding value.
-
#show_default_table ⇒ Object
Display a help table.
-
#show_table(title, pad, *cols) ⇒ Object
Display contents in a table.
Constructor Details
#initialize(args) ⇒ ArgsHelper
Initialize the helper
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/argshelper.rb', line 47 def initialize(args) @args_a = args @args = nil @skeys = [] @lkeys = [] @kvals = [] @kdescriptions = [] @static = {} @no_vals = [] @flag_pairs = BiHash.new end |
Instance Attribute Details
#BiHash ⇒ Object (readonly)
Returns the value of attribute BiHash.
43 44 45 |
# File 'lib/argshelper.rb', line 43 def BiHash @BiHash end |
Instance Method Details
#add_keys(skey = nil, lkey = nil, kval = nil, kdescription = nil) ⇒ Object
Add argument keys
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 |
# File 'lib/argshelper.rb', line 81 def add_keys(skey = nil, lkey = nil, kval = nil, kdescription = nil) arrays = [ skey.is_a?(Array) || skey == nil, lkey.is_a?(Array) || lkey == nil, kval.is_a?(Array) || kval == nil, kdescription.is_a?(Array) || kdescription == nil ] strings = [ skey.is_a?(String) || skey == nil, lkey.is_a?(String) || lkey == nil, kval.is_a?(String) || kval == nil, kdescription.is_a?(String) || kdescription == nil ] if arrays.include?(true) && arrays.include?(false) puts "Mismatched types: Expected either Arrays or Strings" exit end if arrays.include?(true) skey = [] if skey == nil lkey = [] if lkey == nil kval = [] if kval == nil kdescription = [] if kdescription == nil if skey != [] skey.each_with_index do |sk, i| if lkey != [] @flag_pairs.insert(sk, lkey[i]) else @flag_pairs.insert(sk, nil) end end end if lkey != [] && skey == [] lkey.each do |lk| @flag_pairs.insert(lk, nil) end end @skeys = skey @lkeys = lkey @kvals = kval @kdescriptions = kdescription return end if strings.include?(true) skey = '' if skey == nil lkey = '' if lkey == nil kval = '' if kval == nil kdescription = '' if kdescription == nil @skeys.push(skey) @lkeys.push(lkey) @kvals.push(kval) @kdescriptions.push(kdescription) if skey != '' @flag_pairs.insert(skey, lkey) end if lkey != '' && skey == '' @flag_pairs.insert(lkey, skey) end return end end |
#add_static_flag_opts(sflag = nil, lflag = nil, opts = []) ⇒ Object
Define specific options available for a certain flag
66 67 68 69 70 71 72 73 74 |
# File 'lib/argshelper.rb', line 66 def add_static_flag_opts(sflag = nil, lflag = nil, opts = []) if sflag != nil @static[sflag] = opts end if lflag != nil @static[lflag] = opts end end |
#get_value(flag) ⇒ String?
Returns the value of a given flag
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/argshelper.rb', line 212 def get_value(flag) fp = @flag_pairs.get_value(flag) if !@no_vals.include?(flag) if @args.has_key?(flag) return @args[flag] elsif @args.has_key?(fp) && fp != nil return @args[fp] else return nil end else return nil end end |
#has_arg?(flag) ⇒ Boolean
Returns whether a flag has been found
204 205 206 207 |
# File 'lib/argshelper.rb', line 204 def has_arg?(flag) fp = @flag_pairs.get_value(flag) return fp != nil && (@args_a.include?(flag) || @args_a.include?(fp)) end |
#parse_args ⇒ Object
Main processing of argument flags Handles errors in flags and flag values
167 168 169 170 171 172 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 |
# File 'lib/argshelper.rb', line 167 def parse_args @args = @args_a.each_slice(2).to_a.inject({}) { |h, k| h[k[0]] = k[1]; h } keys = @skeys + @lkeys @args.each do |k, v| if !keys.include?(k) puts "Unknown option `#{k}'" exit end if keys.include?(v) puts "Missing values for `#{k}' and `#{v}'" exit end if v != nil if v.start_with?('-') puts "Warning: Value of `#{k}' appears to be a flag" end if @static.has_key?(k) if !@static[k].include?(v) puts "Unknown option `#{v}' for `#{k}'" exit end end end end if remove_keys(@no_vals).has_blank? puts "Missing argument(s)" exit end end |
#set_no_value(sflag = nil, lflag = nil) ⇒ Object
Flag doesn’t have a corresponding value
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/argshelper.rb', line 143 def set_no_value(sflag = nil, lflag = nil) if sflag == nil && lflag == nil return end if sflag != nil if @args_a.include?(sflag) index = @args_a.find_index(sflag) @args_a = @args_a.insert(index + 1, nil) @no_vals.push(sflag) end end if lflag != nil if @args_a.include?(lflag) index = @args_a.find_index(lflag) @args_a = @args_a.insert(index + 1, nil) @no_vals.push(lflag) end end end |
#show_default_table ⇒ Object
Display a help table
228 229 230 |
# File 'lib/argshelper.rb', line 228 def show_default_table show_table("Options", 2, @skeys, @lkeys, @kvals, @kdescriptions) end |
#show_table(title, pad, *cols) ⇒ Object
Display contents in a table
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/argshelper.rb', line 236 def show_table(title, pad, *cols) mlcols = [] cols = cols.map { |x| x == [] ? nil : x } cols.compact! cols.each do |c| if c.is_a? Array mlcols.push(c.map(&:to_s).max_by(&:length).length) elsif c.is_a? String mlcols.push(c.length) elsif c.is_a? Numeric mlcols.push(c.to_s.length) end end clens = [] mlcols.each { |m| clens.push((pad * 2) + m) } rows = cols.transpose sum = clens.inject(0){ |s, x| s + x } sum += cols.length - 1 tlen = title.length tpadl = (sum - tlen) / 2 tpadr = sum - (tlen + tpadl) print '+' print '-' * sum puts '+' print '|' print ' ' * tpadl print title print ' ' * tpadr puts '|' clens.each do |l| print '+' print '-' * l end puts '+' rows.each do |row| row.each_with_index do |col, i| len = clens[i] - col.length - pad print '|' print ' ' * pad print col print ' ' * len end puts '|' end clens.each do |l| print '+' print '-' * l end puts '+' end |