~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/commands/doc/doc.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# doc.rb: a class holding all informations
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/commands/commands'
 
16
require 'ctioga2/commands/doc/markup'
 
17
 
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision: 138 $', '$Date: 2010-01-21 22:39:55 +0100 (Thu, 21 Jan 2010) $')
 
21
 
 
22
  module Commands
 
23
 
 
24
    # The base of the 'self-documentation' of CTioga2
 
25
    module Documentation
 
26
 
 
27
      # The base class for all documentation.
 
28
      class Doc
 
29
        
 
30
        # The hash containing all the commands, as returned
 
31
        # by Interpreter::commands.
 
32
        attr_accessor :commands
 
33
 
 
34
        # The hash containing all the groups, as returned
 
35
        # by Interpreter::commands.
 
36
        attr_accessor :groups
 
37
 
 
38
        # The hash containing all the types, as returned
 
39
        # by Interpreter::commands.
 
40
        attr_accessor :types
 
41
 
 
42
        # The hash containing all the backends, as returned by
 
43
        # Data::Backends::Backend::list_backends
 
44
        attr_accessor :backends
 
45
 
 
46
        # Wether or not to ignore blacklisted commands
 
47
        attr_accessor :ignore_blacklisted
 
48
 
 
49
        # Create a Doc object caring about the current state of
 
50
        # registered commands and such.
 
51
        def initialize
 
52
          @commands = Interpreter::commands
 
53
          @groups = Interpreter::groups
 
54
          @types = Interpreter::types
 
55
          @backends = Data::Backends::Backend::list_backends
 
56
 
 
57
          @ignore_blacklisted = ! (ENV.key?("CT2_DEV") && 
 
58
                                   ! ENV["CT2_DEV"].empty?)
 
59
        end
 
60
 
 
61
        # Returns a [ cmds, groups ] hash containing the list of
 
62
        # commands, and the groups to be documented.
 
63
        def documented_commands
 
64
          cmds = group_commands
 
65
 
 
66
          groups = cmds.keys.sort do |a,b|
 
67
            if ! a
 
68
              1
 
69
            elsif ! b
 
70
              -1
 
71
            else
 
72
              if a.priority == b.priority
 
73
                a.name <=> b.name
 
74
              else
 
75
                a.priority <=> b.priority
 
76
              end
 
77
            end
 
78
          end
 
79
          if @ignore_blacklisted
 
80
            groups.delete_if {|g| g && g.blacklisted }
 
81
          end
 
82
          return [cmds, groups]
 
83
        end
 
84
 
 
85
        # Display command-line help.
 
86
        def display_command_line_help(options)
 
87
          CommandLineHelp.new(options).
 
88
            print_commandline_options(*self.documented_commands)
 
89
        end
 
90
 
 
91
        protected 
 
92
 
 
93
 
 
94
        # Groups Command by CommandGroup, _nil_ being a proper value,
 
95
        # and return the corresponding hash.
 
96
        def group_commands
 
97
          ret_val = {}
 
98
          for name, cmd in @commands
 
99
            group = cmd.group
 
100
            if ret_val.key?(group)
 
101
              ret_val[group] << cmd
 
102
            else
 
103
              ret_val[group] = [cmd]
 
104
            end
 
105
          end
 
106
          
 
107
          return ret_val
 
108
        end
 
109
 
 
110
      end
 
111
    end
 
112
  end
 
113
end
 
114