[英]in a console app what's the best way to prevent special sequences in command line arguments from being escaped?
I wrote this console app that reads a sequence of command line arguments and does something with them. 我编写了这个控制台应用程序,它读取一系列命令行参数并对其进行处理。
The problem is that if the user enters something like: 问题是,如果用户输入如下内容:
--folder "C:\my folder\" --username john
the args String array of the Main function will have 2 elements rather than 4: Main函数的args String数组将包含2个元素,而不是4个:
1st element: "--folder"
2nd element: "C:\my folder\" --username john"
(the \\" sequence is escaped as a double quote.) (\\“序列以双引号转义。)
Since not using quotes would result in 5 elements... 由于不使用引号将导致5个元素...
--folder C:\my folder\ --username john
1st element: --folder
2nd element: C:\my
3rd element: folder\
4th element: --username
5th element: john
... what's the best way to get around this problem? ...解决这个问题的最佳方法是什么?
This is a consequence of the weird command line parsing rules on Windows. 这是Windows上奇怪的命令行解析规则的结果。 See here http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx .
请参阅http://msdn.microsoft.com/zh-CN/library/windows/desktop/17w5ykft(v=vs.85).aspx 。 The command line should actually be:
--folder "C:\\my folder\\\\" --username john
命令行实际上应该是:--
--folder "C:\\my folder\\\\" --username john
This is a user input error. 这是用户输入错误。 You shouldn't try to correct it code;
您不应该尝试更正它的代码。 just fail gracefully.
只是优雅地失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.