Class: Perennial::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/perennial/option_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionParser

Returns a new instance of OptionParser.



6
7
8
9
10
11
12
# File 'lib/perennial/option_parser.rb', line 6

def initialize
  @parsed_values = {}
  @arguments     = []
  @callbacks     = {}
  @descriptions  = {}
  @shortcuts     = {}
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



4
5
6
# File 'lib/perennial/option_parser.rb', line 4

def arguments
  @arguments
end

Class Method Details

.defaultObject



75
76
77
78
# File 'lib/perennial/option_parser.rb', line 75

def self.default
  return @default if defined?(@default) && @default.present?
  @default = setup_default!
end

.parse_argv(with = default) ⇒ Object



80
81
82
83
# File 'lib/perennial/option_parser.rb', line 80

def self.parse_argv(with = default)
  with.parse
  ARGV.replace with.arguments
end

Over ride with your apps custom banner



49
50
# File 'lib/perennial/option_parser.rb', line 49

def self.print_banner
end

.setup_default!Object



85
86
87
88
89
# File 'lib/perennial/option_parser.rb', line 85

def self.setup_default!
  opts = self.new
  opts.add_defaults!
  return opts
end

Instance Method Details

#add(name, description, opts = {}, &blk) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/perennial/option_parser.rb', line 14

def add(name, description, opts = {}, &blk)
  name = name.to_sym
  @callbacks[name] = blk
  @descriptions[name] = description
  shortcut = opts.has_key?(:shortcut) ? opts[:shortcut] : generate_default_shortcut(name)
  @shortcuts[shortcut] = name unless shortcut.blank?
end

#add_defaults!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/perennial/option_parser.rb', line 52

def add_defaults!
  return if defined?(@defaults_added) && @defaults_added
  logger_levels = Logger::LEVELS.keys.map { |k| k.to_s }
  add(:daemon, 'Runs this application as a daemon', :shortcut => "d") { Settings.daemon = true }
  add(:verbose, 'Runs this application verbosely, writing to STDOUT', :shortcut => "v") { Settings.verbose = true }
  add(:log_level, "Sets this applications log level, one of: #{logger_levels.join(", ")}", :shortcut => "l") do |level|
    if logger_levels.include?(level)
      Settings.log_level = level.to_sym
    else
      puts "The provided log level must be one of #{logger_levels.join(", ")} (Given #{level})"
      exit!
    end
  end
  add(:help, "Shows this help message") do
    self.print_banner
    $stdout.puts "Usage: #{$0} [options]"
    $stdout.puts "\nOptions:"
    $stdout.puts self.summary
    exit!
  end
  @defaults_added = true
end

#parse(arguments = ARGV) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/perennial/option_parser.rb', line 36

def parse(arguments = ARGV)
  arguments, options = ArgumentParser.parse(arguments)
  @arguments = arguments
  options.each_pair do |name, value|
    name = name.gsub("-", "_")
    expanded_name = @shortcuts[name] || name.to_sym
    callback = @callbacks[expanded_name]
    callback.call(value) if callback.present?
  end
  return nil
end

#summaryObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/perennial/option_parser.rb', line 22

def summary
  output = []
  max_length = 0
  @callbacks.each_key do |name|
    shortcuts = []
    @shortcuts.each_pair { |k,v| shortcuts << k if v == name }
    text = "--#{name.to_s.gsub("_", "-")}"
    text << ", #{shortcuts.map { |sc| "-#{sc}" }.join(", ")}" unless shortcuts.empty?
    max_length = [text.size, max_length].max
    output << [text, @descriptions[name]]
  end
  output.map { |text, description| "#{text.ljust(max_length)} - #{description}" }.join("\n")
end