-
Notifications
You must be signed in to change notification settings - Fork 0
/
clap.gen.go
154 lines (126 loc) · 3.71 KB
/
clap.gen.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
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
// generated by goclap; DO NOT EDIT
package main
import (
"flag"
"fmt"
"io"
"os"
"reflect"
"strconv"
)
type clapCommand struct {
usage func() string
opts []clapInput
args []clapInput
}
type clapInput struct {
name string
value flag.Value
required bool
}
func clapFatalf(cmdName, format string, args ...any) {
msg := fmt.Sprintf(format, args...)
fmt.Fprintf(os.Stderr, "error: %s.\nRun '%s -h' for usage.\n", msg, cmdName)
os.Exit(2)
}
func (cc *clapCommand) parse(args []string) ([]string, error) {
f := flag.FlagSet{Usage: func() {}}
f.SetOutput(io.Discard)
for i := range cc.opts {
o := &cc.opts[i]
f.Var(o.value, o.name, "")
}
if err := f.Parse(args); err != nil {
if err == flag.ErrHelp {
fmt.Println(cc.usage())
os.Exit(0)
}
return nil, err
}
rest := f.Args()
if len(cc.args) > 0 {
for i := range cc.args {
arg := &cc.args[i]
if len(rest) <= i {
if arg.required {
return nil, fmt.Errorf("missing required arg '%s'", arg.name)
}
return nil, nil
}
if err := arg.value.Set(rest[i]); err != nil {
return nil, fmt.Errorf("parsing positional argument '%s': %v", arg.name, err)
}
}
return nil, nil
}
return rest, nil
}
type clapBool bool
func clapNewBool(p *bool) *clapBool { return (*clapBool)(p) }
func (v *clapBool) String() string { return strconv.FormatBool(bool(*v)) }
func (v *clapBool) Set(s string) error {
b, err := strconv.ParseBool(s)
if err != nil {
return fmt.Errorf(`invalid boolean value "%s"`, s)
}
*v = clapBool(b)
return err
}
func (*clapBool) IsBoolFlag() bool { return true }
type clapString string
func clapNewString(p *string) *clapString { return (*clapString)(p) }
func (v *clapString) String() string { return string(*v) }
func (v *clapString) Set(s string) error {
*v = clapString(s)
return nil
}
type clapInt[T int | int8 | int16 | int32 | int64] struct{ v *T }
func clapNewInt[T int | int8 | int16 | int32 | int64](p *T) clapInt[T] { return clapInt[T]{p} }
func (v clapInt[T]) String() string { return strconv.FormatInt(int64(*v.v), 10) }
func (v clapInt[T]) Set(s string) error {
u64, err := strconv.ParseInt(s, 0, reflect.TypeFor[T]().Bits())
if err != nil {
return numError(err)
}
*v.v = T(u64)
return nil
}
func numError(err error) error {
if ne, ok := err.(*strconv.NumError); ok {
return ne.Err
}
return err
}
func (*goclap) UsageHelp() string {
return `goclap - Pre-build tool to generate command line argument parsing code from Go comments
usage:
goclap [options]
options:
-type <arg> The root command struct name
-srcdir <arg> Directory of source files to parse (default ".")
-with-version Include goclap's version info in the generated code
-out <arg> Output file path (default "./clap.gen.go")
-usg-layout-kind <arg> How the usage message for each command will be structured
(possible values: packed or roomy)
-usg-text-width <arg> Max width for lines of text in the usage message
-version Print version info and exit
-h Show this help message`
}
func (c *goclap) Parse(args []string) {
p := clapCommand{
usage: c.UsageHelp,
opts: []clapInput{
{name: "type", value: clapNewString(&c.rootCmdType)},
{name: "srcdir", value: clapNewString(&c.srcDir)},
{name: "with-version", value: clapNewBool(&c.withVersion)},
{name: "out", value: clapNewString(&c.outFilePath)},
{name: "usg-layout-kind", value: clapNewString(&c.usgLayoutKind)},
{name: "usg-text-width", value: clapNewInt(&c.usgTextWidth)},
{name: "version", value: clapNewBool(&c.version)},
},
}
_, err := p.parse(args)
if err != nil {
clapFatalf("goclap", err.Error())
}
}