Module | Webgen::CLI |
In: |
lib/webgen/cli.rb
lib/webgen/cli/create_command.rb lib/webgen/cli/utils.rb lib/webgen/cli/apply_command.rb lib/webgen/cli/run_command.rb lib/webgen/cli/webgui_command.rb |
Namespace for all classes that act as CLI commands.
Each CLI command class needs to be put into this module and has to end with Command, otherwise it is not used. A CLI command is an extension that can be invoked from the webgen command and thus needs to be derived from CmdParse::Command. For detailed information on this class and the whole cmdparse package have a look at cmdparse.rubyforge.org!
Here is a sample CLI command extension which could be put, for example, into the ext/init.rb of a webgen website:
require 'webgen/cli' class Webgen::CLI::SampleCommand < CmdParse::Command def initialize super('sample', false) self.short_desc = "This sample command just outputs its parameters" self.description = Webgen::CLI::Utils.format("Uses the global verbosity level and outputs additional " + "information when the level is set to verbose!") @username = nil self.options = CmdParse::OptionParserWrapper.new do |opts| opts.separator "Options:" opts.on('-u', '--user USER', String, 'Specify an additional user name to output') {|username| @username = username} end end def execute(args) if args.length == 0 raise OptionParser::MissingArgument.new('ARG1 [ARG2 ...]') else puts "Command line arguments:" args.each {|arg| puts arg} if commandparser.verbosity == :verbose puts "Yeah, some additional information is always cool!" end puts "The entered username: #{@username}" if @username end end end
Note the use of Webgen::CLI::Utils.format in the initialize method so that the long text gets wrapped correctly! The Utils class provides some other useful methods, too!
For information about which attributes are available on the webgen command parser instance have a look at Webgen::CLI::CommandParser!
Return an array of lines which represents the text in content formatted sothat no line is longer than width characters. The indent parameter specifies the amount of spaces prepended to each line. If first_line_indented is true, then the first line is indented.
# File lib/webgen/cli/utils.rb, line 45 45: def self.format(content, indent = 0, first_line_indented = false, width = DEFAULT_WIDTH) 46: content = (content || '').dup 47: length = width - indent 48: 49: paragraphs = content.split(/\n\n/) 50: if (0..1) === paragraphs.length 51: pattern = /^(.{0,#{length}})[ \n]/m 52: lines = [] 53: while content.length > length 54: if content =~ pattern 55: str = $1 56: len = $&.length 57: else 58: str = content[0, length] 59: len = length 60: end 61: lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + str.gsub(/\n/, ' ') 62: content.slice!(0, len) 63: end 64: lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + content.gsub(/\n/, ' ') unless content.strip.empty? 65: lines 66: else 67: ((format(paragraphs.shift, indent, first_line_indented, width) << '') + 68: paragraphs.collect {|p| format(p, indent, true, width) << '' }).flatten[0..-2] 69: end 70: end
Creates a listing of the key-value pairs of hash under a section called name.
# File lib/webgen/cli/utils.rb, line 85 85: def self.hash_output(name, hash) 86: ljust = 20 87: puts section('Name', ljust) + "#{lred(name)}" 88: 89: hash.sort_by {|k,v| k.to_s }.each do |name, value| 90: next unless value.respond_to?(:to_str) 91: desc = format(value.to_str, ljust) 92: puts section(name.to_s.capitalize, ljust) + desc.shift 93: desc.each {|line| puts line} 94: end 95: puts 96: end
Tries to match name to a unique bundle name of the WebsiteManager wm. If this can not be done, it is checked whether name is actually a valid bundle URL and if so, the URL source is added to the bundles of wm.
Returns the correct bundle name or raises an error.
# File lib/webgen/cli/utils.rb, line 103 103: def self.match_bundle_name(wm, name) 104: matches = wm.bundles.keys.select {|k| k =~ /#{Regexp.escape(name)}/} 105: if matches.size > 1 106: raise ArgumentError.new("#{name} matches more than one bundle: #{matches.join(", ")}") 107: elsif matches.size == 0 108: begin 109: source = Webgen::Source::TarArchive.new(name) 110: wm.add_source(source, 'custom-URL-source') 111: name = 'custom-URL-source' 112: rescue 113: raise ArgumentError.new("#{name} is neither a valid bundle name nor a valid URL") 114: end 115: else 116: name = matches.first 117: end 118: name 119: end
Used for dynamically formatting the text (setting color, bold face, …).
# File lib/webgen/cli/utils.rb, line 34 34: def self.method_missing(id, text = nil) 35: if USE_ANSI_COLORS && Color.respond_to?(id) 36: Color.send(id, text.to_s) 37: else 38: text.to_s 39: end 40: end
Return a section header with the given text formatted in the given color and indented according to indent. The whole text is also left justified to the column specified with ljustlength.
# File lib/webgen/cli/utils.rb, line 80 80: def self.section(text, ljustlength = 0, indent = 4, color = :green) 81: ' '*indent + "#{send(color, text)}".ljust(ljustlength - indent + send(color).length) 82: end