Skip to content

Commit

Permalink
Add support to bind parameters with white space
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jul 11, 2022
1 parent e390ef2 commit ce6ef26
Showing 1 changed file with 16 additions and 4 deletions.
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

0 comments on commit ce6ef26

Please sign in to comment.