public static Action <CommandContext> ParseCommandFromCli(CommandRegistry commandRegistry, string line) { var commandElements = line.Split(" "); int i = 0; string?GetNextArgument() => i < commandElements.Length ? commandElements[i++] : null; var commandName = GetNextArgument(); if (commandName == null) { throw new Exception("Missing command name"); } return(commandRegistry.ParseCommand(commandName, GetNextArgument)); }
public static Action <CommandContext> ParseCommandFromLaunchArguments(CommandRegistry commandRegistry, string[] args, ref int index) { var i = index; string?GetNextArgument() => i < args.Length ? args[i++] : null; var commandName = GetNextArgument(); if (commandName == null) { throw new Exception("Missing command name"); } if (!commandName.StartsWith("-")) { throw new Exception($"Unknow option: {commandName}"); } commandName = commandName.Substring(1); var command = commandRegistry.ParseCommand(commandName, GetNextArgument); index = i; return(command); }