File: adapter.go

package info (click to toggle)
golang-github-alecthomas-mango-kong 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 248 kB
  • sloc: sh: 32; makefile: 3
file content (71 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package mangokong

import (
	"fmt"
	"strings"

	"github.com/alecthomas/kong"
	"github.com/muesli/mango"
	"github.com/muesli/roff"
)

// ManFlag can be used as a Kong flag that will write a default man page to stdout.
type ManFlag bool

func (m ManFlag) BeforeApply(app *kong.Kong) error {
	man := NewManPage(1, app.Model)
	fmt.Fprint(app.Stdout, man.Build(roff.NewDocument()))
	app.Exit(0)
	return nil
}

// NewManPage from a Kong Application.
func NewManPage(section uint, app *kong.Application) *mango.ManPage {
	help := strings.Split(strings.TrimSpace(app.Help), "\n")
	man := mango.NewManPage(section, app.Name, help[0])
	if app.Detail != "" {
		man = man.WithLongDescription(app.Detail)
	} else {
		man = man.WithLongDescription(strings.TrimSpace(strings.Join(help[1:], "\n")))
	}
	addCommand(man, app.Node, nil)
	return man
}

func addCommand(man *mango.ManPage, node *kong.Node, parent *mango.Command) {
	var item *mango.Command
	name := node.Summary()
	if parent == nil {
		name = node.Name
	}
	item = mango.NewCommand(name, node.Help, node.Detail)
	if parent == nil {
		man.Root = *item
		item = &man.Root
	} else {
		err := parent.AddCommand(item)
		if err != nil { // Kong already validates structure, so this should never fail.
			panic(err)
		}
	}

	for _, child := range node.Children {
		if child.Hidden {
			continue
		}
		addCommand(man, child, item)
	}

	for _, flag := range node.Flags {
		short := ""
		if flag.Short != 0 {
			short = fmt.Sprintf("%c", flag.Short)
		}
		item.AddFlag(mango.Flag{
			Name:  strings.TrimPrefix(flag.Summary(), "--"),
			Short: short,
			Usage: flag.Help,
			PFlag: true,
		})
	}
}