2

我喜欢通过 help2man 和 txt2man 使用“--help”输出生成手册页。Ruby 的 RDoc 系统非常方便,但我似乎无法完全按照我想要的方式配置 RDoc::usage。这是一个示例脚本:

#!/usr/bin/env ruby
#
# === Name
#
#   foobar - Example command for StackOverflow question
#
# === Synopsis
# 
#   Usage: foobar [--help]
#
# === Options
#
#   --help      This help message

require 'rdoc/usage'

RDoc::usage('name', 'synopsis', 'options')

脚本的输出如下所示:

Name
    foobar - Example command for StackOverflow question

Synopsis
    Usage: foobar [--help]

Options
    --help      This help message

但我真的很想为我的使用输出抑制“名称”和“概要”标题,但仍将它们标记为输出到手册页的部分。

使用 ':section:' 标记适用于 RDoc::Rdoc,但不适用于 RDoc::usage。是否有一种明显的方法可以在不打印标题的情况下标记 usage.rb 部分?

4

1 回答 1

1

查看源代码RDoc::usage_no_exit;你有两种方法可以抓住它的胆量来实现你的愿望:

  1. 设置ENV['RI']为强制执行不同的格式化选项(包括指定自定义格式化程序类),或
  2. 重新定义默认 RI::TextFormatter 的display_heading(和/或其他方法)以(不)显示标题或其他任何内容

    require 'rdoc/usage'
    require 'rdoc/ri/ri_formatter'
    
    module RI
      class TextFormatter
        def display_heading
          # nop
        end
      end
    end
    
    RDoc::usage('name')
    
于 2009-03-12T19:50:13.870 回答