Skip to content

Commit

Permalink
make the man page section selectable
Browse files Browse the repository at this point in the history
Signed-off-by: Nobuhiro MIKI <nob@bobuhiro11.net>
  • Loading branch information
bobuhiro11 committed Jan 27, 2021
1 parent 44ec985 commit 25e94ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
19 changes: 14 additions & 5 deletions docs.go
Expand Up @@ -15,38 +15,47 @@ import (
// The function errors if either parsing or writing of the string fails.
func (a *App) ToMarkdown() (string, error) {
var w bytes.Buffer
if err := a.writeDocTemplate(&w); err != nil {
if err := a.writeDocTemplate(&w, 8); err != nil {
return "", err
}
return w.String(), nil
}

// ToMan creates a man page string for the `*App`
// ToMan creates a man page string with section number for the `*App`
// The function errors if either parsing or writing of the string fails.
func (a *App) ToMan() (string, error) {
func (a *App) ToManWithSection(sectionNumber int) (string, error) {
var w bytes.Buffer
if err := a.writeDocTemplate(&w); err != nil {
if err := a.writeDocTemplate(&w, sectionNumber); err != nil {
return "", err
}
man := md2man.Render(w.Bytes())
return string(man), nil
}

// ToMan creates a man page string for the `*App`
// The function errors if either parsing or writing of the string fails.
func (a *App) ToMan() (string, error) {
man, err := a.ToManWithSection(8)
return man, err
}

type cliTemplate struct {
App *App
SectionNum int
Commands []string
GlobalArgs []string
SynopsisArgs []string
}

func (a *App) writeDocTemplate(w io.Writer) error {
func (a *App) writeDocTemplate(w io.Writer, sectionNum int) error {
const name = "cli"
t, err := template.New(name).Parse(MarkdownDocTemplate)
if err != nil {
return err
}
return t.ExecuteTemplate(w, name, &cliTemplate{
App: a,
SectionNum: sectionNum,
Commands: prepareCommands(a.Commands, 0),
GlobalArgs: prepareArgsWithValues(a.VisibleFlags()),
SynopsisArgs: prepareArgsSynopsis(a.VisibleFlags()),
Expand Down
12 changes: 12 additions & 0 deletions docs_test.go
Expand Up @@ -147,3 +147,15 @@ func TestToMan(t *testing.T) {
expect(t, err, nil)
expectFileContent(t, "testdata/expected-doc-full.man", res)
}

func TestToManWithSection(t *testing.T) {
// Given
app := testApp()

// When
res, err := app.ToManWithSection(8)

// Then
expect(t, err, nil)
expectFileContent(t, "testdata/expected-doc-full.man", res)
}
2 changes: 1 addition & 1 deletion template.go
Expand Up @@ -74,7 +74,7 @@ OPTIONS:
{{end}}{{end}}
`

var MarkdownDocTemplate = `% {{ .App.Name }} 8
var MarkdownDocTemplate = `% {{ .App.Name }} {{ .SectionNum }}
# NAME
Expand Down

0 comments on commit 25e94ac

Please sign in to comment.