Class: Perennial::OptionParser
- Includes:
- Singleton
- Defined in:
- lib/perennial/option_parser.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
Class Method Summary collapse
- .method_missing(name, *args, &blk) ⇒ Object
-
.print_banner ⇒ Object
Over ride with your apps custom banner.
- .setup! ⇒ Object
Instance Method Summary collapse
- #add(name, description, opts = {}, &blk) ⇒ Object
- #add_defaults! ⇒ Object
-
#initialize ⇒ OptionParser
constructor
A new instance of OptionParser.
- #parse(arguments = ARGV) ⇒ Object
- #setup ⇒ Object
- #summary ⇒ Object
Constructor Details
#initialize ⇒ OptionParser
Returns a new instance of OptionParser.
8 9 10 11 12 13 14 |
# File 'lib/perennial/option_parser.rb', line 8 def initialize @parsed_values = {} @arguments = [] @callbacks = {} @descriptions = {} @shortcuts = {} end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
6 7 8 |
# File 'lib/perennial/option_parser.rb', line 6 def arguments @arguments end |
Class Method Details
.method_missing(name, *args, &blk) ⇒ Object
50 51 52 |
# File 'lib/perennial/option_parser.rb', line 50 def self.method_missing(name, *args, &blk) self.instance.send(name, *args, &blk) end |
.print_banner ⇒ Object
Over ride with your apps custom banner
55 56 |
# File 'lib/perennial/option_parser.rb', line 55 def self. end |
.setup! ⇒ Object
87 88 89 90 91 92 |
# File 'lib/perennial/option_parser.rb', line 87 def self.setup! opts = self.instance opts.add_defaults! opts.parse ARGV.replace opts.arguments end |
Instance Method Details
#add(name, description, opts = {}, &blk) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/perennial/option_parser.rb', line 16 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/perennial/option_parser.rb', line 58 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') { Settings.daemon = true } add(:verbose, 'Runs this application verbosely, writing to STDOUT') { Settings.verbose = true } add(:log_level, "Sets this applications log level, one of: #{logger_levels.join(", ")}") 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. $stdout.puts "Usage: #{$0} [options]" $stdout.puts "\nOptions:" $stdout.puts self.summary exit! end @defaults_added = true end |
#parse(arguments = ARGV) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/perennial/option_parser.rb', line 38 def parse(arguments = ARGV) arguments, = ArgumentParser.parse(arguments) @arguments = arguments .each_pair do |name, value| name = name.gsub("-", "_") = @shortcuts[name] || name.to_sym callback = @callbacks[] callback.call(value) if callback.present? end return nil end |
#setup ⇒ Object
81 82 83 84 85 |
# File 'lib/perennial/option_parser.rb', line 81 def setup return if defined?(@setup) && @setup setup! @setup = true end |
#summary ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/perennial/option_parser.rb', line 24 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 + 2) + description }.join("\n") end |