Repository navigation
Expand file tree
/
Copy pathusage.go
More file actions
148 lines (118 loc) · 3.55 KB
/
Copy pathusage.go
File metadata and controls
148 lines (118 loc) · 3.55 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Szerszam argument library: szargs.
Copyright (C) 2024 Leslie Dancsecs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package szargs
import (
"os"
"strings"
"golang.org/x/term"
)
const (
usagePrefix = "usage: "
defaultLineWidth = 78
)
// Prepare desc appends paragraphs (separated by blank lines) to single lines
// in preparation for reflowing to different widths.
func prepareDesc(prefix, desc string) string {
var res strings.Builder
lines := strings.Split(strings.Trim(desc, "\n"), "\n")
first := 0
for i, l := range lines {
if l == "" {
res.WriteString(
prefix +
strings.Join(lines[first:i], " ") +
"\n\n",
)
first = i + 1
}
}
if first < len(lines) {
res.WriteString(prefix + strings.Join(lines[first:], " "))
}
return strings.TrimRight(res.String(), "\n")
}
const spacePlaceholder = "\u001f"
// RegisterUsage registers a new flag and its description if and only if the
// flag has not been already registered.
func (args *Args) RegisterUsage(item, desc string) {
if !args.usageDefined[item] {
args.usageHeader += " " +
strings.ReplaceAll(item, " ", spacePlaceholder)
args.usageBody += "\n" + item + "\n" +
prepareDesc(" ", desc) + "\n"
args.usageDefined[item] = true
}
}
func terminalWidth(out *os.File) int {
fd := int(out.Fd())
if term.IsTerminal(fd) {
termWidth, _, err := term.GetSize(fd)
if err != nil || termWidth == 0 {
termWidth = defaultLineWidth
}
return termWidth
}
return defaultLineWidth
}
func (args *Args) buildSynopsisHeader(synopsisVersions []string) string {
var header string
for i, synopsis := range synopsisVersions {
if i == 0 {
header += "\n" + usagePrefix
} else {
header += "\n" + strings.Repeat(" ", len(usagePrefix))
}
header += args.programName + " " + synopsis
}
return header
}
// Usage returns a usage messages representing the Args object. It is
// formatted to the lineWidth provided. A zero uses the defaultLineWidth
// while a negative value caused an effort to determine if writing to a
// terminal and if so using its width otherwise defaulting.
func (args *Args) Usage(lineWidth int) string {
var header string
if lineWidth < 0 {
lineWidth = terminalWidth(os.Stdout)
}
if lineWidth < 1 {
lineWidth = defaultLineWidth
}
args.lineWidth = lineWidth
if len(args.usageSynopsis) > 0 {
header = args.buildSynopsisHeader(args.usageSynopsis)
} else {
if args.usageHeader == "" {
header = "\nusage: " + args.programName
} else {
header = "\n" + reflowLine("usage: "+args.programName+" ",
args.usageHeader,
args.lineWidth,
)
header = strings.ReplaceAll(header, spacePlaceholder, " ")
}
}
return strings.TrimRight(
header[1:]+
"\n\n"+
reflowLine("", args.programDesc, args.lineWidth)+"\n\n"+
reflowLines(" ", args.usageBody, args.lineWidth)+"\n",
"\n",
)
}
// AddSynopsis includes another static synopsis message.
func (args *Args) AddSynopsis(s string) {
args.usageSynopsis = append(args.usageSynopsis, s)
}