当我从注册表或其他地方获取完整的命令行字符串时,例如:
mycommand -p -o c:\file\1.txt -r output
或
"c:\program files\dir\executable.exe" -options -o1 -o2
如何在可执行文件和参数之间轻松拆分?
由于
答案 0 :(得分:3)
Windows命令行解析相当不稳定,因为它是由C运行时库完成的(您可以检查Visual Studio安装目录中的代码,如
当然,您机器上的实际路径可能会有所不同。并且,如果您没有使用Visual Studio安装C运行时源,则不会是它们。
我相信它的“逻辑”是从DOS继承的,所以它相当苛刻。
基本语法是这样的:
命令行是由空格分隔的1个或多个WORDS的序列。
每个Word都是以下一个或多个的序列:BARE_WORD,QUOTED_WORD或ESCAPE_SEQUENCE。单词由空格或命令行的结尾终止。
BARE_WORD是除反斜杠('\'),双引号('“')或空格以外的1个或多个字符的序列。
QUADED_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;
}
}
祝你好运。
答案 1 :(得分:1)
给定一个包含大概有效命令行条目的字符串:
static string[] SplitArgs(string input)
{
var args = new List<string>();
var parts = input.Split(' ');
for (int ii = 0; ii < parts.Length; ++ii)
{
// if it starts with a quote, search to the end
// NB: this does not handle the case of --x="hello world"
// an arguments post processor is required in that case
if (parts[ii].StartsWith("\""))
{
var builder = new StringBuilder(parts[ii].Substring(0));
while (ii + 1 < parts.Length
&& !parts[++ii].EndsWith("\""))
{
builder.Append(' ');
}
// if we made it here before the end of the string
// it is the end of a quoted argument
if (ii < parts.Length)
builder.Append(parts[ii].Substring(0, parts[ii].Length - 1));
args.Add(builder.ToString());
}
else
args.Add(part[ii]);
}
return args.ToArray();
}