public ICommand InitializeEntryPoint( CommandLineInput commandLineInput, IReadOnlyDictionary <string, string> environmentVariables, ITypeActivator activator) { var command = TryFindCommand(commandLineInput, out var argumentOffset) ?? throw CliFxException.CannotFindMatchingCommand(commandLineInput); var parameterInputs = argumentOffset == 0 ? commandLineInput.UnboundArguments.ToArray() : commandLineInput.UnboundArguments.Skip(argumentOffset).ToArray(); return(command.CreateInstance(parameterInputs, commandLineInput.Options, environmentVariables, activator)); }
// TODO: this out parameter is not a really nice design public CommandSchema?TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset) { // Try to find the command that contains the most of the input arguments in its name for (var i = commandLineInput.UnboundArguments.Count; i >= 0; i--) { var potentialCommandName = string.Join(" ", commandLineInput.UnboundArguments.Take(i)); var matchingCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialCommandName)); if (matchingCommand != null) { argumentOffset = i; return(matchingCommand); } } argumentOffset = 0; return(Commands.FirstOrDefault(c => c.IsDefault)); }
public ICommand InitializeEntryPoint( CommandLineInput commandLineInput, IReadOnlyDictionary <string, string> environmentVariables, ITypeActivator activator) { var command = TryFindCommand(commandLineInput, out var argumentOffset); if (command == null) { throw new CliFxException( $"Can't find a command that matches arguments [{string.Join(" ", commandLineInput.UnboundArguments)}]."); } var parameterValues = argumentOffset == 0 ? commandLineInput.UnboundArguments.Select(a => a.Value).ToArray() : commandLineInput.UnboundArguments.Skip(argumentOffset).Select(a => a.Value).ToArray(); return(command.CreateInstance(parameterValues, commandLineInput.Options, environmentVariables, activator)); }
public ICommand InitializeEntryPoint( CommandLineInput commandLineInput, IReadOnlyDictionary <string, string> environmentVariables) => InitializeEntryPoint(commandLineInput, environmentVariables, new DefaultTypeActivator());
public CommandSchema?TryFindCommand(CommandLineInput commandLineInput) => TryFindCommand(commandLineInput, out _);
public ICommand InitializeEntryPoint(CommandLineInput commandLineInput) => InitializeEntryPoint(commandLineInput, new Dictionary <string, string>());