Repository navigation
feat: support compact short flag syntax with compact struct tag - #306
AdamMagued wants to merge 2 commits into
Conversation
alexflint
left a comment
There was a problem hiding this comment.
Thanks for this @AdamMagued.
One issue with this feature is that go-arg currently supports long arguments with a single hyphen, as in
./program -something somevaluefor an argument struct like this:
var args struct {
Something string
}
As a result, there is an ambiguity for compact flags that are prefixes of another long flag, as in:
var args struct {
S string `arg:"-s,compact"
Something string
}
In this case, ./program -something somevalue could be parsed with the -s flag getting the value "omething" and "somevalue" being interpreted as a positional.
Probably the best way to resolve this is to make sure that if an option can be resolved using the existing semantics then those take precedence. If findOption fails, then consider compact options.
|
|
||
| // check for compact short flag syntax, e.g. -c10 | ||
| if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 && !strings.Contains(arg, "=") { | ||
| shortChar := string(arg[1]) |
There was a problem hiding this comment.
Please make sure to handle options that are unicode characters here, as in arg:"-汉,compact".
There was a problem hiding this comment.
Updated cmdFromStruct and Parser.process in parse.go to use utf8.RuneCountInString and utf8.DecodeRuneInString for multi-byte UTF-8 short flags such as -汉, with unit tests added in TestCompactShortFlagUnicode.
…ct flags Signed-off-by: AdamMagued <adamismailmageud@gmail.com>
|
Resolves #289.
Summary
compactstruct tag support to allow short options to be specified without a separating space (e.g.-c10)-c 10) and explicit equals assignments (-c=10)compactis only configured on short options taking valuesparse_test.govalidating compact short option parsing and edge casesVerified 100% passing tests in container sandbox.