Class Index [+]

Quicksearch

Webgen::CLI

Namespace for all classes that act as CLI commands.

Implementing a CLI command

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!

Sample CLI command

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!

Public Class Methods

format(content, indent = 0, first_line_indented = false, width = DEFAULT_WIDTH) click to toggle source

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 44
44:     def self.format(content, indent = 0, first_line_indented = false, width = DEFAULT_WIDTH)
45:       content = (content || '').dup
46:       length = width - indent
47: 
48:       paragraphs = content.split(/\n\n/)
49:       if (0..1) === paragraphs.length
50:         pattern = /^(.{0,#{length}})[ \n]/
51:         lines = []
52:         while content.length > length
53:           if content =~ pattern
54:             str = $1
55:             len = $&.length
56:           else
57:             str = content[0, length]
58:             len = length
59:           end
60:           lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + str.gsub(/\n/, ' ')
61:           content.slice!(0, len)
62:         end
63:         lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + content.gsub(/\n/, ' ') unless content.strip.empty?
64:         lines
65:       else
66:         ((format(paragraphs.shift, indent, first_line_indented, width) << '') +
67:          paragraphs.collect {|p| format(p, indent, true, width) << '' }).flatten[0..2]
68:       end
69:     end
hash_output(name, hash) click to toggle source

Creates a listing of the key-value pairs of hash under a section called name.

    # File lib/webgen/cli/utils.rb, line 84
84:     def self.hash_output(name, hash)
85:       ljust = 20
86:       puts section('Name', ljust) + "#{lred(name)}"
87: 
88:       hash.sort_by {|k,v| k.to_s }.each do |name, value|
89:         next unless value.respond_to?(:to_str)
90:         desc = format(value.to_str, ljust)
91:         puts section(name.to_s.capitalize, ljust) + desc.shift
92:         desc.each {|line| puts line}
93:       end
94:       puts
95:     end
headline(text, indent = 2) click to toggle source

Return a headline with the given text and amount of indent.

    # File lib/webgen/cli/utils.rb, line 72
72:     def self.headline(text, indent = 2)
73:       ' '*indent + "#{bold(text)}"
74:     end
match_bundle_name(wm, name) click to toggle source

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 102
102:     def self.match_bundle_name(wm, name)
103:       matches = wm.bundles.keys.select {|k| k =~ /#{Regexp.escape(name)}/}
104:       if matches.size > 1
105:         raise ArgumentError.new("#{name} matches more than one bundle: #{matches.join(", ")}")
106:       elsif matches.size == 0
107:         begin
108:           source = Webgen::Source::TarArchive.new(name)
109:           wm.add_source(source, 'custom-URL-source')
110:           name = 'custom-URL-source'
111:         rescue
112:           raise ArgumentError.new("#{name} is neither a valid bundle name nor a valid URL")
113:         end
114:       else
115:         name = matches.first
116:       end
117:       name
118:     end
method_missing(id, text = nil) click to toggle source

Used for dynamically formatting the text (setting color, bold face, …).

    # File lib/webgen/cli/utils.rb, line 33
33:     def self.method_missing(id, text = nil)
34:       if USE_ANSI_COLORS && Color.respond_to?(id)
35:         Color.send(id, text.to_s)
36:       else
37:         text.to_s
38:       end
39:     end
section(text, ljustlength = 0, indent = 4, color = :green) click to toggle source

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 79
79:     def self.section(text, ljustlength = 0, indent = 4, color = :green)
80:       ' '*indent + "#{send(color, text)}".ljust(ljustlength - indent + send(color).length)
81:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.