-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.go
More file actions
175 lines (144 loc) · 4.03 KB
/
args.go
File metadata and controls
175 lines (144 loc) · 4.03 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
/*
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 (
"fmt"
"path/filepath"
"regexp"
"strings"
)
// Args provides a single point to access and extract program arguments.
type Args struct {
usageDefined map[string]bool
usageHeader string
usageSynopsis []string
usageBody string
lineWidth int
programName string
programDesc string
args []string
err error
}
var reIsGroup = regexp.MustCompile(`^-[A-Za-z]+$`)
func makeArgList(arg string) []string {
if reIsGroup.MatchString(arg) {
list := make([]string, 0, len(arg)-1)
for _, option := range arg[1:] {
list = append(list, "-"+string(option))
}
return list
}
return []string{arg}
}
// New creates a new Args object based in the arguments passed. The first
// element of the arguments must be the program name.
func New(programDesc string, args []string) *Args {
if len(args) < 1 {
return &Args{
usageDefined: make(map[string]bool),
usageHeader: "",
usageSynopsis: nil,
usageBody: "",
programName: "NotDefined",
programDesc: prepareDesc("", programDesc),
lineWidth: defaultLineWidth,
args: nil,
err: ErrNoArgs,
}
}
var myArgs []string
for _, arg := range args[1:] {
myArgs = append(myArgs, makeArgList(arg)...)
}
return &Args{
usageDefined: make(map[string]bool),
usageHeader: "",
usageSynopsis: nil,
usageBody: "",
programName: filepath.Base(args[0]),
programDesc: prepareDesc("", programDesc),
lineWidth: defaultLineWidth,
args: myArgs,
err: nil,
}
}
// PushErr registers the provided error if not nil to the Args error stack.
func (args *Args) PushErr(err error) {
if err != nil {
if args.err == nil {
args.err = err
} else {
args.err = fmt.Errorf("%w: %w", args.err, err)
}
}
}
// Err returns any errors encountered or registered while parsing the
// arguments.
func (args *Args) Err() error {
return args.err
}
// HasErr returns true if any errors have been encountered or registered.
func (args *Args) HasErr() bool {
return args.err != nil
}
// HasNext returns true if any arguments remain unabsorbed.
func (args *Args) HasNext() bool {
return len(args.args) > 0
}
// PushArg places the supplied argument to the end of the internal args list.
func (args *Args) PushArg(arg string) {
args.args = append(args.args, arg)
}
// Args returns a copy of the current argument list.
func (args *Args) Args() []string {
cpy := make([]string, len(args.args))
copy(cpy, args.args)
return cpy
}
// Count returns the number of times the flag appears.
func (args *Args) Count(flag, desc string) int {
var count int
args.RegisterUsage(flag, desc)
count, args.args = argFlag(flag).count(args.args)
return count
}
// Is returns true if the flag is present one and only one time.
func (args *Args) Is(flag, desc string) bool {
var (
found bool
err error
)
args.RegisterUsage(flag, desc)
found, args.args, err = argFlag(flag).is(args.args)
if err != nil {
args.PushErr(err)
}
return found
}
// Done registers an error if there are any remaining arguments.
func (args *Args) Done() {
if len(args.args) > 0 {
args.PushErr(
fmt.Errorf("%w: [%v]",
ErrUnexpected,
strings.Join(args.args, " "),
),
)
}
}
// ProgramName returns the configured program name.
func (args *Args) ProgramName() string {
return args.programName
}