Repository navigation
Expand file tree
/
Copy pathusage.go
More file actions
373 lines (244 loc) · 7.2 KB
/
Copy pathusage.go
File metadata and controls
373 lines (244 loc) · 7.2 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2019-2025 Matthew Wilson and Synesis Information Systems.
// Copyright 2015-2019 Matthew Wilson. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
/*
* Created: 4th September 2015
* Updated: 28th February 2025
*/
package clasp
import (
"fmt"
"io"
"os"
"path"
"reflect"
"strings"
)
/* /////////////////////////////////////////////////////////////////////////
* types
*/
// T.B.C.
type UsageFlag int
// Protocol for exiting process.
type Exiter interface {
Exit(exitCode int)
}
// Defines options for usage (see [ShowUsage]).
type UsageParams struct {
Stream io.Writer
ProgramName string
UsageFlags UsageFlag
ExitCode int
Exiter Exiter
Version any
VersionPrefix string
InfoLines []string
ValuesString string
// If the empty string is specified, then a default string is used if
// any specifications are specified; if a whitespace-only string is specified,
// then no flags/options element is presented
FlagsAndOptionsString string
}
func (params UsageParams) String() string {
return fmt.Sprintf("<%T{ Stream=%v, ProgramName=%q, UsageFlags=0x%x, ExitCode=%d, Exiter=%v, Version=%v, VersionPrefix=%q, InfoLines=%v, ValuesString=%q, FlagsAndOptionsString=%q }>", params, params.Stream, params.ProgramName, params.UsageFlags, params.ExitCode, params.Exiter, params.Version, params.VersionPrefix, params.InfoLines, params.ValuesString, params.FlagsAndOptionsString)
}
/* /////////////////////////////////////////////////////////////////////////
* helpers
*/
const (
Usage_None UsageFlag = 0
)
// TODO: rename the following with `Usage_` prefix
const (
SkipBlanksBetweenLines UsageFlag = 1 << iota // T.B.C.
DontCallExit // T.B.C.
DontCallExitIfZero // T.B.C.
)
/* /////////////////////////////////////////////////////////////////////////
* locals
*/
type default_exiter struct {
}
func (de *default_exiter) Exit(exitCode int) {
os.Exit(exitCode)
}
func should_call_Exit(params UsageParams) bool {
if 0 != (DontCallExit & params.UsageFlags) {
return false
}
if 0 == params.ExitCode {
if 0 != (DontCallExitIfZero & params.UsageFlags) {
return false
}
}
return true
}
func collect_array_as_strings(a []any) []string {
r := make([]string, len(a))
for i, v := range a {
r[i] = fmt.Sprintf("%v", v)
}
return r
}
func get_program_name(params UsageParams) string {
program_name := params.ProgramName
if 0 == len(program_name) {
arg0 := os.Args[0]
program_name = path.Base(arg0)
}
return program_name
}
func generate_version_string(params UsageParams, apiFunctionName string) string {
program_name := get_program_name(params)
version_prefix := params.VersionPrefix
var version string
switch v := params.Version.(type) {
case string:
version = v
case []string:
version = strings.Join(v, ".")
case []int:
as := make([]string, len(v))
for i, n := range v {
as[i] = fmt.Sprintf("%v", n)
}
version = strings.Join(as, ".")
case []uint16:
as := make([]string, len(v))
for i, n := range v {
as[i] = fmt.Sprintf("%v", n)
}
version = strings.Join(as, ".")
default:
panic(fmt.Sprintf("%v() called with UsageParams.Version of an invalid type %T, but must be instance of string, []string, []int, []uint16", v, reflect.TypeOf(v)))
}
return fmt.Sprintf("%s %s%s", program_name, version_prefix, version)
}
/* /////////////////////////////////////////////////////////////////////////
* API
*/
// T.B.C.
func ShowUsage(specifications []Specification, params UsageParams) (rc int, err error) {
for i, a := range specifications {
switch a.Type {
case FlagType, OptionType:
case SectionType:
default:
panic(fmt.Sprintf("specification[%d] - '%v' - is an instance of type %T, but must be instance of either %T or %T!", i, a, a, FlagType, OptionType))
}
}
exiter := params.Exiter
if exiter == nil {
exiter = new(default_exiter)
}
if params.Stream == nil {
if 0 == params.ExitCode {
params.Stream = os.Stdout
} else {
params.Stream = os.Stderr
}
}
program_name := get_program_name(params)
if "" == params.FlagsAndOptionsString && 0 != len(specifications) {
params.FlagsAndOptionsString = "[ ... flags and options ... ]"
}
if "" != strings.TrimSpace(params.FlagsAndOptionsString) {
params.FlagsAndOptionsString = " " + params.FlagsAndOptionsString
} else {
params.FlagsAndOptionsString = ""
}
if "" != params.ValuesString {
params.ValuesString = " " + params.ValuesString
}
for _, info_line := range params.InfoLines {
if ":version:" == info_line {
version := generate_version_string(params, "ShowUsage")
fmt.Fprintf(params.Stream, "%s\n", version)
} else {
fmt.Fprintf(params.Stream, "%s\n", info_line)
}
}
fmt.Fprintf(params.Stream, "USAGE: %s%s%s\n", program_name, params.FlagsAndOptionsString, params.ValuesString)
if 0 != len(specifications) {
fmt.Fprintf(params.Stream, "\n")
fmt.Fprintf(params.Stream, "flags/options:\n")
if 0 == (SkipBlanksBetweenLines & params.UsageFlags) {
fmt.Fprintf(params.Stream, "\n")
}
voas := make(map[string][]Specification)
pure := make([]Specification, 0)
for _, a := range specifications {
ix_eq := strings.Index(a.Name, "=")
if ix_eq < 0 {
pure = append(pure, a)
} else {
name := a.Name[0:ix_eq]
if _, ok := voas[name]; !ok {
voas[name] = make([]Specification, 0)
}
voas[name] = append(voas[name], a)
}
}
for _, a := range pure {
switch a.Type {
case FlagType:
for _, b := range a.Aliases {
fmt.Fprintf(params.Stream, "\t%v\n", b)
}
fmt.Fprintf(params.Stream, "\t%v\n", a.Name)
case OptionType:
for _, c := range voas[a.Name] {
fmt.Fprintf(params.Stream, "\t%v %v\n", c.Aliases[0], c.Name)
}
for _, b := range a.Aliases {
fmt.Fprintf(params.Stream, "\t%v <value>\n", b)
}
fmt.Fprintf(params.Stream, "\t%v=<value>\n", a.Name)
case SectionType:
if 0 != (SkipBlanksBetweenLines & params.UsageFlags) {
fmt.Fprintf(params.Stream, "\n")
}
fmt.Fprintf(params.Stream, "\t%v\n\n", a.Name)
continue
}
if 0 != len(a.Help) {
fmt.Fprintf(params.Stream, "\t\t%v\n", a.Help)
}
if 0 != len(a.ValueSet) {
fmt.Fprintf(params.Stream, "\t\twhere <value> one of:\n")
for j := 0; j != len(a.ValueSet); j++ {
fmt.Fprintf(params.Stream, "\t\t\t%v\n", a.ValueSet[j])
}
}
if 0 == (SkipBlanksBetweenLines & params.UsageFlags) {
fmt.Fprintf(params.Stream, "\n")
}
}
}
if should_call_Exit(params) {
exiter.Exit(params.ExitCode)
}
return params.ExitCode, nil
}
// T.B.C.
func ShowVersion(specifications []Specification, params UsageParams) (rc int, err error) {
exiter := params.Exiter
if params.Stream == nil {
if 0 == params.ExitCode {
params.Stream = os.Stdout
} else {
params.Stream = os.Stderr
}
}
if exiter == nil {
exiter = new(default_exiter)
}
version := generate_version_string(params, "ShowVersion")
fmt.Fprintf(params.Stream, "%s\n", version)
if should_call_Exit(params) {
exiter.Exit(params.ExitCode)
}
return
}
/* ///////////////////////////// end of file //////////////////////////// */