From b990f81eda42395eff63d77093092663de751712 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Mon, 16 Apr 2018 22:39:33 -0500 Subject: support quoted strings in console commands --- src/SMAPI/Framework/CommandManager.cs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/SMAPI/Framework/CommandManager.cs b/src/SMAPI/Framework/CommandManager.cs index 79a23d03..78e03827 100644 --- a/src/SMAPI/Framework/CommandManager.cs +++ b/src/SMAPI/Framework/CommandManager.cs @@ -72,7 +72,7 @@ namespace StardewModdingAPI.Framework if (string.IsNullOrWhiteSpace(input)) return false; - string[] args = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + string[] args = this.ParseArgs(input); string name = args[0]; args = args.Skip(1).ToArray(); @@ -103,6 +103,32 @@ namespace StardewModdingAPI.Framework /********* ** Private methods *********/ + /// + /// Parses a string into an array of arguments. + /// + /// The string to parse. + private string[] ParseArgs(string input) + { + bool inQuotes = false; + IList args = new List(); + IList currentArg = new List(); + foreach (char c in input) + { + if (c == '"') + { + inQuotes = !inQuotes; + } + else if (!inQuotes && char.IsWhiteSpace(c)) + { + args.Add(string.Concat(currentArg)); + currentArg.Clear(); + } + else + currentArg.Add(c); + } + return args.Where(item => !string.IsNullOrWhiteSpace(item)).ToArray(); + } + /// Get a normalised command name. /// The command name. private string GetNormalisedName(string name) -- cgit