将选项传递给 man 以获取自写的帮助文件

将选项传递给 man 以获取自写的帮助文件

我对手册页的语法有疑问。

我刚刚为我的小项目编写了联机帮助页格式的文档。我想允许用户 man commandname option 按照 git 文档中的方式输入,例如 ,man git commit但我不知道如何做好。我知道通过man()在以下位置定义函数来实现此目的的“肮脏”方法.bashrc

man()
{
    case ${#} in
        0) /usr/bin/man ;;
        1) /usr/bin/man "${1}" ;;
        2) /usr/bin/man "${1}-${2}" ;;
        *) echo "Too many arguments" ;;
    esac
}

有人可以告诉我如何改进我的文档,以便我的用户可以将选项传递给man

答案1

man 的手册页解释了如何特殊处理在命令行上给出的名称对:

   --no-subpages
          By default, man will try to interpret pairs of manual page names
          given on the command line as equivalent to a single manual  page
          name  containing  a  hyphen or an underscore.  This supports the
          common pattern of programs that implement a  number  of  subcom-
          mands,  allowing  them to provide manual pages for each that can
          be accessed using similar syntax as would be used to invoke  the
          subcommands themselves.  For example:
            $ man -aw git diff
            /usr/share/man/man1/git-diff.1.gz

所以你只需要为你的每个选项提供单独的手册页文件,就像git提供的git.1.gz等等git-diff.1.gz,你就会自动得到你想要的。

答案2

这已经是一个功能曼德布人,Linux 发行版上常见的实现。从man 1 man:

--no-subpages
  By  default,  man  will  try  to  interpret pairs of manual page names given on the
  command line as equivalent to a single manual page name containing a hyphen  or  an
  underscore.   This  supports the common pattern of programs that implement a number
  of subcommands, allowing them to provide manual pages for each that can be accessed
  using  similar  syntax  as would be used to invoke the subcommands themselves.  For
  example:

    $ man -aw git diff
    /usr/share/man/man1/git-diff.1.gz

因此,您所需要做的就是拥有一个正确命名为 的联机帮助页<command>-<subcommand>

这可能不是其他实现中的功能。例如,macOSman不支持:

~ man git commit
# shows git manpage
No manual entry for commit

相关内容