Add 'manpage' command to generate manual pages

This commit is contained in:
Alexander Neumann 2017-08-05 10:57:01 +02:00
parent 9bf3141893
commit a3ab17b470

58
cmd/restic/cmd_manpage.go Normal file
View file

@ -0,0 +1,58 @@
package main
import (
"os"
"github.com/restic/restic/internal/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
var cmdManpage = &cobra.Command{
Use: "manpage [command]",
Short: "generate manual pages",
Long: `
The "manpage" command generates a manual page for a single command. It can also
be used to write all manual pages to a directory. If the output directory is
set and no command is specified, all manpages are written to the directory.
`,
RunE: runManpage,
}
var manpageOpts = struct {
OutputDir string
}{}
func init() {
cmdRoot.AddCommand(cmdManpage)
fs := cmdManpage.Flags()
fs.StringVar(&manpageOpts.OutputDir, "output-dir", "", "write man pages to this `directory`")
}
func runManpage(cmd *cobra.Command, args []string) error {
header := &doc.GenManHeader{
Title: "restic backup",
Section: "1",
Source: "generated by `restic manpage`",
}
dir := manpageOpts.OutputDir
if dir != "" {
Verbosef("writing man pages to directory %v\n", dir)
return doc.GenManTree(cmdRoot, header, dir)
}
if len(args) > 1 {
return errors.Fatalf("more than one command given: %v", args)
}
name := args[0]
for _, cmd := range cmdRoot.Commands() {
if cmd.Name() == name {
return doc.GenMan(cmd, header, os.Stdout)
}
}
return errors.Fatalf("command %q is not known", args)
}