Class: NumRu::Misc::KeywordOptAutoHelp

Inherits:
KeywordOpt
  • Object
show all
Defined in:
lib/numru/misc/keywordopt.rb

Overview

class NumRu::Misc::KeywordOptAutoHelp < NumRu::Misc::KeywordOpt

Same as class NumRu::Misc::KeywordOpt, but the method #interpret shows a help message and raise an exception if option ‘help’ is provided as an argument and is not nil or false (NumRu::Misc::HelpMessagingException < StandardError or if the arguments cannot be interpreted correctly (ArgumentError). Option ‘help’ is automatically defined, so you do not have to define it yourself.

Instance Method Summary collapse

Methods inherited from KeywordOpt

#[], #help, #keys, #select_existent

Constructor Details

#initialize(*args) ⇒ KeywordOptAutoHelp



334
335
336
337
# File 'lib/numru/misc/keywordopt.rb', line 334

def initialize(*args)
  args.push(['help',  false, 'show help message if true'])
  super(*args)
end

Instance Method Details

#interpret(hash) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/numru/misc/keywordopt.rb', line 339

def interpret(hash)
  begin
    out = super(hash)
  rescue
    raise $!.inspect + "\n  Available parameters are:\n" + help
  end
  if out['help']
    puts   "<< Description of options >>\n" + help
    puts ' Current values=' + out.inspect
    raise Misc::HelpMessagingException, '** help messaging done **'
  end
  out
end

#set(hash) ⇒ Object

Raises:

  • (ArgumentError)


353
354
355
356
357
358
359
360
# File 'lib/numru/misc/keywordopt.rb', line 353

def set(hash)
  raise ArgumentError, "not a hash" if !hash.is_a?(Hash)
  if hash['help']
    puts   "<< Description of options >>\n" + help
    raise Misc::HelpMessagingException, '** help messaging done **'
  end
  super
end