class CTioga2::Commands::Documentation::Doc

The base class for all documentation.

Attributes

backends[RW]

The hash containing all the backends, as returned by Data::Backends::Backend::list_backends

commands[RW]

The hash containing all the commands, as returned by Interpreter::commands.

functions[RW]

The functions

groups[RW]

The hash containing all the groups, as returned by Interpreter::commands.

ignore_blacklisted[RW]

Wether or not to ignore blacklisted commands

types[RW]

The hash containing all the types, as returned by Interpreter::commands.

Public Class Methods

new() click to toggle source

Create a Doc object caring about the current state of registered commands and such.

# File lib/ctioga2/commands/doc/doc.rb, line 52
def initialize
  @commands = Interpreter::commands
  @groups = Interpreter::groups
  @types = Interpreter::types
  @backends = Data::Backends::Backend::list_backends
  @functions = Function::functions

  @ignore_blacklisted = ! (ENV.key?("CT2_DEV") && 
                           ! ENV["CT2_DEV"].empty?)
end

Public Instance Methods

display_command_line_help(options) click to toggle source

Display command-line help.

# File lib/ctioga2/commands/doc/doc.rb, line 88
def display_command_line_help(options)
  CommandLineHelp.new(options).
    print_commandline_options(*self.documented_commands)
end
display_help_on(cmd, options) click to toggle source

Displays help on a given command

# File lib/ctioga2/commands/doc/doc.rb, line 94
def display_help_on(cmd, options)
  if ! cmd.is_a? Command
    cd = Interpreter::commands[cmd]
    raise "Unkown command '#{cmd}'" unless cd
    cmd = cd
  end
  puts text_doc(cmd)
end
documented_commands() click to toggle source

Returns a [ cmds, groups ] hash containing the list of commands, and the groups to be documented.

# File lib/ctioga2/commands/doc/doc.rb, line 65
def documented_commands
  cmds = group_commands

  groups = cmds.keys.sort do |a,b|
    if ! a
      1
    elsif ! b
      -1
    else
      if a.priority == b.priority
        a.name <=> b.name
      else
        a.priority <=> b.priority
      end
    end
  end
  if @ignore_blacklisted
    groups.delete_if {|g| g && g.blacklisted }
  end
  return [cmds, groups]
end
text_doc(cmd, options = {}) click to toggle source

Returns a string that represents a plain text documentation

# File lib/ctioga2/commands/doc/doc.rb, line 104
def text_doc(cmd, options = {})

  size ||= 80

  str = "Synopsis: "
  str << cmd.name
  for arg in cmd.arguments
    str << " #{arg.type.name}"
  end

  os = ""
  for k,v in cmd.optional_arguments
    os << " /#{k.gsub(/_/,'-')}=#{v.type.name}"
  end
  s2 = WordWrapper.wrap(os, size-4) # 4 for the spaces
  str << "\nOptions: #{s2.join("\n    ")}"
  shrt = MarkedUpText.new(self, cmd.short_description).to_s
  mup = MarkedUpText.new(self, cmd.long_description).to_s
  s2 = WordWrapper.wrap(mup.to_s, size)
  return "#{cmd.name} -- #{shrt}\n#{str}\n#{s2.join("\n")}"
end

Protected Instance Methods

group_commands() click to toggle source

Groups Command by CommandGroup, nil being a proper value, and return the corresponding hash.

# File lib/ctioga2/commands/doc/doc.rb, line 132
def group_commands
  ret_val = {}
  for name, cmd in @commands
    group = cmd.group
    if ret_val.key?(group)
      ret_val[group] << cmd
    else
      ret_val[group] = [cmd]
    end
  end
  
  return ret_val
end