Skip to content

fix: keep args after -- verbatim in strict mode - #176

Merged
inhere merged 1 commit into
gookit:masterfrom
youdie006:strict-keep-args-after-dashdash
Oct 29, 2026
Merged

inhere merged 1 commit into
gookit:masterfrom
youdie006:strict-keep-args-after-dashdash

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

#175 made expandShortArgs leave the arguments after -- alone, like the other gflag preprocessors (shorts.go, reorder.go, flags.go). Strict mode's strictFormatArgs still rewrites them, so --a becomes -a and ---b becomes -b before the command sees them:

gcli.SetStrictMode(true)
app.Run([]string{"test", "---name", "tom", "--", "--a", "---b", "--c=1"})
// before: name="tom" files=["-a" "-b" "--c=1"]
// after:  name="tom" files=["--a" "---b" "--c=1"]

The fix stops normalizing at the first -- and appends the rest unchanged. The new test also checks that an option after a positional argument (file ---name tom) is still normalized.

One input changes in a way that isn't a fix: if a value-taking option is given -- as its value (--name -- ---b), the ---b after it is no longer rewritten. Strict mode also still rewrites the value of a value-taking option (--name --a gives -a). Fixing that would need the option metadata, so I left it out.

go test ./..., go vet ./... and gofmt pass on Go 1.26.3.

Written with AI assistance (Claude); I have reviewed the change.

strictFormatArgs normalized every arg, including the positional
literals after "--", so "-- --a" reached the command as "-a".
@inhere
inhere merged commit 3fc8192 into gookit:master Oct 29, 2026
7 checks passed
inhere added a commit that referenced this pull request Oct 29, 2026
…tion values verbatim

The strict mode normalization lived in gcli/util.go while the option metadata it
needs sits in gflag, so it normalized every option-looking token: the value of a
value-taking option was rewritten too.

    gcli.SetStrictMode(true)
    app.Run([]string{"test", "--name", "--a"})  // before: name="-a", now: "--a"

- Gflag.StrictFormatArgs(args, fs) is the single implementation now, next to
  ExpandShortArgs and RearrangeArgs. It keeps the tokens after "--" verbatim (from
  #176) and the value token of a known value-taking option, which optMeta reports
  with its short alias resolved, so "-n ---deep" is protected like "--name".
- the option token itself is still normalized before its value is copied, so
  "---name tom" keeps working; a bool option takes no value, so the token after it
  is still normalized as an option; "--" as a value keeps the previous behaviour.
- gcli keeps the mode switch (SetStrictMode also enables EnhanceShort) and calls
  the gflag helper; the local strictFormatArgs is gone.

Tests: a table of StrictFormatArgs cases (form, values, terminator, unknown
options, the short alias) and an end-to-end gcli case for the values that look
like options. A 252-cell matrix (strict x EnhanceShort level x reorder x 21 arg
vectors) differs from the previous tree only in the three intended inputs.
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.

2 participants