-
Notifications
You must be signed in to change notification settings - Fork 15
/
pingtype.go
49 lines (41 loc) · 977 Bytes
/
pingtype.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
// Copyright (c) Berk D. Demir and the runitor contributors.
// SPDX-License-Identifier: 0BSD
package main
import (
"flag"
"fmt"
"strings"
)
// PingType is an enumerator type for PingType* constants.
type PingType int
//go:generate enumer -type PingType -trimprefix=PingType -transform=kebab
const (
PingTypeExitCode PingType = iota
PingTypeSuccess
PingTypeFail
PingTypeLog
)
func pingTypeFlag(name string, dflt PingType, usage string) *PingType {
p := new(PingType)
*p = dflt
opts := fmt.Sprintf("%s (default %s)", pingTypeOpts("|"), dflt)
usage = usage + " (" + opts + ")"
flag.Func(name, usage, func(s string) (err error) {
*p, err = PingTypeString(s)
if err != nil {
err = fmt.Errorf("recognized options: %s", opts)
}
return err
})
return p
}
func pingTypeOpts(sep string) string {
var b strings.Builder
for _, v := range PingTypeValues() {
if b.Len() > 0 {
b.WriteString(sep)
}
b.WriteString(v.String())
}
return b.String()
}