Conversation
…scaping
By default the console splits input with POSIX shell semantics, so an
unquoted backslash escapes the next character. This mangles values that
carry literal backslashes (e.g. Windows paths: `C:\Windows\Temp` becomes
`C:WindowsTemp`) and makes a trailing backslash request a continuation
line, which is surprising when the console is used as a general Cobra
frontend rather than a shell.
Introduce a console-wide EscapeMode with two values:
- EscapeShell (default): unchanged POSIX escape behavior.
- EscapeLiteral: backslashes are ordinary characters; quotes still
group words, `C:\Windows\Temp` is passed through verbatim, and a
trailing backslash no longer requests another line.
Select it with Console.SetEscapeMode(console.EscapeLiteral). The mode is
threaded through all three shell-word splitters so command execution,
multiline-continuation detection, and completion/highlighting stay
consistent. The EscapeShell path is byte-for-byte unchanged.
Closes #88.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #89 +/- ##
==========================================
+ Coverage 26.75% 27.32% +0.57%
==========================================
Files 28 28
Lines 2119 2137 +18
==========================================
+ Hits 567 584 +17
+ Misses 1505 1503 -2
- Partials 47 50 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #88.
Problem
The console splits every input line with POSIX shell semantics, so an unquoted backslash escapes the next character. When the console is used as a general Cobra frontend rather than a shell, this is surprising:
ls C:\Windows\Tempreaches the command as["ls", "C:WindowsTemp"]— the backslashes are eaten.ls C:\Windows\Temp\is treated as an incomplete line (trailing backslash = continuation) and blocks waiting for more input.Users currently have to quote the whole value (
'C:\Windows\Temp') or double every backslash. As @h3zh1 noted in #88, that's cumbersome for a Cobra-frontend use case.I confirmed empirically that the
mvdan.cc/shcomment-stripping stage is innocent — it round-trips backslashes untouched. The mangling comes entirely from the shellquote-derived word splitters.Solution
A console-wide
EscapeMode, default unchanged:EscapeShell(default)EscapeLiteralC:\Windows\Temppasses through verbatim; a trailing backslash no longer requests another lineImplementation: each of the three shellquote-derived splitters gained an
EscapeModeparameter; in literal mode thecase char == EscapeCharbranch is simply skipped, so backslash falls through as a plain rune. Because literal mode never enters the escape state,ErrUnterminatedEscapecan't fire — the trailing-backslash multiline bug is fixed for free, while unterminated quotes still correctly request more input.Per the design discussion, the mode is threaded through all three call sites — command execution (
line.Parse), multiline detection (AcceptMultiline), and completion/highlighting — so aC:\path also completes and highlights consistently.Menu.RunCommandLinehonors it too.Tests
EscapeShelltests pass unmodified (only the new mode arg threaded through).internal/lineandinternal/completion.TestRunCommandLineEscapeModeproving the mode reaches the actual arg vector a cobra command receives — including a case pinning the current shell-modeC:\Windows\Temp→C:WindowsTempbehavior for contrast.go build,go vet,go test ./...all green.🤖 Generated with Claude Code