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 25, 2021
1 parent 2e9dc40 commit a6a5f77
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
12 changes: 7 additions & 5 deletions docs.go
Expand Up @@ -13,19 +13,19 @@ import (

// ToMarkdown creates a markdown string for the `*App`
// The function errors if either parsing or writing of the string fails.
func (a *App) ToMarkdown() (string, error) {
func (a *App) ToMarkdown(sectionNum int) (string, error) {
var w bytes.Buffer
if err := a.writeDocTemplate(&w); err != nil {
if err := a.writeDocTemplate(&w, sectionNum); err != nil {
return "", err
}
return w.String(), 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) {
func (a *App) ToMan(sectionNum int) (string, error) {
var w bytes.Buffer
if err := a.writeDocTemplate(&w); err != nil {
if err := a.writeDocTemplate(&w, sectionNum); err != nil {
return "", err
}
man := md2man.Render(w.Bytes())
Expand All @@ -34,19 +34,21 @@ func (a *App) ToMan() (string, error) {

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
10 changes: 5 additions & 5 deletions docs_test.go
Expand Up @@ -90,7 +90,7 @@ func TestToMarkdownFull(t *testing.T) {
app := testApp()

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

// Then
expect(t, err, nil)
Expand All @@ -103,7 +103,7 @@ func TestToMarkdownNoFlags(t *testing.T) {
app.Flags = nil

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

// Then
expect(t, err, nil)
Expand All @@ -116,7 +116,7 @@ func TestToMarkdownNoCommands(t *testing.T) {
app.Commands = nil

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

// Then
expect(t, err, nil)
Expand All @@ -129,7 +129,7 @@ func TestToMarkdownNoAuthors(t *testing.T) {
app.Authors = []*Author{}

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

// Then
expect(t, err, nil)
Expand All @@ -141,7 +141,7 @@ func TestToMan(t *testing.T) {
app := testApp()

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

// Then
expect(t, err, nil)
Expand Down
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 a6a5f77

Please sign in to comment.