可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a single string that contains the command-line parameters to be passed to another executable and I need to extract the string[] containing the individual parameters in the same way that C# would if the commands had been specified on the command-line. The string[] will be used when executing another assemblies entry-point via reflection.
Is there a standard function for this? Or is there a preferred method (regex?) for splitting the parameters correctly? It must handle \'\"\' delimited strings that may contain spaces correctly, so I can\'t just split on \' \'.
Example string:
string parameterString = @\"/src:\"\"C:\\tmp\\Some Folder\\Sub Folder\"\" /users:\"\"abcdefg@hijkl.com\"\" tasks:\"\"SomeTask,Some Other Task\"\" -someParam foo\";
Example result:
string[] parameterArray = new string[] {
@\"/src:C:\\tmp\\Some Folder\\Sub Folder\",
@\"/users:abcdefg@hijkl.com\",
@\"tasks:SomeTask,Some Other Task\",
@\"-someParam\",
@\"foo\"
};
I do not need a command-line parsing library, just a way to get the String[] that should be generated.
Update: I had to change the expected result to match what is actually generated by C# (removed the extra \"\'s in the split strings)
回答1:
In addition to the good and pure managed solution by Earwicker, it may be worth mentioning, for sake of completeness, that Windows also provides the CommandLineToArgvW
function for breaking up a string into an array of strings:
LPWSTR *CommandLineToArgvW(
LPCWSTR lpCmdLine, int *pNumArgs);
Parses a Unicode command line string
and returns an array of pointers to
the command line arguments, along with
a count of such arguments, in a way
that is similar to the standard C
run-time argv and argc values.
An example of calling this API from C# and unpacking the resulting string array in managed code can be found at, “Converting Command Line String to Args[] using CommandLineToArgvW() API.” Below is a slightly simpler version of the same code:
[DllImport(\"shell32.dll\", SetLastError = true)]
static extern IntPtr CommandLineToArgvW(
[MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
public static string[] CommandLineToArgs(string commandLine)
{
int argc;
var argv = CommandLineToArgvW(commandLine, out argc);
if (argv == IntPtr.Zero)
throw new System.ComponentModel.Win32Exception();
try
{
var args = new string[argc];
for (var i = 0; i < args.Length; i++)
{
var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
args[i] = Marshal.PtrToStringUni(p);
}
return args;
}
finally
{
Marshal.FreeHGlobal(argv);
}
}
回答2:
It annoys me that there\'s no function to split a string based on a function that examines each character. If there was, you could write it like this:
public static IEnumerable<string> SplitCommandLine(string commandLine)
{
bool inQuotes = false;
return commandLine.Split(c =>
{
if (c == \'\\\"\')
inQuotes = !inQuotes;
return !inQuotes && c == \' \';
})
.Select(arg => arg.Trim().TrimMatchingQuotes(\'\\\"\'))
.Where(arg => !string.IsNullOrEmpty(arg));
}
Although having written that, why not write the necessary extension methods. Okay, you talked me into it...
Firstly, my own version of Split that takes a function that has to decide whether the specified character should split the string:
public static IEnumerable<string> Split(this string str,
Func<char, bool> controller)
{
int nextPiece = 0;
for (int c = 0; c < str.Length; c++)
{
if (controller(str[c]))
{
yield return str.Substring(nextPiece, c - nextPiece);
nextPiece = c + 1;
}
}
yield return str.Substring(nextPiece);
}
It may yield some empty strings depending on the situation, but maybe that information will be useful in other cases, so I don\'t remove the empty entries in this function.
Secondly (and more mundanely) a little helper that will trim a matching pair of quotes from the start and end of a string. It\'s more fussy than the standard Trim method - it will only trim one character from each end, and it will not trim from just one end:
public static string TrimMatchingQuotes(this string input, char quote)
{
if ((input.Length >= 2) &&
(input[0] == quote) && (input[input.Length - 1] == quote))
return input.Substring(1, input.Length - 2);
return input;
}
And I suppose you\'ll want some tests as well. Well, alright then. But this must be absolutely the last thing! First a helper function that compares the result of the split with the expected array contents:
public static void Test(string cmdLine, params string[] args)
{
string[] split = SplitCommandLine(cmdLine).ToArray();
Debug.Assert(split.Length == args.Length);
for (int n = 0; n < split.Length; n++)
Debug.Assert(split[n] == args[n]);
}
Then I can write tests like this:
Test(\"\");
Test(\"a\", \"a\");
Test(\" abc \", \"abc\");
Test(\"a b \", \"a\", \"b\");
Test(\"a b \\\"c d\\\"\", \"a\", \"b\", \"c d\");
Here\'s the test for your requirements:
Test(@\"/src:\"\"C:\\tmp\\Some Folder\\Sub Folder\"\" /users:\"\"abcdefg@hijkl.com\"\" tasks:\"\"SomeTask,Some Other Task\"\" -someParam\",
@\"/src:\"\"C:\\tmp\\Some Folder\\Sub Folder\"\"\", @\"/users:\"\"abcdefg@hijkl.com\"\"\", @\"tasks:\"\"SomeTask,Some Other Task\"\"\", @\"-someParam\");
Note that the implementation has the extra feature that it will remove quotes around an argument if that makes sense (thanks to the TrimMatchingQuotes function). I believe that\'s part of the normal command-line interpretation.
回答3:
The Windows command-line parser behaves just as you say, split on space unless there\'s a unclosed quote before it. I would recommend writing the parser yourself. Something like this maybe:
static string[] ParseArguments(string commandLine)
{
char[] parmChars = commandLine.ToCharArray();
bool inQuote = false;
for (int index = 0; index < parmChars.Length; index++)
{
if (parmChars[index] == \'\"\')
inQuote = !inQuote;
if (!inQuote && parmChars[index] == \' \')
parmChars[index] = \'\\n\';
}
return (new string(parmChars)).Split(\'\\n\');
}
回答4:
I took the answer from Jeffrey L Whitledge and enhanced it a little.
It now supports both single and double quotes. You can use quotes in the parameters itself by using other typed quotes.
It also strips the quotes from the arguments since these do not contribute to the argument information.
public static string[] SplitArguments(string commandLine)
{
var parmChars = commandLine.ToCharArray();
var inSingleQuote = false;
var inDoubleQuote = false;
for (var index = 0; index < parmChars.Length; index++)
{
if (parmChars[index] == \'\"\' && !inSingleQuote)
{
inDoubleQuote = !inDoubleQuote;
parmChars[index] = \'\\n\';
}
if (parmChars[index] == \'\\\'\' && !inDoubleQuote)
{
inSingleQuote = !inSingleQuote;
parmChars[index] = \'\\n\';
}
if (!inSingleQuote && !inDoubleQuote && parmChars[index] == \' \')
parmChars[index] = \'\\n\';
}
return (new string(parmChars)).Split(new[] { \'\\n\' }, StringSplitOptions.RemoveEmptyEntries);
}
回答5:
The good and pure managed solution by Earwicker failed to handle arguments like this:
Test(\"\\\"He whispered to her \\\\\\\"I love you\\\\\\\".\\\"\", \"He whispered to her \\\"I love you\\\".\");
It returned 3 elements:
\"He whispered to her \\\"I
love
you\\\".\"
So here is a fix to support the \"quoted \\\"escape\\\" quote\":
public static IEnumerable<string> SplitCommandLine(string commandLine)
{
bool inQuotes = false;
bool isEscaping = false;
return commandLine.Split(c => {
if (c == \'\\\\\' && !isEscaping) { isEscaping = true; return false; }
if (c == \'\\\"\' && !isEscaping)
inQuotes = !inQuotes;
isEscaping = false;
return !inQuotes && Char.IsWhiteSpace(c)/*c == \' \'*/;
})
.Select(arg => arg.Trim().TrimMatchingQuotes(\'\\\"\').Replace(\"\\\\\\\"\", \"\\\"\"))
.Where(arg => !string.IsNullOrEmpty(arg));
}
Tested with 2 additional cases:
Test(\"\\\"C:\\\\Program Files\\\"\", \"C:\\\\Program Files\");
Test(\"\\\"He whispered to her \\\\\\\"I love you\\\\\\\".\\\"\", \"He whispered to her \\\"I love you\\\".\");
Also noted that the accepted answer by Atif Aziz which uses CommandLineToArgvW also failed. It returned 4 elements:
He whispered to her \\
I
love
you\".
Hope this helps someone looking for such a solution in the future.
回答6:
Environment.GetCommandLineArgs()
回答7:
I like iterators, and nowadays LINQ makes IEnumerable<String>
as easily usable as arrays of string, so my take following the spirit of Jeffrey L Whitledge\'s answer is (as a extension method to string
):
public static IEnumerable<string> ParseArguments(this string commandLine)
{
if (string.IsNullOrWhiteSpace(commandLine))
yield break;
var sb = new StringBuilder();
bool inQuote = false;
foreach (char c in commandLine) {
if (c == \'\"\' && !inQuote) {
inQuote = true;
continue;
}
if (c != \'\"\' && !(char.IsWhiteSpace(c) && !inQuote)) {
sb.Append(c);
continue;
}
if (sb.Length > 0) {
var result = sb.ToString();
sb.Clear();
inQuote = false;
yield return result;
}
}
if (sb.Length > 0)
yield return sb.ToString();
}
回答8:
This The Code Project article is what I\'ve used in the past. It\'s a good bit of code, but it might work.
This MSDN article is the only thing I could find that explains how C# parses command line arguments.
回答9:
In your question you asked for a regex, and I am a big fan and user of them, so when I needed to do this same argument split as you, I wrote my own regex after googling around and not finding a simple solution. I like short solutions, so I made one and here it is:
var re = @\"\\G(\"\"((\"\"\"\"|[^\"\"])+)\"\"|(\\S+)) *\";
var ms = Regex.Matches(CmdLine, re);
var list = ms.Cast<Match>()
.Select(m => Regex.Replace(
m.Groups[2].Success
? m.Groups[2].Value
: m.Groups[4].Value, @\"\"\"\"\"\", @\"\"\"\")).ToArray();
It handles blanks and quotes inside quotation marks, and converts enclosed \"\" to \". Feel free to use the code!
回答10:
A purely managed solution might be helpful. There are too many \"problem\" comments for the WINAPI function and it\'s not available on other platforms. Here\'s my code that has a well-defined behaviour (that you can change if you like).
It should do the same as what .NET/Windows do when providing that string[] args
parameter, and I\'ve compared it with a number of \"interesting\" values.
This is a classic state-machine implementation that takes each single character from the input string and interprets it for the current state, producing output and a new state. The state is defined in the variables escape
, inQuote
, hadQuote
and prevCh
, and the output is collected in currentArg
and args
.
Some of the specialties that I\'ve discovered by experiments on a real command prompt (Windows 7): \\\\
produces \\
, \\\"
produces \"
, \"\"
within a quoted range produces \"
.
The ^
character seems to be magical, too: it always disappears when not doubling it. Otherwise it has no effect on a real command line. My implementation does not support this, as I haven\'t found a pattern in this behaviour. Maybe somebody knows more about it.
Something that doesn\'t fit in this pattern is the following command:
cmd /c \"argdump.exe \"a b c\"\"
The cmd
command seems to catch the outer quotes and take the rest verbatim. There must be some special magic sauce in this.
I\'ve done no benchmarks on my method, but consider it reasonably fast. It doesn\'t use Regex
and doesn\'t do any string concatenation but instead uses a StringBuilder
to collect the characters for an argument and puts them in a list.
/// <summary>
/// Reads command line arguments from a single string.
/// </summary>
/// <param name=\"argsString\">The string that contains the entire command line.</param>
/// <returns>An array of the parsed arguments.</returns>
public string[] ReadArgs(string argsString)
{
// Collects the split argument strings
List<string> args = new List<string>();
// Builds the current argument
var currentArg = new StringBuilder();
// Indicates whether the last character was a backslash escape character
bool escape = false;
// Indicates whether we\'re in a quoted range
bool inQuote = false;
// Indicates whether there were quotes in the current arguments
bool hadQuote = false;
// Remembers the previous character
char prevCh = \'\\0\';
// Iterate all characters from the input string
for (int i = 0; i < argsString.Length; i++)
{
char ch = argsString[i];
if (ch == \'\\\\\' && !escape)
{
// Beginning of a backslash-escape sequence
escape = true;
}
else if (ch == \'\\\\\' && escape)
{
// Double backslash, keep one
currentArg.Append(ch);
escape = false;
}
else if (ch == \'\"\' && !escape)
{
// Toggle quoted range
inQuote = !inQuote;
hadQuote = true;
if (inQuote && prevCh == \'\"\')
{
// Doubled quote within a quoted range is like escaping
currentArg.Append(ch);
}
}
else if (ch == \'\"\' && escape)
{
// Backslash-escaped quote, keep it
currentArg.Append(ch);
escape = false;
}
else if (char.IsWhiteSpace(ch) && !inQuote)
{
if (escape)
{
// Add pending escape char
currentArg.Append(\'\\\\\');
escape = false;
}
// Accept empty arguments only if they are quoted
if (currentArg.Length > 0 || hadQuote)
{
args.Add(currentArg.ToString());
}
// Reset for next argument
currentArg.Clear();
hadQuote = false;
}
else
{
if (escape)
{
// Add pending escape char
currentArg.Append(\'\\\\\');
escape = false;
}
// Copy character from input, no special meaning
currentArg.Append(ch);
}
prevCh = ch;
}
// Save last argument
if (currentArg.Length > 0 || hadQuote)
{
args.Add(currentArg.ToString());
}
return args.ToArray();
}
回答11:
Use:
public static string[] SplitArguments(string args) {
char[] parmChars = args.ToCharArray();
bool inSingleQuote = false;
bool inDoubleQuote = false;
bool escaped = false;
bool lastSplitted = false;
bool justSplitted = false;
bool lastQuoted = false;
bool justQuoted = false;
int i, j;
for(i=0, j=0; i<parmChars.Length; i++, j++) {
parmChars[j] = parmChars[i];
if(!escaped) {
if(parmChars[i] == \'^\') {
escaped = true;
j--;
} else if(parmChars[i] == \'\"\' && !inSingleQuote) {
inDoubleQuote = !inDoubleQuote;
parmChars[j] = \'\\n\';
justSplitted = true;
justQuoted = true;
} else if(parmChars[i] == \'\\\'\' && !inDoubleQuote) {
inSingleQuote = !inSingleQuote;
parmChars[j] = \'\\n\';
justSplitted = true;
justQuoted = true;
} else if(!inSingleQuote && !inDoubleQuote && parmChars[i] == \' \') {
parmChars[j] = \'\\n\';
justSplitted = true;
}
if(justSplitted && lastSplitted && (!lastQuoted || !justQuoted))
j--;
lastSplitted = justSplitted;
justSplitted = false;
lastQuoted = justQuoted;
justQuoted = false;
} else {
escaped = false;
}
}
if(lastQuoted)
j--;
return (new string(parmChars, 0, j)).Split(new[] { \'\\n\' });
}
Based on Vapour in the Alley\'s answer, this one also supports ^ escapes.
Examples:
- this is a test
- this \"is a\" test
- this ^\"is a^\" test
- this \"\" \"is a ^^ test\"
It also supports multiple spaces (breaks arguments just one time per block of spaces).
回答12:
Currently, this is the code that I have:
private String[] SplitCommandLineArgument(String argumentString)
{
StringBuilder translatedArguments = new StringBuilder(argumentString);
bool escaped = false;
for (int i = 0; i < translatedArguments.Length; i++)
{
if (translatedArguments[i] == \'\"\')
{
escaped = !escaped;
}
if (translatedArguments[i] == \' \' && !escaped)
{
translatedArguments[i] = \'\\n\';
}
}
string[] toReturn = translatedArguments.ToString().Split(new char[] { \'\\n\' }, StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < toReturn.Length; i++)
{
toReturn[i] = RemoveMatchingQuotes(toReturn[i]);
}
return toReturn;
}
public static string RemoveMatchingQuotes(string stringToTrim)
{
int firstQuoteIndex = stringToTrim.IndexOf(\'\"\');
int lastQuoteIndex = stringToTrim.LastIndexOf(\'\"\');
while (firstQuoteIndex != lastQuoteIndex)
{
stringToTrim = stringToTrim.Remove(firstQuoteIndex, 1);
stringToTrim = stringToTrim.Remove(lastQuoteIndex - 1, 1); //-1 because we\'ve shifted the indicies left by one
firstQuoteIndex = stringToTrim.IndexOf(\'\"\');
lastQuoteIndex = stringToTrim.LastIndexOf(\'\"\');
}
return stringToTrim;
}
It doesn\'t work with escaped quotes, but it works for the cases that I\'ve come up against so far.
回答13:
This is a reply to Anton\'s code, which do not work with escaped quotes. I modified 3 places.
- The constructor for StringBuilder in SplitCommandLineArguments, replacing any \\\" with \\r
- In the for-loop in SplitCommandLineArguments, I now replace the \\r character back to \\\".
- Changed the SplitCommandLineArgument method from private to public static.
public static string[] SplitCommandLineArgument( String argumentString )
{
StringBuilder translatedArguments = new StringBuilder( argumentString ).Replace( \"\\\\\\\"\", \"\\r\" );
bool InsideQuote = false;
for ( int i = 0; i < translatedArguments.Length; i++ )
{
if ( translatedArguments[i] == \'\"\' )
{
InsideQuote = !InsideQuote;
}
if ( translatedArguments[i] == \' \' && !InsideQuote )
{
translatedArguments[i] = \'\\n\';
}
}
string[] toReturn = translatedArguments.ToString().Split( new char[] { \'\\n\' }, StringSplitOptions.RemoveEmptyEntries );
for ( int i = 0; i < toReturn.Length; i++ )
{
toReturn[i] = RemoveMatchingQuotes( toReturn[i] );
toReturn[i] = toReturn[i].Replace( \"\\r\", \"\\\"\" );
}
return toReturn;
}
public static string RemoveMatchingQuotes( string stringToTrim )
{
int firstQuoteIndex = stringToTrim.IndexOf( \'\"\' );
int lastQuoteIndex = stringToTrim.LastIndexOf( \'\"\' );
while ( firstQuoteIndex != lastQuoteIndex )
{
stringToTrim = stringToTrim.Remove( firstQuoteIndex, 1 );
stringToTrim = stringToTrim.Remove( lastQuoteIndex - 1, 1 ); //-1 because we\'ve shifted the indicies left by one
firstQuoteIndex = stringToTrim.IndexOf( \'\"\' );
lastQuoteIndex = stringToTrim.LastIndexOf( \'\"\' );
}
return stringToTrim;
}
回答14:
I don\'t think there are single quotes or ^ quotes for C# applications.
The following function is working fine for me:
public static IEnumerable<String> SplitArguments(string commandLine)
{
Char quoteChar = \'\"\';
Char escapeChar = \'\\\\\';
Boolean insideQuote = false;
Boolean insideEscape = false;
StringBuilder currentArg = new StringBuilder();
// needed to keep \"\" as argument but drop whitespaces between arguments
Int32 currentArgCharCount = 0;
for (Int32 i = 0; i < commandLine.Length; i++)
{
Char c = commandLine[i];
if (c == quoteChar)
{
currentArgCharCount++;
if (insideEscape)
{
currentArg.Append(c); // found \\\" -> add \" to arg
insideEscape = false;
}
else if (insideQuote)
{
insideQuote = false; // quote ended
}
else
{
insideQuote = true; // quote started
}
}
else if (c == escapeChar)
{
currentArgCharCount++;
if (insideEscape) // found \\\\ -> add \\\\ (only \\\" will be \")
currentArg.Append(escapeChar + escapeChar);
insideEscape = !insideEscape;
}
else if (Char.IsWhiteSpace(c))
{
if (insideQuote)
{
currentArgCharCount++;
currentArg.Append(c); // append whitespace inside quote
}
else
{
if (currentArgCharCount > 0)
yield return currentArg.ToString();
currentArgCharCount = 0;
currentArg.Clear();
}
}
else
{
currentArgCharCount++;
if (insideEscape)
{
// found non-escaping backslash -> add \\ (only \\\" will be \")
currentArg.Append(escapeChar);
currentArgCharCount = 0;
insideEscape = false;
}
currentArg.Append(c);
}
}
if (currentArgCharCount > 0)
yield return currentArg.ToString();
}
回答15:
You can have a look at the code I\'ve posted yesterday:
[C#] Path & arguments strings
It splits a filename + arguments into string[]. Short paths, environment variables, and missing file extensions are handled.
(Initially it was for UninstallString in Registry.)
回答16:
Try this code:
string[] str_para_linha_comando(string str, out int argumentos)
{
string[] linhaComando = new string[32];
bool entre_aspas = false;
int posicao_ponteiro = 0;
int argc = 0;
int inicio = 0;
int fim = 0;
string sub;
for(int i = 0; i < str.Length;)
{
if (entre_aspas)
{
// Está entre aspas
sub = str.Substring(inicio+1, fim - (inicio+1));
linhaComando[argc - 1] = sub;
posicao_ponteiro += ((fim - posicao_ponteiro)+1);
entre_aspas = false;
i = posicao_ponteiro;
}
else
{
tratar_aspas:
if (str.ElementAt(i) == \'\\\"\')
{
inicio = i;
fim = str.IndexOf(\'\\\"\', inicio + 1);
entre_aspas = true;
argc++;
}
else
{
// Se não for aspas, então ler até achar o primeiro espaço em branco
if (str.ElementAt(i) == \' \')
{
if (str.ElementAt(i + 1) == \'\\\"\')
{
i++;
goto tratar_aspas;
}
// Pular os espaços em branco adiconais
while(str.ElementAt(i) == \' \') i++;
argc++;
inicio = i;
fim = str.IndexOf(\' \', inicio);
if (fim == -1) fim = str.Length;
sub = str.Substring(inicio, fim - inicio);
linhaComando[argc - 1] = sub;
posicao_ponteiro += (fim - posicao_ponteiro);
i = posicao_ponteiro;
if (posicao_ponteiro == str.Length) break;
}
else
{
argc++;
inicio = i;
fim = str.IndexOf(\' \', inicio);
if (fim == -1) fim = str.Length;
sub = str.Substring(inicio, fim - inicio);
linhaComando[argc - 1] = sub;
posicao_ponteiro += fim - posicao_ponteiro;
i = posicao_ponteiro;
if (posicao_ponteiro == str.Length) break;
}
}
}
}
argumentos = argc;
return linhaComando;
}
It\'s written in Portuguese.
回答17:
Here\'s a one liner that gets the job done (see the one line that does all of the work inside the BurstCmdLineArgs(...) method).
Not what I\'d call the most readable line of code, but you can break it out for readability\'s sake. It\'s simple on purpose and does not work well for all argument cases (like file name arguments that contain the split string character delimiter in them).
This solution has worked well in my solutions that use it. Like I said, it gets the job done without a rat\'s nest of code to handle every possible argument format n-factorial.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CmdArgProcessor
{
class Program
{
static void Main(string[] args)
{
// test switches and switches with values
// -test1 1 -test2 2 -test3 -test4 -test5 5
string dummyString = string.Empty;
var argDict = BurstCmdLineArgs(args);
Console.WriteLine(\"Value for switch = -test1: {0}\", argDict[\"test1\"]);
Console.WriteLine(\"Value for switch = -test2: {0}\", argDict[\"test2\"]);
Console.WriteLine(\"Switch -test3 is present? {0}\", argDict.TryGetValue(\"test3\", out dummyString));
Console.WriteLine(\"Switch -test4 is present? {0}\", argDict.TryGetValue(\"test4\", out dummyString));
Console.WriteLine(\"Value for switch = -test5: {0}\", argDict[\"test5\"]);
// Console output:
//
// Value for switch = -test1: 1
// Value for switch = -test2: 2
// Switch -test3 is present? True
// Switch -test4 is present? True
// Value for switch = -test5: 5
}
public static Dictionary<string, string> BurstCmdLineArgs(string[] args)
{
var argDict = new Dictionary<string, string>();
// Flatten the args in to a single string separated by a space.
// Then split the args on the dash delimiter of a cmd line \"switch\".
// E.g. -mySwitch myValue
// or -JustMySwitch (no value)
// where: all values must follow a switch.
// Then loop through each string returned by the split operation.
// If the string can be split again by a space character,
// then the second string is a value to be paired with a switch,
// otherwise, only the switch is added as a key with an empty string as the value.
// Use dictionary indexer to retrieve values for cmd line switches.
// Use Dictionary::ContainsKey(...) where only a switch is recorded as the key.
string.Join(\" \", args).Split(\'-\').ToList().ForEach(s => argDict.Add(s.Split()[0], (s.Split().Count() > 1 ? s.Split()[1] : \"\")));
return argDict;
}
}
}
回答18:
Oh heck. It\'s all ... Eugh. But this is legit official. From Microsoft in C# for .NET Core, maybe windows only, maybe cross-platform, but MIT licensed.
Select tidbits, method declarations and notable comments;
internal static unsafe string[] InternalCreateCommandLine(bool includeArg0)
private static unsafe int SegmentCommandLine(char * pCmdLine, string[] argArray, bool includeArg0)
private static unsafe int ScanArgument0(ref char* psrc, char[] arg)
private static unsafe int ScanArgument(ref char* psrc, ref bool inquote, char[] arg)
-
// First, parse the program name (argv[0]). Argv[0] is parsed under special rules. Anything up to
// the first whitespace outside a quoted subtring is accepted. Backslashes are treated as normal
// characters.
-
// Rules: 2N backslashes + \" ==> N backslashes and begin/end quote
// 2N+1 backslashes + \" ==> N backslashes + literal \"
// N backslashes ==> N backslashes
This is code ported to .NET Core from .NET Framework from what I assume is either the MSVC C library or CommandLineToArgvW
.
Here\'s my half-hearted attempt at handling some of the shenanigans with Regular Expressions, and ignoring the argument zero bit. It\'s a little bit wizardy.
private static readonly Regex RxWinArgs
= new Regex(\"([^\\\\s\\\"]+\\\"|((?<=\\\\s|^)(?!\\\"\\\"(?!\\\"))\\\")+)(\\\"\\\"|.*?)*\\\"[^\\\\s\\\"]*|[^\\\\s]+\",
RegexOptions.Compiled
| RegexOptions.Singleline
| RegexOptions.ExplicitCapture
| RegexOptions.CultureInvariant);
internal static IEnumerable<string> ParseArgumentsWindows(string args) {
var match = RxWinArgs.Match(args);
while (match.Success) {
yield return match.Value;
match = match.NextMatch();
}
}
Tested it a fair bit on wacky generated output. It\'s output matches a fair percentage of what the monkeys typed up and ran through CommandLineToArgvW
.
回答19:
I am not sure if I understood you, but is the problem that the character used as splitter, is also to be found inside the text? (Except for that it is escaped with double \"?)
If so, I would create a for
loop, and replace all instances where <\"> is present with <|> (or another \"safe\" character, but make sure that it only replaces <\">, and not <\"\">
After iterating the string, I would do as previously posted, split the string, but now on the character <|>.
回答20:
Yes, the string object has a built in function called Split()
that takes a single parameter specifying the character to look for as a delimiter, and returns an array of strings (string[]) with the individual values in it.