Skip to content

feat: support compact short flag syntax with compact struct tag - #306

Open
AdamMagued wants to merge 2 commits into
alexflint:masterfrom
AdamMagued:feat/issue-289-compact-short-flags
Open

AdamMagued wants to merge 2 commits into
alexflint:masterfrom
AdamMagued:feat/issue-289-compact-short-flags

Conversation

@AdamMagued

Copy link
Copy Markdown

Resolves #289.

Summary

  • Added compact struct tag support to allow short options to be specified without a separating space (e.g. -c10)
  • Preserved support for separated arguments (-c 10) and explicit equals assignments (-c=10)
  • Added validation ensuring compact is only configured on short options taking values
  • Added comprehensive unit tests in parse_test.go validating compact short option parsing and edge cases

Verified 100% passing tests in container sandbox.

@alexflint alexflint left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 somevalue

for 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.

Comment thread parse.go Outdated

// 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])

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure to handle options that are unicode characters here, as in arg:"-汉,compact".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@AdamMagued

Copy link
Copy Markdown
Author
  • parse.go: Parser.process now calls findOption first so existing long and single-hyphen option resolution takes precedence over compact short flag fallback
  • parse.go: cmdFromStruct and Parser.process now use utf8.RuneCountInString and utf8.DecodeRuneInString to support multi-byte UTF-8 short flags such as arg:"-汉,compact"
  • parse_test.go: Added TestCompactShortFlagUnicode and TestCompactShortFlagLongOptionPrecedence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: no space between the flag and the arg

2 participants