Class: CTioga2::Commands::Documentation::Doc

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/commands/doc/doc.rb

Overview

The base class for all documentation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDoc

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



52
53
54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#backendsObject

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



42
43
44
# File 'lib/ctioga2/commands/doc/doc.rb', line 42

def backends
  @backends
end

#commandsObject

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



30
31
32
# File 'lib/ctioga2/commands/doc/doc.rb', line 30

def commands
  @commands
end

#functionsObject

The functions



48
49
50
# File 'lib/ctioga2/commands/doc/doc.rb', line 48

def functions
  @functions
end

#groupsObject

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



34
35
36
# File 'lib/ctioga2/commands/doc/doc.rb', line 34

def groups
  @groups
end

#ignore_blacklistedObject

Wether or not to ignore blacklisted commands



45
46
47
# File 'lib/ctioga2/commands/doc/doc.rb', line 45

def ignore_blacklisted
  @ignore_blacklisted
end

#typesObject

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



38
39
40
# File 'lib/ctioga2/commands/doc/doc.rb', line 38

def types
  @types
end

Instance Method Details

#display_command_line_help(options) ⇒ Object

Display command-line help.



88
89
90
91
# 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) ⇒ Object

Displays help on a given command



94
95
96
97
98
99
100
101
# 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_commandsObject

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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 = {}) ⇒ Object

Returns a string that represents a plain text documentation



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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