当我从注册表或其他地方获取完整的命令行字符串时,例如:
mycommand -p -o c:\file\1.txt -r output
Run Code Online (Sandbox Code Playgroud)
或者
"c:\program files\dir\executable.exe" -options -o1 -o2
Run Code Online (Sandbox Code Playgroud)
如何在可执行文件和参数之间轻松分割?
谢谢
Windows 命令行解析相当奇怪,因为它是由 C 运行时库完成的(您可以检查 Visual Studio 安装目录中的代码,例如
当然,您计算机上的实际路径可能会有所不同。而且,如果您没有使用 Visual Studio 安装 C 运行时源,那么它就不是他们的。
我相信这样的“逻辑”是从DOS继承的,所以它相当粗糙。
基本语法是这样的:
命令行是由空格分隔的 1 个或多个单词组成的序列。
每个字都是以下 1 个或多个的序列:BARE_WORD、QUOTED_WORD 或 ESCAPE_SEQUENCE。单词以空格或命令行结尾结束。
BARE_WORD 是除反斜杠 ('\')、双引号 ('"') 或空格之外的 1 个或多个字符的序列。
QUOTED_WORD 由 LEAD_IN_QUOTE ('"') 引入,后跟零个或多个以下内容:
并以 LEAD_OUT_QUOTE ('"') 终止。导入和导出引号将从引用的单词中删除。
ESCAPE_SEQUENCE 是以下结构之一:
就是这样。
命令行上的第一个单词是命令名称(例如,可执行文件的名称/路径)。严格来说,解析命令名应该比其他单词更简单,因为它必须代表有效的 NTFS 文件名。然而,这不一定是真的,这取决于命令行的编写者。
下面是一些示例 C# 代码,它应该以与 Windows 操作系统相同的方式解析任何给定的命令行,但我应该注意这尚未经过彻底测试。
该方法Parse()返回一个IEnumerable<string>,其中第一个元素是命令/程序名称,其余元素是构成参数的单词。
class CommandLineParser
{
char[] cmd; // source buffer
StringBuilder buf; // output buffer
int i; // current position within the source buffer
public CommandLineParser()
{
cmd = null;
buf = null;
i = -1;
return;
}
public IEnumerable<string> Parse( string commandLine )
{
cmd = commandLine.ToCharArray();
buf = new StringBuilder();
i = 0;
while ( i < cmd.Length )
{
char ch = cmd[i];
if ( char.IsWhiteSpace( ch ) ) { throw new InvalidOperationException(); }
else if ( ch == '\\' ) { ParseEscapeSequence(); }
else if ( ch == '"' ) { ParseQuotedWord(); }
else { ParseBareWord(); }
if ( i >= cmd.Length || char.IsWhiteSpace( cmd[i] ) )
{
string arg = buf.ToString();
yield return arg;
buf.Length = 0;
ConsumeWhitespace();
}
}
}
/// <summary>
/// Parse a quoted word
/// </summary>
private void ParseQuotedWord()
{
// scan over the lead-in quotation mark w/o adding it to the buffer
++i;
// scan the contents of the quoted word into the buffer
while ( i < cmd.Length && cmd[i] != '"' )
{
char ch = cmd[i];
if ( ch == '\\' ) { ParseEscapeSequence(); }
else { buf.Append( ch ); ++i; }
}
// scan over the lead-out quotation mark w/o adding it to the buffer
if ( i < cmd.Length )
{
++i;
}
return;
}
/// <summary>
/// Parse a bareword
/// </summary>
private void ParseBareWord()
{
while ( i < cmd.Length )
{
char ch = cmd[i];
if ( char.IsWhiteSpace( ch ) ) break; // whitespace terminates a bareword
else if ( ch == '"' ) break; // lead-in quote starts a quoted word
else if ( ch == '\\' ) break; // escape sequence terminates the bareword
buf.Append(ch); // otherwise, keep reading this word
++i;
}
return;
}
/// <summary>
/// Parse an escape sequence of one or more backslashes followed an an optional trailing quotation mark
/// </summary>
private void ParseEscapeSequence()
{
//---------------------------------------------------------------------------------------------------------
// The rule is that:
//
// * An even number of backslashes followed by a quotation mark ('"') means that
// - the backslashes are escaped, so half that many get injected into the buffer, and
// - the quotation mark is a lead-in/lead-out quotation mark that marks the start of a quoted word
// which does not get added to the buffer.
//
// * An odd number of backslashes followed by a quotation mark ('"') means that
// - the backslashes are escaped, so half that many get injected into the buffer, and
// - the quotation mark is escaped. It's a literal quotation mark that also gets injected into the buffer
//
// * Any number of backslashes that aren't followed by a quotation mark ('"') have no special meaning:
// all of them get added to the buffer as-sis.
//
//---------------------------------------------------------------------------------------------------------
//
// scan in the backslashes
//
int p = i; // start of the escape sequence
while ( i < cmd.Length && cmd[i] == '\\' )
{
buf.Append( '\\' );
++i;
}
//
// if the backslash sequence is followed by a quotation mark, it's an escape sequence
//
if ( i < cmd.Length && cmd[i] == '"' )
{
int n = ( i - p ); // find the number of backslashes seen
int quotient = n >> 1; // n divide 2 ( 5 div 2 = 2 , 6 div 2 = 3 )
int remainder = n & 1; // n modulo 2 ( 5 mod 2 = 1 , 6 mod 2 = 0 )
buf.Length -= ( quotient + remainder ); // remove the unwanted backslashes
if ( remainder != 0 )
{
// the trailing quotation mark is an escaped, literal quotation mark
// add it to the buffer and increment the pointer
buf.Append( '"' );
++i;
}
}
return;
}
/// <summary>
/// Consume inter-argument whitespace
/// </summary>
private void ConsumeWhitespace()
{
while ( i < cmd.Length && char.IsWhiteSpace( cmd[i] ) )
{
++i;
}
return;
}
}
class Program
{
static void Main()
{
CommandLineParser parser = new CommandLineParser();
string commandLine = RetrieveUnparsedCommandLine();
int i = 0;
IEnumerable<string> args = parser.Parse( commandLine );
Console.WriteLine( "-------------------" );
foreach ( string arg in args )
{
string template = i > 0 ? "argv[0:#0]" : "command";
string label = string.Format( template , i++ );
Console.WriteLine( "{0}: {1}" , label , arg );
}
Console.WriteLine( "-------------------------" );
return;
}
static string RetrieveUnparsedCommandLine()
{
// get the raw command line. Source might be registry, config file, whatever
string commandLine = Environment.CommandLine;
return commandLine;
}
}
Run Code Online (Sandbox Code Playgroud)
祝你好运。
| 归档时间: |
|
| 查看次数: |
1521 次 |
| 最近记录: |