Class: GLI::RDocCommand

Inherits:
Command show all
Defined in:
lib/support/rdoc.rb

Instance Attribute Summary

Attributes inherited from CommandLineToken

#aliases, #description, #long_description, #name

Instance Method Summary collapse

Methods inherited from Command

#action, #arg_name, #arguments_description, #clear_nexts, #default_value, #desc, #flag, #flags, name_as_string, #names, #switch, #switches, #usage

Methods inherited from CommandLineToken

#<=>, #usage

Constructor Details

#initializeRDocCommand

Returns a new instance of RDocCommand.



7
8
9
# File 'lib/support/rdoc.rb', line 7

def initialize
  super(:rdoc,'Generates RDoc for your command line interface')
end

Instance Method Details

#execute(g, o, a) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/support/rdoc.rb', line 11

def execute(g,o,a)
  File.open("#{GLI.program_name}.rdoc",'w') do |file|
    file << "= <tt>#{GLI.program_name}</tt>\n\n"
    file << "    "
    file << GLI.program_name
    file << " "
    global_options = GLI.switches.merge(GLI.flags)
    if (global_options && global_options.length > 0)
      file << "[global options] "
    end
    file << "command_name"
    file << " [command-specific options]"
    file << " [--] arguments...\n\n"
    file << "* Use the command +help+ to get a summary of commands\n"
    file << "* Use the command <tt>help command_name</tt> to get a help for +command_name+\n"
    file << "* Use <tt>--</tt> to stop command line argument processing; useful if your arguments have dashes in them\n"
    file << "\n"
    if (global_options && global_options.length > 0)
      file << "== Global Options\n"
      file << "These options are available for any command and are specified before the name of the command\n\n"
      output_flags(file,global_options)
    end
    file << "== Commands\n"
    GLI.commands.values.sort.each do |command|
      next if command == self
      file << "[<tt>#{command.name}</tt>] #{command.description}\n"
    end
    file << "\n"

    GLI.commands.values.sort.each do |command|
      next if command == self
      file << "=== <tt>#{command.name} #{command.arguments_description}</tt>\n\n"
      file << "#{command.description}\n\n"
      if command.aliases
        file << "*Aliases*\n"
        command.aliases.each do |al|
          file << "* <tt><b>#{al}</b></tt>\n"
        end 
        file << "\n"
      end
      all_options = command.switches.merge(command.flags)
      if (all_options && all_options.length > 0)
        file << "#{command.long_description}\n\n"
        file << "==== Options\n"
        file << "These options are specified *after* the command.\n\n"
        output_flags(file,all_options)
      end
    end
  end
end

#output_flags(file, flags) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/support/rdoc.rb', line 62

def output_flags(file,flags)
  flags.values.sort.each do |flag|
    file << "[<tt>#{flag.usage}</tt>] #{flag.description}"
    if flag.kind_of? Flag
      file << " <i>( default: <tt>#{flag.default_value}</tt>)</i>" if flag.default_value
    end
    file << "\n"
    if flag.long_description
      file << "\n"
      # 12 is 4 for tt, 5 for /tt, 2 for the brackets and 1 for spacing
      (flag.usage.length + 12).times { file << " " }
      file << "#{flag.long_description}\n\n"
    end
  end
end