Module: ConsoleAppSupport::ConsoleAppMixin

Included in:
ConsoleAppHelper
Defined in:
lib/consoleappmixin.rb

Constant Summary collapse

OPTIONS_LINE_EXPRESSION =

Regexp used to read application options from the main source file

%r{^\s*#\s*-([\w-]+)\s*,\s*--([\w-]+)\s*(\S.*)$}
OPTION_VALUE_EXPRESSION =

Regexp used to determine the Ruby type of each application option

%r{^\s*\[(\w+)\]}
DEFAULT_VALUE_EXPRESSION =

Regexp used to determine the default value of each application option

%r{default[^=]*=\s*(.*\S)\s*$}i
REQUIRED_VALUE_EXPRESSION =

Regexp used to determine if application option is required

%r{required}i
SPECIAL_OPTIONS =

Predefined names for application options that trigger this module’s specialized behavior:

  • :help - Prints usage and exits

  • :config_file - Selects a config file for more options

  • :log_level - Sets the STDOUT logging level

  • :log4r_file - Loads a Log4r config file

{
  :help         => "help",
  :config_file  => "config_file",
  :log_level    => "log_level",
  :log4r_file   => "log4r_file",
}
INITIAL_LOG_LEVEL =

Logging level during module initilization. Redefine this to DEBUG before calling super if you need to trace parsing of the main application source file.

'INFO'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_optionsObject (readonly)

An array of symbols representing the names of the application’s possible configuration options.



40
41
42
# File 'lib/consoleappmixin.rb', line 40

def app_options
  @app_options
end

#commandline_optionsObject (readonly)

Hash containing the command-line values, indexed by app_options



48
49
50
# File 'lib/consoleappmixin.rb', line 48

def commandline_options
  @commandline_options
end

#configfile_optionsObject (readonly)

Hash containing the config file values, indexed by app_options



46
47
48
# File 'lib/consoleappmixin.rb', line 46

def configfile_options
  @configfile_options
end

#default_optionsObject (readonly)

Hash of containing the default configuration values, indexed by app_options



44
45
46
# File 'lib/consoleappmixin.rb', line 44

def default_options
  @default_options
end

#logObject (readonly)

A Log4r logger, initially pointed to STDOUT at level INITIAL_LOG_LEVEL



51
52
53
# File 'lib/consoleappmixin.rb', line 51

def log
  @log
end

#optsObject (readonly)

Hash containing the final configuration values, indexed by app_options



42
43
44
# File 'lib/consoleappmixin.rb', line 42

def opts
  @opts
end

Instance Method Details

#initialize(logger_name = self.class.name, skip_config = false) ⇒ Object

Parses the top-level source file for application configuration options. Unless skip_config is true, read_configuration_files is then called.



82
83
84
85
86
87
88
89
90
91
# File 'lib/consoleappmixin.rb', line 82

def initialize(logger_name = self.class.name, skip_config = false)
  @app_options = []
  @opts = @configfile_options = {}
  initialize_logging logger_name
  populate_app_options
  setup_default_options
  parse_command_line
  read_configuration_files unless skip_config
  verify_required_options
end

#read_configuration_filesObject

Reads the application config file for configuration options, then loads the Log4r config file (as directed by :log4r_file option). Returns @opts, the final runtime_options hash.



96
97
98
99
100
101
102
103
104
# File 'lib/consoleappmixin.rb', line 96

def read_configuration_files
  config_file = get_config_file_name
  load_config_file(config_file) if config_file_exists? config_file
  @log.debug "Config file options loaded: #{@configfile_options.inspect}"
  @opts = @default_options.merge(@configfile_options.merge(@commandline_options))
  @log.debug "Final run-time options: #{@opts.inspect}"
  load_log4r_config_file
  @opts
end