引用路径和避免转义字符的C#命令行解析 [英] C# Command-Line Parsing of Quoted Paths and Avoiding Escape Characters

查看:390
本文介绍了引用路径和避免转义字符的C#命令行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这怎么可能解析命令行参数是作为PTED路径间$ P $? ARGS []包含如果他们引述会自动加入字符串,例如:

How is it possible to parse command-line arguments that are to be interpreted as paths? args[] contains strings that are automatically joined if they are quoted, e.g.:

example.exe一二三四

example.exe one two "three four"

args[0] = one
args[1] = two
args[2] = three four

不过,ARGS []将无法解析财产C:\\实例\\作为参数。相反,它会提供参数为C:\\实例(与包括额外的引号)这是由于在路径中的反斜杠被视为转义字符,因此用户在命令中提供的最终报价直插成为争论的一部分。

However, args[] will not property parse "C:\Example\" as an argument. Rather, it will supply the argument as "C:\Example"" (with the extra quote included.) This is due to the backslash in the path being treated as an escape character and thus the end quotation that the user supplied on the command-line becomes part of the argument.

.e.g:

example.exe一C:\\ InputFolderC:\\ OutuptFolder \\

example.exe one "C:\InputFolder" "C:\OutuptFolder\"

args[0] = one
args[1] = C:\InputFolder"
args[2] = C:\OutputFolder"

这是简单的组装机可能是:

An easy kludge might be:

_path = args[i].Replace("\"", @"\");

不过,我敢肯定有一个最佳实践这一点。一个人怎么可能正确地分析inlcudes路径的命令行,preventing不适当地被填充ARGS []数组刺已经分析为转义字符?

However, I'm sure there is a best-practice for this. How might one correctly parse a command line that inlcudes paths, preventing the args[] array from improperly being populated with stings that have been parsed for escape characters?

请注意:我不希望在自己的项目一个完整的命令行解析库!我只需要处理引用路径,并希望在手动的方式这样做。请不要reccomend NConsoler,单声道,或其他任何大型的厨房水槽命令行解析库。

NOTE: I would not like to include an entire command-line parsing library in my project! I need only to handle quoted paths and wish to do so in a "manual" fashion. Please do not reccomend NConsoler, Mono, or any other large "kitchen sink" command-line parsing library.

另外请注意:据我所知,这不是重复的问题。虽然其他的问题集中在通用的命令行解析,这个问题是特定于该路径时介绍其中部分PTED为转义序列间$ P $的问题。

ALSO NOTE: As far as I can tell, this is not a duplicate question. While other questions focus on generic command-line parsing, this question is specific to the problem that paths introduce when parts of them are interpreted as escape sequences.

推荐答案

不是一个答案,但这里的一些的背景和解释从杰弗里谭,微软在线社区支持(2006年12月7日):

Not an answer, but here's some background and explanation from Jeffrey Tan, Microsoft Online Community Support (12/7/2006):

请注意:这不是没有code失利
  但是经过了设计,因为backslashe是
  通常用来逃避某些
  特殊字符。此外,此
  算法是一样的Win32命令
  行参数解析功能
  CommandLineToArgvW。见备注
  以下部分:
   http://msdn2.microsoft.com/en-us/library/bb776391.aspx

Note: this is not not a code defeat but by design, since backslashe are normally used to escape certain special character. Also, this algorithm is the same as Win32 command line arguments parsing function CommandLineToArgvW. See the Remarks section below: http://msdn2.microsoft.com/en-us/library/bb776391.aspx

也提到了FX方法 Environment.GetCommandLineArgs 为斜线处理行为的进一步的解释。

Also makes reference to the FX method Environment.GetCommandLineArgs for further explanation of the slash handling behavior.

我个人认为这是一个拖了,我很惊讶,我没有用之前一直位。或者,也许我不知道吗?盲置换斜线报价不打我作为一个解决方案,但。我投票的问题了,因为它是一个大开眼界。

Personally I think this is a drag, and I'm surprised I haven't been bit by it before. Or maybe I have and don't know it? Blind replacement of quotes with slashes doesn't strike me as a solution, though. I'm voting the question up, because it was an eye opener.

这篇关于引用路径和避免转义字符的C#命令行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆