class RDoc::Options
RDoc::Options
处理选项的解析和存储
已保存的 Options
¶ ↑
您可以将一些选项(如标记格式)保存在 gem 中的 .rdoc_options
文件中。最简单的方法是
rdoc --markup tomdoc --write-options
这将自动创建文件并使用您指定的选项填充它。
以下选项将不会被保存,因为它们会干扰用户的偏好或 RDoc 的正常操作
-
--coverage-report
-
--dry-run
-
--encoding
-
--force-update
-
--format
-
--pipe
-
--quiet
-
--template
-
--verbose
自定义 Options
¶ ↑
生成器可以挂钩到 RDoc::Options
中以添加特定于生成器的命令行选项。
当在 ARGV 中遇到 --format
时,RDoc
会在生成器类上调用 ::setup_options 以将额外的选项添加到选项解析器。自定义生成器的 Options
必须出现在 --format
之后。 rdoc --help
将列出所有已安装生成器的选项。
示例
class RDoc::Generator::Spellcheck RDoc::RDoc.add_generator self def self.setup_options rdoc_options op = rdoc_options.option_parser op.on('--spell-dictionary DICTIONARY', RDoc::Options::Path) do |dictionary| rdoc_options.spell_dictionary = dictionary end end end
当然,默认情况下 RDoc::Options
不会响应 spell_dictionary
,因此您需要添加它
class RDoc::Options ## # The spell dictionary used by the spell-checking plugin. attr_accessor :spell_dictionary end
选项验证器¶ ↑
OptionParser
验证器将验证和转换用户输入值。除了 OptionParser
附带的验证器 (String
, Integer
, Float
, TrueClass
, FalseClass
, Array
, Regexp
, Date
, Time
, URI
等) 之外,RDoc::Options
添加了 Path
, PathArray
和 Template
。
常量
- DEPRECATED
-
已弃用的选项。
- Directory
-
OptionParser
的选项验证器,匹配文件系统中存在的目录。 - Path
-
OptionParser
的选项验证器,匹配文件系统中存在的文件或目录。 - PathArray
-
OptionParser
的选项验证器,匹配文件系统中存在的以逗号分隔的文件或目录列表。 - SPECIAL
-
RDoc
选项被 –write-options 忽略(或特别处理) - Template
-
OptionParser
的选项验证器,匹配安装的生成器的模板目录,该目录位于"rdoc/generator/template/#{template_name}"
属性
如果为 true,则仅报告未记录的文件
如果为 true,RDoc
将不会写入任何文件。
将 mixin 方法、属性和常量嵌入到类文档中。通过 --[no-]embed-mixins
设置 Set
(默认为 false
)。
输出编码。所有输入文件都将转码为此编码。
默认编码为 UTF-8。这是通过 –encoding 设置的。
将排除与此模式匹配的文件
要处理的文件列表
即使输出目录看起来不像 rdoc 输出目录,也创建输出
如果为 true,则扫描比标志文件更新的源文件。
用于标记文本的格式化程序
输出生成器的描述(使用 --format
选项设置)
加载的生成器选项。用于防止 –help 多次加载相同的选项。
旧的 rdoc 行为:超链接与方法名称匹配的所有单词,即使前面没有 ‘#’ 或 ‘::’
在源代码中包含行号
输出区域设置。
区域设置数据所在的目录。
要在初始索引页面中显示的文件、类或模块的名称(如果未指定,则使用我们遇到的第一个文件)
标记格式。以下之一:rdoc
(默认)、markdown
、rd
、tomdoc
。请参阅 标记格式。
输出目录的名称
输出标题装饰?
指南、FAQ 和其他与类无关的页面所在的 Directory
。如果这些位于项目的根目录,则可以不设置此项。
Array
目录,用于搜索满足 :include 的文件
在超链接的实例方法名称的前面包含 ‘#’
指示是否应跳过测试套件的文件
用于复制静态文件的 Directory
制表符中的列数
生成输出时要使用的 Template
模板所在的 Directory
其他模板样式表
文档标题
RDoc
是否应更新输出目录中的时间戳?
详细程度,零表示静默
记录的方法的最小可见性。以下之一::public
、:protected
、:private
或 :nodoc
。
:nodoc
可见性会忽略所有与可见性相关的指令。可以使用 :doc: 指令在每个方法的基础上覆盖其他可见性。
如果无法解析 rdoc-ref 链接,则发出警告。默认为 false
Web cvs 前端的 URL
公共类方法
源代码
# File lib/rdoc/options.rb, line 1339 def self.load_options options_file = File.expand_path '.rdoc_options' return RDoc::Options.new unless File.exist? options_file RDoc.load_yaml begin options = YAML.safe_load File.read('.rdoc_options'), permitted_classes: [RDoc::Options, Symbol] rescue Psych::SyntaxError raise RDoc::Error, "#{options_file} is not a valid rdoc options file" end return RDoc::Options.new unless options # Allow empty file. raise RDoc::Error, "#{options_file} is not a valid rdoc options file" unless RDoc::Options === options or Hash === options if Hash === options # Override the default values with the contents of YAML file. options = RDoc::Options.new options end options end
如果文件存在,则从 .rdoc_options 加载选项,否则创建一个新的 RDoc::Options
实例。
公共实例方法
源代码
# File lib/rdoc/options.rb, line 502 def check_files @files.delete_if do |file| if File.exist? file then if File.readable? file then false else warn "file '#{file}' not readable" true end else warn "file '#{file}' not found" true end end end
检查命令行上的文件是否存在
源代码
# File lib/rdoc/options.rb, line 523 def check_generator if @generator then raise OptionParser::InvalidOption, "generator already set to #{@generator_name}" end end
确保只加载一个生成器
源代码
# File lib/rdoc/options.rb, line 535 def default_title=(string) @title ||= string end
设置标题,但仅在尚未设置的情况下。用于从源文件设置标题,以便从命令行设置的标题具有优先级。
源代码
# File lib/rdoc/options.rb, line 563 def exclude if @exclude.nil? or Regexp === @exclude then # done, #finish is being re-run @exclude elsif @exclude.empty? then nil else Regexp.new(@exclude.join("|")) end end
为 exclude
创建正则表达式
源代码
# File lib/rdoc/options.rb, line 579 def finish if @write_options then write_options exit end @op_dir ||= 'doc' root = @root.to_s if @rdoc_include.empty? || !@rdoc_include.include?(root) @rdoc_include << root end @exclude = self.exclude finish_page_dir check_files # If no template was specified, use the default template for the output # formatter unless @template then @template = @generator_name @template_dir = template_dir_for @template end if @locale_name @locale = RDoc::I18n::Locale[@locale_name] @locale.load(@locale_dir) else @locale = nil end self end
源代码
# File lib/rdoc/options.rb, line 620 def finish_page_dir return unless @page_dir @files << @page_dir page_dir = Pathname(@page_dir) begin page_dir = page_dir.expand_path.relative_path_from @root rescue ArgumentError # On Windows, sometimes crosses different drive letters. page_dir = page_dir.expand_path end @page_dir = page_dir end
源代码
# File lib/rdoc/options.rb, line 639 def generator_descriptions lengths = [] generators = RDoc::RDoc::GENERATORS.map do |name, generator| lengths << name.length description = generator::DESCRIPTION if generator.const_defined? :DESCRIPTION [name, description] end longest = lengths.max generators.sort.map do |name, description| if description then " %-*s - %s" % [longest, name, description] else " #{name}" end end.join "\n" end
返回正确间隔的生成器及其描述列表。
源代码
# File lib/rdoc/options.rb, line 665 def parse argv ignore_invalid = true argv.insert(0, *ENV['RDOCOPT'].split) if ENV['RDOCOPT'] opts = OptionParser.new do |opt| @option_parser = opt opt.program_name = File.basename $0 opt.version = RDoc::VERSION opt.release = nil opt.summary_indent = ' ' * 4 opt.banner = <<-EOF Usage: #{opt.program_name} [options] [names...] Files are parsed, and the information they contain collected, before any output is produced. This allows cross references between all files to be resolved. If a name is a directory, it is traversed. If no names are specified, all Ruby files in the current directory (and subdirectories) are processed. How RDoc generates output depends on the output formatter being used, and on the options you give. Options can be specified via the RDOCOPT environment variable, which functions similar to the RUBYOPT environment variable for ruby. $ export RDOCOPT="--show-hash" will make rdoc show hashes in method links by default. Command-line options always will override those in RDOCOPT. Available formatters: #{generator_descriptions} RDoc understands the following file formats: EOF parsers = Hash.new { |h, parser| h[parser] = [] } RDoc::Parser.parsers.each do |regexp, parser| parsers[parser.name.sub('RDoc::Parser::', '')] << regexp.source end parsers.sort.each do |parser, regexp| opt.banner += " - #{parser}: #{regexp.join ', '}\n" end opt.banner += " - TomDoc: Only in ruby files\n" opt.banner += "\n The following options are deprecated:\n\n" name_length = DEPRECATED.keys.sort_by { |k| k.length }.last.length DEPRECATED.sort_by { |k,| k }.each do |name, reason| opt.banner += " %*1$2$s %3$s\n" % [-name_length, name, reason] end opt.accept Template do |template| template_dir = template_dir_for template unless template_dir then $stderr.puts "could not find template #{template}" nil else [template, template_dir] end end opt.accept Directory do |directory| directory = File.expand_path directory raise OptionParser::InvalidArgument unless File.directory? directory directory end opt.accept Path do |path| path = File.expand_path path raise OptionParser::InvalidArgument unless File.exist? path path end opt.accept PathArray do |paths,| paths = if paths then paths.split(',').map { |d| d unless d.empty? } end paths.map do |path| path = File.expand_path path raise OptionParser::InvalidArgument unless File.exist? path path end end opt.separator nil opt.separator "Parsing options:" opt.separator nil opt.on("--encoding=ENCODING", "-e", Encoding.list.map { |e| e.name }, "Specifies the output encoding. All files", "read will be converted to this encoding.", "The default encoding is UTF-8.", "--encoding is preferred over --charset") do |value| @encoding = Encoding.find value @charset = @encoding.name # may not be valid value end opt.separator nil opt.on("--locale=NAME", "Specifies the output locale.") do |value| @locale_name = value end opt.on("--locale-data-dir=DIR", "Specifies the directory where locale data live.") do |value| @locale_dir = value end opt.separator nil opt.on("--all", "-a", "Synonym for --visibility=private.") do |value| @visibility = :private end opt.separator nil opt.on("--exclude=PATTERN", "-x", Regexp, "Do not process files or directories", "matching PATTERN.") do |value| @exclude << value end opt.separator nil opt.on("--no-skipping-tests", nil, "Don't skip generating documentation for test and spec files") do |value| @skip_tests = false end opt.separator nil opt.on("--extension=NEW=OLD", "-E", "Treat files ending with .new as if they", "ended with .old. Using '-E cgi=rb' will", "cause xxx.cgi to be parsed as a Ruby file.") do |value| new, old = value.split(/=/, 2) unless new and old then raise OptionParser::InvalidArgument, "Invalid parameter to '-E'" end unless RDoc::Parser.alias_extension old, new then raise OptionParser::InvalidArgument, "Unknown extension .#{old} to -E" end end opt.separator nil opt.on("--[no-]force-update", "-U", "Forces rdoc to scan all sources even if", "no files are newer than the flag file.") do |value| @force_update = value end opt.separator nil opt.on("--pipe", "-p", "Convert RDoc on stdin to HTML") do @pipe = true end opt.separator nil opt.on("--tab-width=WIDTH", "-w", Integer, "Set the width of tab characters.") do |value| raise OptionParser::InvalidArgument, "#{value} is an invalid tab width" if value <= 0 @tab_width = value end opt.separator nil opt.on("--visibility=VISIBILITY", "-V", RDoc::VISIBILITIES + [:nodoc], "Minimum visibility to document a method.", "One of 'public', 'protected' (the default),", "'private' or 'nodoc' (show everything)") do |value| @visibility = value end opt.separator nil opt.on("--[no-]embed-mixins", "Embed mixin methods, attributes, and constants", "into class documentation. (default false)") do |value| @embed_mixins = value end opt.separator nil markup_formats = RDoc::Text::MARKUP_FORMAT.keys.sort opt.on("--markup=MARKUP", markup_formats, "The markup format for the named files.", "The default is rdoc. Valid values are:", markup_formats.join(', ')) do |value| @markup = value end opt.separator nil opt.on("--root=ROOT", Directory, "Root of the source tree documentation", "will be generated for. Set this when", "building documentation outside the", "source directory. Default is the", "current directory.") do |root| @root = Pathname(root) end opt.separator nil opt.on("--page-dir=DIR", Directory, "Directory where guides, your FAQ or", "other pages not associated with a class", "live. Set this when you don't store", "such files at your project root.", "NOTE: Do not use the same file name in", "the page dir and the root of your project") do |page_dir| @page_dir = page_dir end opt.separator nil opt.separator "Common generator options:" opt.separator nil opt.on("--force-output", "-O", "Forces rdoc to write the output files,", "even if the output directory exists", "and does not seem to have been created", "by rdoc.") do |value| @force_output = value end opt.separator nil generator_text = @generators.keys.map { |name| " #{name}" }.sort opt.on("-f", "--fmt=FORMAT", "--format=FORMAT", @generators.keys, "Set the output formatter. One of:", *generator_text) do |value| check_generator @generator_name = value.downcase setup_generator end opt.separator nil opt.on("--include=DIRECTORIES", "-i", PathArray, "Set (or add to) the list of directories to", "be searched when satisfying :include:", "requests. Can be used more than once.") do |value| @rdoc_include.concat value.map { |dir| dir.strip } end opt.separator nil opt.on("--[no-]coverage-report=[LEVEL]", "--[no-]dcov", "-C", Integer, "Prints a report on undocumented items.", "Does not generate files.") do |value| value = 0 if value.nil? # Integer converts -C to nil @coverage_report = value @force_update = true if value end opt.separator nil opt.on("--output=DIR", "--op", "-o", "Set the output directory.") do |value| @op_dir = value end opt.separator nil opt.on("-d", "Deprecated --diagram option.", "Prevents firing debug mode", "with legacy invocation.") do |value| end opt.separator nil opt.separator 'HTML generator options:' opt.separator nil opt.on("--charset=CHARSET", "-c", "Specifies the output HTML character-set.", "Use --encoding instead of --charset if", "available.") do |value| @charset = value end opt.separator nil opt.on("--hyperlink-all", "-A", "Generate hyperlinks for all words that", "correspond to known methods, even if they", "do not start with '#' or '::' (legacy", "behavior).") do |value| @hyperlink_all = value end opt.separator nil opt.on("--main=NAME", "-m", "NAME will be the initial page displayed.") do |value| @main_page = value end opt.separator nil opt.on("--[no-]line-numbers", "-N", "Include line numbers in the source code.", "By default, only the number of the first", "line is displayed, in a leading comment.") do |value| @line_numbers = value end opt.separator nil opt.on("--show-hash", "-H", "A name of the form #name in a comment is a", "possible hyperlink to an instance method", "name. When displayed, the '#' is removed", "unless this option is specified.") do |value| @show_hash = value end opt.separator nil opt.on("--template=NAME", "-T", Template, "Set the template used when generating", "output. The default depends on the", "formatter used.") do |(template, template_dir)| @template = template @template_dir = template_dir end opt.separator nil opt.on("--template-stylesheets=FILES", PathArray, "Set (or add to) the list of files to", "include with the html template.") do |value| @template_stylesheets.concat value end opt.separator nil opt.on("--title=TITLE", "-t", "Set TITLE as the title for HTML output.") do |value| @title = value end opt.separator nil opt.on("--copy-files=PATH", Path, "Specify a file or directory to copy static", "files from.", "If a file is given it will be copied into", "the output dir. If a directory is given the", "entire directory will be copied.", "You can use this multiple times") do |value| @static_path << value end opt.separator nil opt.on("--webcvs=URL", "-W", "Specify a URL for linking to a web frontend", "to CVS. If the URL contains a '\%s', the", "name of the current file will be", "substituted; if the URL doesn't contain a", "'\%s', the filename will be appended to it.") do |value| @webcvs = value end opt.separator nil opt.separator "ri generator options:" opt.separator nil opt.on("--ri", "-r", "Generate output for use by `ri`. The files", "are stored in the '.rdoc' directory under", "your home directory unless overridden by a", "subsequent --op parameter, so no special", "privileges are needed.") do |value| check_generator @generator_name = "ri" @op_dir ||= RDoc::RI::Paths::HOMEDIR setup_generator end opt.separator nil opt.on("--ri-site", "-R", "Generate output for use by `ri`. The files", "are stored in a site-wide directory,", "making them accessible to others, so", "special privileges are needed.") do |value| check_generator @generator_name = "ri" @op_dir = RDoc::RI::Paths.site_dir setup_generator end opt.separator nil opt.separator "Generic options:" opt.separator nil opt.on("--write-options", "Write .rdoc_options to the current", "directory with the given options. Not all", "options will be used. See RDoc::Options", "for details.") do |value| @write_options = true end opt.separator nil opt.on("--[no-]dry-run", "Don't write any files") do |value| @dry_run = value end opt.separator nil opt.on("-D", "--[no-]debug", "Displays lots on internal stuff.") do |value| $DEBUG_RDOC = value end opt.separator nil opt.on("--warn-missing-rdoc-ref", "Warn if rdoc-ref links can't be resolved") do |value| @warn_missing_rdoc_ref = value end opt.separator nil opt.on("--[no-]ignore-invalid", "Ignore invalid options and continue", "(default true).") do |value| ignore_invalid = value end opt.separator nil opt.on("--quiet", "-q", "Don't show progress as we parse.") do |value| @verbosity = 0 end opt.separator nil opt.on("--verbose", "-V", "Display extra progress as RDoc parses") do |value| @verbosity = 2 end opt.separator nil opt.on("--version", "-v", "print the version") do puts opt.version exit end opt.separator nil opt.on("--help", "-h", "Display this help") do RDoc::RDoc::GENERATORS.each_key do |generator| setup_generator generator end puts opt.help exit end opt.separator nil end setup_generator 'darkfish' if argv.grep(/\A(-f|--fmt|--format|-r|-R|--ri|--ri-site)\b/).empty? deprecated = [] invalid = [] begin opts.parse! argv rescue OptionParser::ParseError => e if DEPRECATED[e.args.first] then deprecated << e.args.first elsif %w[--format --ri -r --ri-site -R].include? e.args.first then raise else invalid << e.args.join(' ') end retry end unless @generator then @generator = RDoc::Generator::Darkfish @generator_name = 'darkfish' end if @pipe and not argv.empty? then @pipe = false invalid << '-p (with files)' end unless quiet then deprecated.each do |opt| $stderr.puts 'option ' + opt + ' is deprecated: ' + DEPRECATED[opt] end end unless invalid.empty? then invalid = "invalid options: #{invalid.join ', '}" if ignore_invalid then unless quiet then $stderr.puts invalid $stderr.puts '(invalid options are ignored)' end else unless quiet then $stderr.puts opts end $stderr.puts invalid exit 1 end end @files = argv.dup self end
解析命令行选项。
源代码
# File lib/rdoc/options.rb, line 1232 def quiet= bool @verbosity = bool ? 0 : 1 end
将静默设置为 bool
源代码
# File lib/rdoc/options.rb, line 1239 def sanitize_path path require 'pathname' dot = Pathname.new('.').expand_path path.reject do |item| path = Pathname.new(item).expand_path is_reject = nil relative = nil begin relative = path.relative_path_from(dot).to_s rescue ArgumentError # On Windows, sometimes crosses different drive letters. is_reject = true else is_reject = relative.start_with? '..' end is_reject end end
从 path
中删除当前目录之外的目录
源代码
# File lib/rdoc/options.rb, line 1266 def setup_generator generator_name = @generator_name @generator = @generators[generator_name] unless @generator then raise OptionParser::InvalidArgument, "Invalid output formatter #{generator_name}" end return if @generator_options.include? @generator @generator_name = generator_name @generator_options << @generator if @generator.respond_to? :setup_options then @option_parser ||= OptionParser.new @generator.setup_options self end end
为名为 generator_name
的生成器设置输出生成器。
如果找到的生成器响应 :setup_options,则将使用 options 实例调用它。这允许生成器添加自定义选项或设置默认选项。
源代码
# File lib/rdoc/options.rb, line 1288 def template_dir_for template template_path = File.join 'rdoc', 'generator', 'template', template $LOAD_PATH.map do |path| File.join File.expand_path(path), template_path end.find do |dir| File.directory? dir end end
查找 template
的模板目录
源代码
# File lib/rdoc/options.rb, line 1305 def visibility= visibility case visibility when :all @visibility = :private else @visibility = visibility end end
设置记录的方法的最小可见性。
接受 :public
、:protected
、:private
、:nodoc
或 :all
。
当传递 :all
时,可见性将设置为 :private
,类似于 RDOCOPT=“–all”,有关更多信息,请参阅 visibility
。
源代码
# File lib/rdoc/options.rb, line 1317 def warn message super message if @verbosity > 1 end
如果启用了详细模式,则使用 Kernel#warn
显示警告。
Kernel#warn
。源代码
# File lib/rdoc/options.rb, line 1325 def write_options RDoc.load_yaml File.open '.rdoc_options', 'w' do |io| io.set_encoding Encoding::UTF_8 io.print to_yaml end end
将包含已解析选项的 YAML
文件 .rdoc_options 写入当前目录。