fix: preserve whitespace in positional arguments - #2423
Conversation
|
@lenamonj can you give the usage example of the real CLI interface that you are trying to fix? Shell doesn't allow me to enter tab into CLI, needless to say it won't split it. |
|
@abitrolly It works by quoting the input I personally use this for curl, for example when providing some ad-hoc JSON |
|
Sure - quoting is how the whitespace gets in, as @avorima says. The case Before this change the action receives "notes.txt" with the space stripped, so the open fails with a confusing "no such file" for a file that plainly exists. Same shape for any argument where whitespace is content rather than separator - a search pattern of two spaces, or a message string built by a The parser is right to trim when classifying the token (deciding flag vs |
|
Anything else needed from my side to land this? Happy to rebase if that helps. |
|
@lenamonj thanks for the use case. It looks like it may cause minor break for users who didn't expect to strip spaces manually. I wish I had a counter example, but I don't. You say parser detect flags inside quoted strings. I wonder what is the current beaviour for these (sorry, no coding environment ATM). mytool cat " --notes.txt"
mytool cat "--notes.txt"
mytool cat "--file notes.txt"
mytool cat " --file notes.txt"And what is the expected and what would be after the patch.. |
|
Ran all four against main (1a4deb4) and this branch (93595d0), with a
Flag detection is unchanged: the parser still trims the token before deciding whether it is a flag, so a quoted string that looks like a flag is treated as one both before and after. The only difference is the last row, where a positional argument now reaches the action as typed instead of trimmed. On the break risk: anyone relying on the trim was relying on undocumented behaviour, and the fix is |
parseFlagscomputesfirstArg := strings.TrimSpace(rargs[0])to classify each argument, which is correct - but the positional branch then stores the trimmed copy instead of the original argument, so any positional carrying deliberate whitespace is silently rewritten:A filename with a leading space, a message string, a pattern argument - all reach the action modified, with no error. The empty-string branch a few lines above already appends
rargs[0]untrimmed, so this change makes the non-empty branch consistent with its sibling: classify with the trimmed copy, append the original.One line changed in
command_parse.go, plus a regression test.Note: the lone-
-branch nearby has the same trimmed append, but that line is already being rewritten by #2419 (the bare-dash fix), so this PR deliberately does not touch it.