diff options
| author | Thomas Voss <mail@thomasvoss.com> | 2023-11-29 22:09:17 +0100 | 
|---|---|---|
| committer | Thomas Voss <mail@thomasvoss.com> | 2023-11-29 22:09:17 +0100 | 
| commit | f5fbe4aecdc04815cb48f047578e193a6289c3c5 (patch) | |
| tree | 7a9de1f5475803c16a5edfeec4fb04e909df7a1d /opts.go | |
| parent | a6c83a3f1b14512a4138fb1744bafdf3f59caaa0 (diff) | |
Support optional-args for short options
Diffstat (limited to 'opts.go')
| -rw-r--r-- | opts.go | 27 | 
1 files changed, 18 insertions, 9 deletions
| @@ -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) |