aboutsummaryrefslogtreecommitdiff
path: root/opts.go
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-11-29 22:09:17 +0100
committerThomas Voss <mail@thomasvoss.com> 2023-11-29 22:09:17 +0100
commitf5fbe4aecdc04815cb48f047578e193a6289c3c5 (patch)
tree7a9de1f5475803c16a5edfeec4fb04e909df7a1d /opts.go
parenta6c83a3f1b14512a4138fb1744bafdf3f59caaa0 (diff)
Support optional-args for short options
Diffstat (limited to 'opts.go')
-rw-r--r--opts.go27
1 files changed, 18 insertions, 9 deletions
diff --git a/opts.go b/opts.go
index 3d07b0e..4fe07a5 100644
--- a/opts.go
+++ b/opts.go
@@ -62,14 +62,23 @@ type LongOpt struct {
// of the first non-option argument in optind. In the case of failure,
// err will be one of [BadOptionError] or [NoArgumentError].
func Get(args []string, optstr string) (flags []Flag, optind int, err error) {
- argmap := make(map[rune]bool)
+ argmap := make(map[rune]ArgMode)
rs := []rune(optstr)
- for i, r := range rs {
- if r != ':' {
- argmap[r] = false
- } else if i > 0 {
- argmap[rs[i-1]] = true
+ if rs[0] == ':' {
+ rs = rs[1:]
+ }
+ for len(rs) > 0 {
+ switch r := rs[0]; {
+ case len(rs) > 2 && rs[1] == ':' && rs[2] == ':':
+ argmap[r] = Optional
+ rs = rs[3:]
+ case len(rs) > 1 && rs[1] == ':':
+ argmap[r] = Required
+ rs = rs[2:]
+ default:
+ argmap[r] = None
+ rs = rs[1:]
}
}
@@ -86,14 +95,14 @@ func Get(args []string, optstr string) (flags []Flag, optind int, err error) {
rs := []rune(arg[1:])
for j, r := range rs {
var s string
- argp, ok := argmap[r]
+ am, ok := argmap[r]
switch {
case !ok:
return nil, 0, BadOptionError(r)
- case argp && j < len(rs)-1:
+ case am != None && j < len(rs)-1:
s = string(rs[j+1:])
- case argp:
+ case am == Required:
i++
if i >= len(args) {
return nil, 0, NoArgumentError(r)