Why does C # partially cancel command line arguments?

I try to pass multiple paths as arguments to a console application, but I get the error "Invalid characters in the path." It seems like something is mistakenly accepting the last two characters of the argument "C:\test\" for an escaped double quote.

For example, if I create a new empty console application in C #, for example:

 static void Main(string[] args) { Console.WriteLine(args[0]); Console.ReadLine(); } 

and, under "Project Properties" → "Debugging" I add a command line argument as follows:

Project properties showing the CLI argument:

then my conclusion is as follows:

Command line showing the result: C: \ test "

If the value is evaluated / not executed, why doesn't \t become a tab?

If the value is NOT evaluated / not executed, why \" becomes a double quote?

(Note. I know that I can get around this, for example, trimming backslashes, etc. I ask for help to understand why the arguments seem to be partially evaluated)

+5
source share
1 answer

Admittedly, this applies to Microsoft C command line arguments, but I have verified that these rules are also followed for C #. Command line arguments are split into a string args[] array before being passed to the application.

Parsing C Command Line Arguments

  • Arguments are limited to a space, which is either space or a tab.

  • A string surrounded by double quotes is interpreted as a single argument, regardless of the space contained within. A quoted string can be embedded in an argument. Note that the carriage (^) is not recognized as an escape character or delimiter.

  • The double quotation mark preceded by a backslash, \ "is interpreted as a literal double quote mark (").

  • Back oblique paths are interpreted literally unless they are preceded by a double quote.

  • If an even number of backslashes is followed by a double quotation mark, then one backslash () is placed in the argv array for each pair of backslashes (\), and the double quotation mark (") is interpreted as a line separator.

  • If an odd number of backslashes is followed by a double quotation mark, then one backslash () is placed in the argv array for each pair of backslashes (\), and the double quotation mark is interpreted as an escape sequence of the remaining backslash, resulting in argv places a literal double quotation mark (").

These rules seem to be consistent with the results you see.

I checked the following console application to verify these rules:

 static void Main(string[] args) { foreach (string s in args) { Console.WriteLine(s); } Console.ReadLine(); } 

With the following command line arguments:

 arg1 "arg2 arg3" arg4\" "arg5\"" arg6\\\" 

Output:

 arg1 arg2 arg3 arg4" arg5" arg6\" 

The reason your input argument is not displayed is because the first double quote is interpreted as a separator of the original string, and the second double quote is escaped by the previous backslash and interpreted as a literal double quote - not the end of the separator.

+5
source

Source: https://habr.com/ru/post/1266617/


All Articles