class AstroboaCLI::Command::Base
Attributes
args[R]
log[R]
log_file[R]
options[R]
Public Class Methods
namespace()
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 37 def self.namespace self.to_s.split("::").last.downcase end
new(args=[], options={})
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 46 def initialize(args=[], options={}) @args = args @options = options @log_file = '/tmp/astroboa-cli-log.txt' @log = Logger.new(@log_file) @log.level = Logger::INFO # Check if the proper version of ruby is running ruby_ok? end
Protected Class Methods
extract_description(help)
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 144 def self.extract_description(help) lines = help.split("\n").map { |l| l.strip } lines.shift lines.reject do |line| line =~ /^-(.+)#(.+)/ end.join("\n").strip end
extract_help(file, line)
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 116 def self.extract_help(file, line) buffer = [] lines = File.read(file).split("\n") catch(:done) do (line.to_i-2).downto(0) do |i| case lines[i].strip[0..0] when "", "#" then buffer << lines[i] else throw(:done) end end end buffer.map! do |line| line.strip.gsub(/^#/, "") end buffer.reverse.join("\n").strip end
extract_help_from_caller(line)
click to toggle source
Parse the caller format and identify the file and line number as identified in : www.ruby-doc.org/core/classes/Kernel.html#M001397. This will look for a colon followed by a digit as the delimiter. The biggest complication is windows paths, which have a color after the drive letter. This regex will match paths as anything from the beginning to a colon directly followed by a number (the line number).
Examples of the caller format :
-
c:/Ruby192/lib/…/lib/astroboa-cli/command/server.rb:8:in ‘<module:Command>’
# File lib/astroboa-cli/command/base.rb, line 108 def self.extract_help_from_caller(line) # pull out of the caller the information for the file path and line number if line =~ /^(.+?):(\d+)/ return extract_help($1, $2) end raise "unable to extract help from caller: #{line}" end
extract_options(help)
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 152 def self.extract_options(help) help.split("\n").map { |l| l.strip }.select do |line| line =~ /^-(.+)#(.+)/ end.inject({}) do |hash, line| description = line.split("#", 2).last.strip long = line.match(/--([A-Za-z_\- ]+)/)[1].strip short = line.match(/-([A-Za-z ])/)[1].strip hash.update(long.split(" ").first => { :desc => description, :short => short, :long => long }) end end
extract_summary(help)
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 140 def self.extract_summary(help) extract_description(help).split("\n").first end
inherited(klass)
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 59 def self.inherited(klass) return if klass == AstroboaCLI::Command::Base help = extract_help_from_caller(caller.first) AstroboaCLI::Command.register_namespace( :name => klass.namespace, :description => help.split("\n").first, :long_description => help.split("\n") ) end
method_added(method)
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 71 def self.method_added(method) return if self == AstroboaCLI::Command::Base return if private_method_defined?(method) return if protected_method_defined?(method) help = extract_help_from_caller(caller.first) resolved_method = (method.to_s == "default") ? nil : method.to_s command = [ self.namespace, resolved_method ].compact.join(":") banner = extract_banner(help) || command permute = !banner.index("*") banner.gsub!("*", "") AstroboaCLI::Command.register_command( :klass => self, :method => method, :namespace => self.namespace, :command => command, :banner => banner, :help => help, :summary => extract_summary(help), :description => extract_description(help), :options => extract_options(help), :permute => permute ) end
Protected Instance Methods
extract_option(name, default=true) { |value| ... }
click to toggle source
# File lib/astroboa-cli/command/base.rb, line 163 def extract_option(name, default=true) key = name.gsub("--", "").to_sym return unless options[key] value = options[key] || default block_given? ? yield(value) : value end