forked from platinasystems/goes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathman.go
84 lines (75 loc) · 1.48 KB
/
man.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package goes
import (
"fmt"
"strings"
"github.com/platinasystems/goes/cmd"
"github.com/platinasystems/goes/lang"
)
type maner interface {
Man() lang.Alt
}
var section = struct {
name, synopsis lang.Alt
}{
name: lang.Alt{
lang.EnUS: "NAME",
},
synopsis: lang.Alt{
lang.EnUS: "SYNOPSIS",
},
}
func (g *Goes) Man() lang.Alt {
man := g.MAN
if man == nil {
man = lang.Alt{
lang.EnUS: `
OPTIONS
-d debug block handling
-x print command trace
-f don't terminate script on error
- execute standard input script
SCRIPT execute named script file
SEE ALSO
goes apropos [COMMAND], goes man COMMAND`,
}
}
return man
}
func (g *Goes) man(args ...string) error {
var cmds []cmd.Cmd
for i, arg := range args {
v := g.ByName[arg]
if v == nil {
if i == 0 {
return fmt.Errorf("%s: not found", arg)
}
break
}
cmds = append(cmds, v)
}
if len(cmds) == 0 {
cmds = []cmd.Cmd{g}
}
for i, v := range cmds {
if i > 0 {
fmt.Println()
}
fmt.Print(section.name, "\n\t", v, " - ",
v.Apropos(), "\n\n", section.synopsis, "\n\t",
strings.TrimSpace(v.Usage()), "\n")
if method, found := v.(maner); found {
man := method.Man().String()
if !strings.HasPrefix(man, "\n") {
fmt.Println()
}
fmt.Print(man)
if !strings.HasSuffix(man, "\n") {
fmt.Println()
}
}
}
return nil
}