Class: GLI::OptionParserFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/gli/option_parser_factory.rb

Overview

Factory for creating an OptionParser based on app configuration and DSL calls

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags, switches, accepts) ⇒ OptionParserFactory

Create an OptionParserFactory for the given flags, switches, and accepts



17
18
19
20
21
22
23
24
25
26
# File 'lib/gli/option_parser_factory.rb', line 17

def initialize(flags,switches,accepts)
  @flags = flags
  @switches = switches
  @options_hash = {}
  @option_parser = OptionParser.new do |opts|
    self.class.setup_accepts(opts,accepts)
    self.class.setup_options(opts,@switches,@options_hash)
    self.class.setup_options(opts,@flags,@options_hash)
  end
end

Instance Attribute Details

#option_parserObject (readonly)

Returns the value of attribute option_parser.



28
29
30
# File 'lib/gli/option_parser_factory.rb', line 28

def option_parser
  @option_parser
end

#options_hashObject (readonly)

Returns the value of attribute options_hash.



29
30
31
# File 'lib/gli/option_parser_factory.rb', line 29

def options_hash
  @options_hash
end

Class Method Details

.for_command(command, accepts) ⇒ Object

Create an option parser factory for a command. This has the added feature of setting up -h and –help on the command if those options aren’t otherwise configured, e.g. to allow todo add –help as an alternate to todo help add



9
10
11
12
13
# File 'lib/gli/option_parser_factory.rb', line 9

def self.for_command(command,accepts)
  self.new(command.flags,command.switches,accepts).tap { |factory|
    add_help_switches_to_command(factory.option_parser,command)
  }
end

Instance Method Details

#options_hash_with_defaults_set!Object



31
32
33
34
35
# File 'lib/gli/option_parser_factory.rb', line 31

def options_hash_with_defaults_set!
  set_defaults(@flags,@options_hash)
  set_defaults(@switches,@options_hash)
  @options_hash
end