-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #542 from PixiEditor/args-parsing-fix
Fixed Command Line parsing wrongly paths with space
- Loading branch information
Showing
3 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace PixiEditor.Helpers.UI; | ||
|
||
public static class CommandLineHelpers | ||
{ | ||
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)); | ||
} | ||
|
||
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); | ||
} | ||
|
||
public static string TrimMatchingQuotes(this string input, char quote) | ||
{ | ||
if ((input.Length >= 2) && | ||
(input[0] == quote) && (input[^1] == quote)) | ||
return input.Substring(1, input.Length - 2); | ||
|
||
return input; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters