/tools/Ruby/lib/ruby/1.8/rdoc/options.rb

http://github.com/agross/netopenspace · Ruby · 586 lines · 417 code · 119 blank · 50 comment · 26 complexity · 2e9dddf8102229cb55c32890c0d68431 MD5 · raw file

  1. # We handle the parsing of options, and subsequently as a singleton
  2. # object to be queried for option values
  3. require "rdoc/ri/ri_paths"
  4. class Options
  5. require 'singleton'
  6. require 'getoptlong'
  7. include Singleton
  8. # files matching this pattern will be excluded
  9. attr_accessor :exclude
  10. # the name of the output directory
  11. attr_accessor :op_dir
  12. # the name to use for the output
  13. attr_reader :op_name
  14. # include private and protected methods in the
  15. # output
  16. attr_accessor :show_all
  17. # name of the file, class or module to display in
  18. # the initial index page (if not specified
  19. # the first file we encounter is used)
  20. attr_accessor :main_page
  21. # merge into classes of the name name when generating ri
  22. attr_reader :merge
  23. # Don't display progress as we process the files
  24. attr_reader :quiet
  25. # description of the output generator (set with the <tt>-fmt</tt>
  26. # option
  27. attr_accessor :generator
  28. # and the list of files to be processed
  29. attr_reader :files
  30. # array of directories to search for files to satisfy an :include:
  31. attr_reader :rdoc_include
  32. # title to be used out the output
  33. #attr_writer :title
  34. # template to be used when generating output
  35. attr_reader :template
  36. # should diagrams be drawn
  37. attr_reader :diagram
  38. # should we draw fileboxes in diagrams
  39. attr_reader :fileboxes
  40. # include the '#' at the front of hyperlinked instance method names
  41. attr_reader :show_hash
  42. # image format for diagrams
  43. attr_reader :image_format
  44. # character-set
  45. attr_reader :charset
  46. # should source code be included inline, or displayed in a popup
  47. attr_reader :inline_source
  48. # should the output be placed into a single file
  49. attr_reader :all_one_file
  50. # the number of columns in a tab
  51. attr_reader :tab_width
  52. # include line numbers in the source listings
  53. attr_reader :include_line_numbers
  54. # pattern for additional attr_... style methods
  55. attr_reader :extra_accessors
  56. attr_reader :extra_accessor_flags
  57. # URL of stylesheet
  58. attr_reader :css
  59. # URL of web cvs frontend
  60. attr_reader :webcvs
  61. # Are we promiscuous about showing module contents across
  62. # multiple files
  63. attr_reader :promiscuous
  64. # scan newer sources than the flag file if true.
  65. attr_reader :force_update
  66. module OptionList
  67. OPTION_LIST = [
  68. [ "--accessor", "-A", "accessorname[,..]",
  69. "comma separated list of additional class methods\n" +
  70. "that should be treated like 'attr_reader' and\n" +
  71. "friends. Option may be repeated. Each accessorname\n" +
  72. "may have '=text' appended, in which case that text\n" +
  73. "appears where the r/w/rw appears for normal accessors."],
  74. [ "--all", "-a", nil,
  75. "include all methods (not just public)\nin the output" ],
  76. [ "--charset", "-c", "charset",
  77. "specifies HTML character-set" ],
  78. [ "--debug", "-D", nil,
  79. "displays lots on internal stuff" ],
  80. [ "--diagram", "-d", nil,
  81. "Generate diagrams showing modules and classes.\n" +
  82. "You need dot V1.8.6 or later to use the --diagram\n" +
  83. "option correctly. Dot is available from\n"+
  84. "http://www.research.att.com/sw/tools/graphviz/" ],
  85. [ "--exclude", "-x", "pattern",
  86. "do not process files or directories matching\n" +
  87. "pattern. Files given explicitly on the command\n" +
  88. "line will never be excluded." ],
  89. [ "--extension", "-E", "new=old",
  90. "Treat files ending with .new as if they ended with\n" +
  91. ".old. Using '-E cgi=rb' will cause xxx.cgi to be\n" +
  92. "parsed as a Ruby file"],
  93. [ "--fileboxes", "-F", nil,
  94. "classes are put in boxes which represents\n" +
  95. "files, where these classes reside. Classes\n" +
  96. "shared between more than one file are\n" +
  97. "shown with list of files that sharing them.\n" +
  98. "Silently discarded if --diagram is not given\n" +
  99. "Experimental." ],
  100. [ "--force-update", "-U", nil,
  101. "forces to scan all sources even if newer than\n" +
  102. "the flag file." ],
  103. [ "--fmt", "-f", "format name",
  104. "set the output formatter (see below)" ],
  105. [ "--help", "-h", nil,
  106. "you're looking at it" ],
  107. [ "--help-output", "-O", nil,
  108. "explain the various output options" ],
  109. [ "--image-format", "-I", "gif/png/jpg/jpeg",
  110. "Sets output image format for diagrams. Can\n" +
  111. "be png, gif, jpeg, jpg. If this option is\n" +
  112. "omitted, png is used. Requires --diagram." ],
  113. [ "--include", "-i", "dir[,dir...]",
  114. "set (or add to) the list of directories\n" +
  115. "to be searched when satisfying :include:\n" +
  116. "requests. Can be used more than once." ],
  117. [ "--inline-source", "-S", nil,
  118. "Show method source code inline, rather\n" +
  119. "than via a popup link" ],
  120. [ "--line-numbers", "-N", nil,
  121. "Include line numbers in the source code" ],
  122. [ "--main", "-m", "name",
  123. "'name' will be the initial page displayed" ],
  124. [ "--merge", "-M", nil,
  125. "when creating ri output, merge processed classes\n" +
  126. "into previously documented classes of the name name"],
  127. [ "--one-file", "-1", nil,
  128. "put all the output into a single file" ],
  129. [ "--op", "-o", "dir",
  130. "set the output directory" ],
  131. [ "--opname", "-n", "name",
  132. "Set the 'name' of the output. Has no\n" +
  133. "effect for HTML." ],
  134. [ "--promiscuous", "-p", nil,
  135. "When documenting a file that contains a module\n" +
  136. "or class also defined in other files, show\n" +
  137. "all stuff for that module/class in each files\n" +
  138. "page. By default, only show stuff defined in\n" +
  139. "that particular file." ],
  140. [ "--quiet", "-q", nil,
  141. "don't show progress as we parse" ],
  142. [ "--ri", "-r", nil,
  143. "generate output for use by 'ri.' The files are\n" +
  144. "stored in the '.rdoc' directory under your home\n"+
  145. "directory unless overridden by a subsequent\n" +
  146. "--op parameter, so no special privileges are needed." ],
  147. [ "--ri-site", "-R", nil,
  148. "generate output for use by 'ri.' The files are\n" +
  149. "stored in a site-wide directory, making them accessible\n"+
  150. "to others, so special privileges are needed." ],
  151. [ "--ri-system", "-Y", nil,
  152. "generate output for use by 'ri.' The files are\n" +
  153. "stored in a system-level directory, making them accessible\n"+
  154. "to others, so special privileges are needed. This option\n"+
  155. "is intended to be used during Ruby installations" ],
  156. [ "--show-hash", "-H", nil,
  157. "A name of the form #name in a comment\n" +
  158. "is a possible hyperlink to an instance\n" +
  159. "method name. When displayed, the '#' is\n" +
  160. "removed unless this option is specified" ],
  161. [ "--style", "-s", "stylesheet url",
  162. "specifies the URL of a separate stylesheet." ],
  163. [ "--tab-width", "-w", "n",
  164. "Set the width of tab characters (default 8)"],
  165. [ "--template", "-T", "template name",
  166. "Set the template used when generating output" ],
  167. [ "--title", "-t", "text",
  168. "Set 'txt' as the title for the output" ],
  169. [ "--version", "-v", nil,
  170. "display RDoc's version" ],
  171. [ "--webcvs", "-W", "url",
  172. "Specify a URL for linking to a web frontend\n" +
  173. "to CVS. If the URL contains a '\%s', the\n" +
  174. "name of the current file will be substituted;\n" +
  175. "if the URL doesn't contain a '\%s', the\n" +
  176. "filename will be appended to it." ],
  177. ]
  178. def OptionList.options
  179. OPTION_LIST.map do |long, short, arg,|
  180. [ long,
  181. short,
  182. arg ? GetoptLong::REQUIRED_ARGUMENT : GetoptLong::NO_ARGUMENT
  183. ]
  184. end
  185. end
  186. def OptionList.strip_output(text)
  187. text =~ /^\s+/
  188. leading_spaces = $&
  189. text.gsub!(/^#{leading_spaces}/, '')
  190. $stdout.puts text
  191. end
  192. # Show an error and exit
  193. def OptionList.error(msg)
  194. $stderr.puts
  195. $stderr.puts msg
  196. $stderr.puts "\nFor help on options, try 'rdoc --help'\n\n"
  197. exit 1
  198. end
  199. # Show usage and exit
  200. def OptionList.usage(generator_names)
  201. puts
  202. puts(VERSION_STRING)
  203. puts
  204. name = File.basename($0)
  205. OptionList.strip_output(<<-EOT)
  206. Usage:
  207. #{name} [options] [names...]
  208. Files are parsed, and the information they contain
  209. collected, before any output is produced. This allows cross
  210. references between all files to be resolved. If a name is a
  211. directory, it is traversed. If no names are specified, all
  212. Ruby files in the current directory (and subdirectories) are
  213. processed.
  214. Options:
  215. EOT
  216. OPTION_LIST.each do |long, short, arg, desc|
  217. opt = sprintf("%20s", "#{long}, #{short}")
  218. oparg = sprintf("%-7s", arg)
  219. print "#{opt} #{oparg}"
  220. desc = desc.split("\n")
  221. if arg.nil? || arg.length < 7
  222. puts desc.shift
  223. else
  224. puts
  225. end
  226. desc.each do |line|
  227. puts(" "*28 + line)
  228. end
  229. puts
  230. end
  231. puts "\nAvailable output formatters: " +
  232. generator_names.sort.join(', ') + "\n\n"
  233. puts "For information on where the output goes, use\n\n"
  234. puts " rdoc --help-output\n\n"
  235. exit 0
  236. end
  237. def OptionList.help_output
  238. OptionList.strip_output(<<-EOT)
  239. How RDoc generates output depends on the output formatter being
  240. used, and on the options you give.
  241. - HTML output is normally produced into a number of separate files
  242. (one per class, module, and file, along with various indices).
  243. These files will appear in the directory given by the --op
  244. option (doc/ by default).
  245. - XML output by default is written to standard output. If a
  246. --opname option is given, the output will instead be written
  247. to a file with that name in the output directory.
  248. - .chm files (Windows help files) are written in the --op directory.
  249. If an --opname parameter is present, that name is used, otherwise
  250. the file will be called rdoc.chm.
  251. For information on other RDoc options, use "rdoc --help".
  252. EOT
  253. exit 0
  254. end
  255. end
  256. # Parse command line options. We're passed a hash containing
  257. # output generators, keyed by the generator name
  258. def parse(argv, generators)
  259. old_argv = ARGV.dup
  260. begin
  261. ARGV.replace(argv)
  262. @op_dir = "doc"
  263. @op_name = nil
  264. @show_all = false
  265. @main_page = nil
  266. @marge = false
  267. @exclude = []
  268. @quiet = false
  269. @generator_name = 'html'
  270. @generator = generators[@generator_name]
  271. @rdoc_include = []
  272. @title = nil
  273. @template = nil
  274. @diagram = false
  275. @fileboxes = false
  276. @show_hash = false
  277. @image_format = 'png'
  278. @inline_source = false
  279. @all_one_file = false
  280. @tab_width = 8
  281. @include_line_numbers = false
  282. @extra_accessor_flags = {}
  283. @promiscuous = false
  284. @force_update = false
  285. @css = nil
  286. @webcvs = nil
  287. @charset = case $KCODE
  288. when /^S/i
  289. 'Shift_JIS'
  290. when /^E/i
  291. 'EUC-JP'
  292. else
  293. 'iso-8859-1'
  294. end
  295. accessors = []
  296. go = GetoptLong.new(*OptionList.options)
  297. go.quiet = true
  298. go.each do |opt, arg|
  299. case opt
  300. when "--all" then @show_all = true
  301. when "--charset" then @charset = arg
  302. when "--debug" then $DEBUG = true
  303. when "--exclude" then @exclude << Regexp.new(arg)
  304. when "--inline-source" then @inline_source = true
  305. when "--line-numbers" then @include_line_numbers = true
  306. when "--main" then @main_page = arg
  307. when "--merge" then @merge = true
  308. when "--one-file" then @all_one_file = @inline_source = true
  309. when "--op" then @op_dir = arg
  310. when "--opname" then @op_name = arg
  311. when "--promiscuous" then @promiscuous = true
  312. when "--quiet" then @quiet = true
  313. when "--show-hash" then @show_hash = true
  314. when "--style" then @css = arg
  315. when "--template" then @template = arg
  316. when "--title" then @title = arg
  317. when "--webcvs" then @webcvs = arg
  318. when "--accessor"
  319. arg.split(/,/).each do |accessor|
  320. if accessor =~ /^(\w+)(=(.*))?$/
  321. accessors << $1
  322. @extra_accessor_flags[$1] = $3
  323. end
  324. end
  325. when "--diagram"
  326. check_diagram
  327. @diagram = true
  328. when "--fileboxes"
  329. @fileboxes = true if @diagram
  330. when "--fmt"
  331. @generator_name = arg.downcase
  332. setup_generator(generators)
  333. when "--help"
  334. OptionList.usage(generators.keys)
  335. when "--help-output"
  336. OptionList.help_output
  337. when "--image-format"
  338. if ['gif', 'png', 'jpeg', 'jpg'].include?(arg)
  339. @image_format = arg
  340. else
  341. raise GetoptLong::InvalidOption.new("unknown image format: #{arg}")
  342. end
  343. when "--include"
  344. @rdoc_include.concat arg.split(/\s*,\s*/)
  345. when "--ri", "--ri-site", "--ri-system"
  346. @generator_name = "ri"
  347. @op_dir = case opt
  348. when "--ri" then RI::Paths::HOMEDIR
  349. when "--ri-site" then RI::Paths::SITEDIR
  350. when "--ri-system" then RI::Paths::SYSDIR
  351. else fail opt
  352. end
  353. setup_generator(generators)
  354. when "--tab-width"
  355. begin
  356. @tab_width = Integer(arg)
  357. rescue
  358. $stderr.puts "Invalid tab width: '#{arg}'"
  359. exit 1
  360. end
  361. when "--extension"
  362. new, old = arg.split(/=/, 2)
  363. OptionList.error("Invalid parameter to '-E'") unless new && old
  364. unless RDoc::ParserFactory.alias_extension(old, new)
  365. OptionList.error("Unknown extension .#{old} to -E")
  366. end
  367. when "--force-update"
  368. @force_update = true
  369. when "--version"
  370. puts VERSION_STRING
  371. exit
  372. end
  373. end
  374. @files = ARGV.dup
  375. @rdoc_include << "." if @rdoc_include.empty?
  376. if @exclude.empty?
  377. @exclude = nil
  378. else
  379. @exclude = Regexp.new(@exclude.join("|"))
  380. end
  381. check_files
  382. # If no template was specified, use the default
  383. # template for the output formatter
  384. @template ||= @generator_name
  385. # Generate a regexp from the accessors
  386. unless accessors.empty?
  387. re = '^(' + accessors.map{|a| Regexp.quote(a)}.join('|') + ')$'
  388. @extra_accessors = Regexp.new(re)
  389. end
  390. rescue GetoptLong::InvalidOption, GetoptLong::MissingArgument => error
  391. OptionList.error(error.message)
  392. ensure
  393. ARGV.replace(old_argv)
  394. end
  395. end
  396. def title
  397. @title ||= "RDoc Documentation"
  398. end
  399. # Set the title, but only if not already set. This means that a title set from
  400. # the command line trumps one set in a source file
  401. def title=(string)
  402. @title ||= string
  403. end
  404. private
  405. # Set up an output generator for the format in @generator_name
  406. def setup_generator(generators)
  407. @generator = generators[@generator_name]
  408. if !@generator
  409. OptionList.error("Invalid output formatter")
  410. end
  411. if @generator_name == "xml"
  412. @all_one_file = true
  413. @inline_source = true
  414. end
  415. end
  416. # Check that the right version of 'dot' is available.
  417. # Unfortuately this doesn't work correctly under Windows NT,
  418. # so we'll bypass the test under Windows
  419. def check_diagram
  420. return if RUBY_PLATFORM =~ /mswin|cygwin|mingw|bccwin/
  421. ok = false
  422. ver = nil
  423. IO.popen("dot -V 2>&1") do |io|
  424. ver = io.read
  425. if ver =~ /dot.+version(?:\s+gviz)?\s+(\d+)\.(\d+)/
  426. ok = ($1.to_i > 1) || ($1.to_i == 1 && $2.to_i >= 8)
  427. end
  428. end
  429. unless ok
  430. if ver =~ /^dot.+version/
  431. $stderr.puts "Warning: You may need dot V1.8.6 or later to use\n",
  432. "the --diagram option correctly. You have:\n\n ",
  433. ver,
  434. "\nDiagrams might have strange background colors.\n\n"
  435. else
  436. $stderr.puts "You need the 'dot' program to produce diagrams.",
  437. "(see http://www.research.att.com/sw/tools/graphviz/)\n\n"
  438. exit
  439. end
  440. # exit
  441. end
  442. end
  443. # Check that the files on the command line exist
  444. def check_files
  445. @files.each do |f|
  446. stat = File.stat f rescue error("File not found: #{f}")
  447. error("File '#{f}' not readable") unless stat.readable?
  448. end
  449. end
  450. def error(str)
  451. $stderr.puts str
  452. exit(1)
  453. end
  454. end