Garret,

while all of this is kind of intriguing on an academical level, it is
much more than you asked in your original question:

>>>> Now … what if I want to pass a single string "foo'bar" (note just
>>>> one single quote in the string) as a single string to my `main()`
>>>> method args array?

I think that I answered that both correctly and extensively: You do not
need to escape it, it just works. I also explained how to handle this
situation on the shell level, because your CLI samples all imply that
you must be starting at some kind of shell.

As for the simple tokeniser heuristics used in
CommandLineUtils.translateCommandline, you are correct: There is no such
thing as an escape character here. It is simply:

if ((state == inQuote) || (state == inDoubleQuote))
  throw new CommandLineException(
    "unbalanced quotes in " + toProcess
  );

But really, who would build an application that would need to handle
that on the CLI input level and put their users in quoting hell? That
would be bad application design. For that kind of complexity, usually
you would refer to a text file as input for complex texts.

Bottom line: You can make Maven Exec handle program arguments like
(unquoted, one line of text is one argument):

  It's OK
  She said: "It is OK"

But not:

  She said: "It's OK"

Regards
-- 
Alexander Kriegisch
https://scrum-master.de


Garret Wilson schrieb am 15.10.2023 19:49 (GMT +07:00):

> On 10/15/2023 9:24 AM, Garret Wilson wrote:
>> On 10/15/2023 1:31 AM, Alexander Kriegisch wrote:
>>> …
>>> Let us settle on only using double quotes to enclose arguments 
>>> containing spaces. Then, we do not need to escape single quotes and 
>>> can use them literally. But we do need to escape nested double quotes.
>>
>> …
>> Just for a moment, forget about Bash/PowerShell/CMD escaping rules. 
>> What are escaping the rules of the Maven Exec Plugin? What does it 
>> consider a delimiter for indicating multiple arguments?
> 
> I guess at this point it's easier just to dig into the source code. The 
> relevant plugin source code seems to be here (after a cursory glance):
> 
> https://github.com/mojohaus/exec-maven-plugin/blob/ddefecff84ebbf6427a6bb9eb6c2fdb332bfac7c/src/main/java/org/codehaus/mojo/exec/ExecMojo.java#L600
> 
> It appears to be using `CommandLineUtils.translateCommandline( argsProp 
> )` to parse the string input into an array. A little more searching 
> seems to indicate that that utility is here:
> 
> https://github.com/codehaus-plexus/plexus-utils/blob/d4f0d2de40c5d6cb5d80c8a199fa4f32a4e59879/src/main/java/org/codehaus/plexus/util/cli/CommandLineUtils.java#L348C85-L348C85
> 
> That code uses `StringTokenizer` constructed using `new 
> StringTokenizer(toProcess, "\"\' ", true)`. So yes, Alexander, it does 
> indeed appear to be using both single quotes and and double quotes as 
> delimiters.
> 
> So next is the question of escaping. The 
> [documentation](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/StringTokenizer.html)
> 
> for `StringTokenizer` says nothing about escaping. I glanced (extremely 
> quickly) over the source code for `StringTokenizer`, and I don't see it 
> translating any escape sequences.
> 
> Thus if escaping is happening, it would have to be in 
> `CommandLineUtils.translateCommandline( argsProp )`. And it would be 
> possible, as it uses the `StringTokenizer` option to return delimiters. 
> But glancing (again very quickly) over the `CommandLineUtils` code, I 
> don't see any escaping handling at all.
> 
> Thus my conclusion is that the Maven Exec Plugin handles either single 
> quotes or double quotes as the delimiter, but /does not handle escaping 
> in either case/. You pick one or the other, and cross your fingers that 
> the given argument does not contain that delimiter, because there's no 
> way to escape it ahead of time.
> 
> All the discussion of escaping at the Bash/PowerShell/CMD level, while 
> useful in its own context, has absolutely zero bearing on the issue I 
> was raising, which was: how to escape a quote at the Maven Exec Plugin 
> level. The answer seems to be: you can't.
> 
> (I glanced over the source code extremely fast, so I could have missed 
> something. If so please point it out to me!)
> 
> Garret
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@maven.apache.org
For additional commands, e-mail: users-h...@maven.apache.org

Reply via email to