Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to bind parameters with white space #944

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Bonsai.Configuration/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CommandLineParser
const char WordSeparator = '-';
const char OptionSeparator = ':';
const string CommandPrefix = "--";
readonly Dictionary<string, Action<string>> commands = new Dictionary<string, Action<string>>();
readonly Dictionary<string, Delegate> commands = new Dictionary<string, Delegate>();
Action<string> defaultHandler;

public void RegisterCommand(Action<string> handler)
Expand All @@ -18,10 +18,15 @@ public void RegisterCommand(Action<string> handler)

public void RegisterCommand(string name, Action handler)
{
RegisterCommand(name, option => handler());
RegisterCommandHandler(name, handler);
}

public void RegisterCommand(string name, Action<string> handler)
{
RegisterCommandHandler(name, handler);
}

void RegisterCommandHandler(string name, Delegate handler)
{
if (string.IsNullOrWhiteSpace(name))
{
Expand Down Expand Up @@ -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<string> 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<string> command)
{
var argument = string.Empty;
if (options.Length > 1) argument = options[1];
else if (args.Length > i + 1) argument = args[++i];
command(argument);
}
}
else
{
Expand Down