class StackedConfig::Layers::CommandLineLayer

Attributes

extra_help[RW]
slop_definition[R]

Public Class Methods

new() click to toggle source
# File lib/stacked_config/layers/command_line_layer.rb, line 10
def initialize
  @slop_definition = Slop.new
  build_command_line_options
end

Public Instance Methods

add_command_line_section(title) { |slop_definition| ... } click to toggle source

Yields a slop definition to modify the command line parameters

# File lib/stacked_config/layers/command_line_layer.rb, line 30
def add_command_line_section(title)
  raise 'Incorrect usage' unless block_given?
  slop_definition.separator build_separator(title)
  yield slop_definition
  reload
end
extra_parameters() click to toggle source
# File lib/stacked_config/layers/command_line_layer.rb, line 42
def extra_parameters
  save = ARGV.dup
  return slop_definition.parse!.dup
ensure
  ARGV.replace save
end
help() click to toggle source

@return [String] The formatted command line help

# File lib/stacked_config/layers/command_line_layer.rb, line 38
def help
  slop_definition.to_s
end
load(*args) click to toggle source
# File lib/stacked_config/layers/command_line_layer.rb, line 15
def load(*args)
  slop_definition.parse
  slop_definition.banner = build_banner
  self.replace slop_definition.to_hash.delete_if {|k,v| v.nil?}
end
possible_options() click to toggle source
# File lib/stacked_config/layers/command_line_layer.rb, line 21
def possible_options
  slop_definition.to_hash.keys.sort
end
reload() click to toggle source
# File lib/stacked_config/layers/command_line_layer.rb, line 25
def reload
  load
end

Private Instance Methods

build_banner() click to toggle source
# File lib/stacked_config/layers/command_line_layer.rb, line 67
def build_banner
  if manager.nil?
    'No banner unless added to a manager !'
  else
    header = "\nUsage: #{manager.config_file_base_name} [options]\n#{manager.app_name} Version: #{manager.app_version}\n\n#{manager.app_description}"
    header += "\n#{extra_help}" unless extra_help.nil? or extra_help == ''
    header
  end
end
build_command_line_options() click to toggle source

Builds common used command line options

# File lib/stacked_config/layers/command_line_layer.rb, line 58
def build_command_line_options
  add_command_line_section('Generic options') do |slop|
    slop.on :auto, 'Auto mode. Bypasses questions to user.', :argument => false
    slop.on :simulate, 'Do not perform the actual underlying actions.', :argument => false
    slop.on :v, :verbose, 'Enable verbose mode.', :argument => false
    slop.on :h, :help, 'Displays this help.', :argument => false
  end
end
build_separator(title, width = 80, filler = '-') click to toggle source

 Builds a nice separator

# File lib/stacked_config/layers/command_line_layer.rb, line 53
def build_separator(title, width = 80, filler = '-')
  "#{filler * 2} #{title} ".ljust width, filler
end