Example #1
0
        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));
        }
Example #2
0
        // 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));
        }
Example #3
0
        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));
        }
Example #4
0
 public ICommand InitializeEntryPoint(
     CommandLineInput commandLineInput,
     IReadOnlyDictionary <string, string> environmentVariables) =>
 InitializeEntryPoint(commandLineInput, environmentVariables, new DefaultTypeActivator());
Example #5
0
 public CommandSchema?TryFindCommand(CommandLineInput commandLineInput) =>
 TryFindCommand(commandLineInput, out _);
Example #6
0
 public ICommand InitializeEntryPoint(CommandLineInput commandLineInput) =>
 InitializeEntryPoint(commandLineInput, new Dictionary <string, string>());