From ce6ef26346ac59aca7f3adfc85dea13d888f7696 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Tue, 21 Jun 2022 22:53:02 +0100 Subject: [PATCH] Add support to bind parameters with white space --- Bonsai.Configuration/CommandLineParser.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Bonsai.Configuration/CommandLineParser.cs b/Bonsai.Configuration/CommandLineParser.cs index a85573a12..fef24dbda 100644 --- a/Bonsai.Configuration/CommandLineParser.cs +++ b/Bonsai.Configuration/CommandLineParser.cs @@ -8,7 +8,7 @@ public class CommandLineParser const char WordSeparator = '-'; const char OptionSeparator = ':'; const string CommandPrefix = "--"; - readonly Dictionary> commands = new Dictionary>(); + readonly Dictionary commands = new Dictionary(); Action defaultHandler; public void RegisterCommand(Action handler) @@ -18,10 +18,15 @@ public void RegisterCommand(Action handler) public void RegisterCommand(string name, Action handler) { - RegisterCommand(name, option => handler()); + RegisterCommandHandler(name, handler); } public void RegisterCommand(string name, Action handler) + { + RegisterCommandHandler(name, handler); + } + + void RegisterCommandHandler(string name, Delegate handler) { if (string.IsNullOrWhiteSpace(name)) { @@ -56,9 +61,16 @@ public void Parse(string[] args) for (int i = 0; i < args.Length; i++) { var options = args[i].Split(new[] { OptionSeparator }, 2, StringSplitOptions.RemoveEmptyEntries); - if (commands.TryGetValue(options[0], out Action handler)) + if (commands.TryGetValue(options[0], out Delegate handler)) { - handler(options.Length > 1 ? options[1] : string.Empty); + if (handler is Action action) action(); + else if (handler is Action command) + { + var argument = string.Empty; + if (options.Length > 1) argument = options[1]; + else if (args.Length > i + 1) argument = args[++i]; + command(argument); + } } else {