Class: Baf::OptionsRegistrant

Inherits:
Object
  • Object
show all
Defined in:
lib/baf/options_registrant.rb

Constant Summary collapse

SUMMARY_HEADER =
"\noptions:".freeze
HELP_PARSER_ARGS =
%w[
  -h
  --help
  print\ this\ message
].freeze.each &:freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = []) ⇒ OptionsRegistrant

Returns a new instance of OptionsRegistrant.



15
16
17
# File 'lib/baf/options_registrant.rb', line 15

def initialize options = []
  @options = options
end

Instance Attribute Details

#banner=(value) ⇒ Object

Sets the attribute banner

Parameters:

  • value

    the value to set the attribute banner to.



13
14
15
# File 'lib/baf/options_registrant.rb', line 13

def banner=(value)
  @banner = value
end

Instance Method Details

#flag(*args, **opts) ⇒ Object



19
20
21
# File 'lib/baf/options_registrant.rb', line 19

def flag *args, **opts
  options << Flag.new(*args, **opts)
end

#option(*args) ⇒ Object



23
24
25
# File 'lib/baf/options_registrant.rb', line 23

def option *args
  options << Option.new(*args)
end

#register(env, parser) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/baf/options_registrant.rb', line 27

def register env, parser
  yield if block_given?
  parser.banner = banner if banner
  parser.separator SUMMARY_HEADER
  options_tail, options_standard = options.partition &:tail?
  options_standard.each { |opt| register_option env, parser, opt }
  register_default_options env, parser
  options_tail.each { |opt| register_option env, parser, opt }
end