require 'gli' require 'fileutils' module GLI class RDocCommand < Command def initialize super(:rdoc,'Generates RDoc for your command line interface') end def execute(g,o,a) File.open("#{GLI.program_name}.rdoc",'w') do |file| file << "= #{GLI.program_name}\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 help command_name to get a help for +command_name+\n" file << "* Use -- 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 << "[#{command.name}] #{command.description}\n" end file << "\n" GLI.commands.values.sort.each do |command| next if command == self file << "=== #{command.name} #{command.arguments_description}\n\n" file << "#{command.description}\n\n" if command.aliases file << "*Aliases*\n" command.aliases.each do |al| file << "* #{al}\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 def output_flags(file,flags) flags.values.sort.each do |flag| file << "[#{flag.usage}] #{flag.description}" if flag.kind_of? Flag file << " ( default: #{flag.default_value})" 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 end end