Skip to content

Commit 55cfe48

Browse files
committed
fix(ifdata): Add flags to manpage
1 parent 725853b commit 55cfe48

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

cmd/ifdata/usage.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,15 @@ func usageFunc(cmd *cobra.Command) error {
5252
cmd.SetUsageFunc(nil)
5353
return cmd.Usage()
5454
}
55+
56+
func ManOptions() string {
57+
var s strings.Builder
58+
for _, val := range formatterValues() {
59+
if val == fmtNone {
60+
continue
61+
}
62+
63+
_, _ = fmt.Fprintf(&s, "\n.PP\n\\fB%s\\fP\n\t%s\n", val, val.description())
64+
}
65+
return s.String()
66+
}

internal/generate/manpages/main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"compress/gzip"
56
"errors"
67
"io"
@@ -11,6 +12,7 @@ import (
1112
"time"
1213

1314
"github.com/gabe565/moreutils/cmd"
15+
"github.com/gabe565/moreutils/cmd/ifdata"
1416
"github.com/gabe565/moreutils/internal/cmdutil"
1517
"github.com/gabe565/moreutils/internal/cmdutil/subcommands"
1618
"github.com/gabe565/moreutils/internal/generate/seealsoreplacer"
@@ -83,6 +85,14 @@ func main() {
8385
w = seealsoreplacer.New(w, ".SH SEE ALSO\n", linked)
8486
}
8587

88+
if subCmd.Name() == ifdata.Name {
89+
w = optionsInserter{
90+
w: w,
91+
find: "help for " + ifdata.Name + "\n",
92+
insert: ifdata.ManOptions(),
93+
}
94+
}
95+
8696
if err := doc.GenMan(subCmd, &header, w); err != nil {
8797
panic(err)
8898
}
@@ -97,3 +107,27 @@ func main() {
97107
}
98108
}
99109
}
110+
111+
type optionsInserter struct {
112+
w io.Writer
113+
find string
114+
insert string
115+
}
116+
117+
func (o optionsInserter) Write(p []byte) (int, error) {
118+
if !strings.HasSuffix(o.insert, "\n") {
119+
o.insert += "\n"
120+
}
121+
122+
if bytes.Contains(p, []byte(o.find)) {
123+
beforeIdx := bytes.Index(p, []byte(o.find))
124+
if beforeIdx == -1 {
125+
panic("missing header: " + o.find)
126+
}
127+
beforeIdx += len(o.find)
128+
129+
_, err := o.w.Write(slices.Concat(p[:beforeIdx], []byte(o.find), []byte(o.insert), p[beforeIdx:]))
130+
return len(p), err
131+
}
132+
return o.w.Write(p)
133+
}

0 commit comments

Comments
 (0)