private IEnumerable <ICommand> EvalCommands(string inputString) { IEnumerable <string> tokens = inputString.Split(' '); int count = 0; foreach (string inputToken in tokens) { ++count; ICommand command = GlobalScope.ReservedCommands.SingleOrDefault(cmd => cmd.Name == inputToken); yield return(command?.Initialise(string.Join(" ", tokens.Skip(count).ToArray())) ?? new NoOpCommand()); } }
/// <summary> /// Executes a command /// </summary> /// <param name="cmd">The command to execute</param> /// <param name="args">Arguments to apply to the command</param> /// <param name="directive">Directives to execute with</param> /// <returns>The outcome of the execution</returns> private CommandResult ExecuteCommand(ICommand cmd, string[] args, ExecutionDirective directive) { cmd.Initialise(_context, _formatter); CommandResult result = null; var manualParseInterface = CommandInspector.GetManualParseCommand(cmd); if (manualParseInterface != null) { var subArgs = from arg in args select Parser.PerformSubstitution(_context, arg); result = manualParseInterface.Run(subArgs.ToArray()); } else { var properties = cmd.GetType().GetProperties(); object propValue = null; var processingArgs = args; // Process multi-word named parameters first foreach (var prop in properties) { propValue = null; var namedParameterAttribute = CommandInspector.GetNamedParameter(prop); if (namedParameterAttribute != null && namedParameterAttribute.WordCount > 1) { var words = new List<string>(); for (var i = 0; i < namedParameterAttribute.WordCount; i++) { var value = string.Empty; ParameterUtil.GetParameter(args, "-" + namedParameterAttribute.Name, i, ref value); if (!string.IsNullOrEmpty(value)) words.Add(value); } if (words.Count > 0) { propValue = words.ToArray(); processingArgs = ParameterUtil.RemoveParameter(processingArgs, "-" + namedParameterAttribute.Name, namedParameterAttribute.WordCount); } } ConvertAndAssignParameter(prop, cmd, propValue); } // Find flags var flags = from prop in properties let attr = CommandInspector.GetFlagParameter(prop) where attr != null select attr.Name; // Parse remaining arguments StringDictionary named = null; string[] numbered = null; ParameterUtil.ExtractParameters(out named, out numbered, processingArgs, flags.ToArray()); // Map the parameters to properties foreach (var prop in properties) { propValue = null; var namedParameterAttribute = CommandInspector.GetNamedParameter(prop); if (namedParameterAttribute != null) { if (named.ContainsKey(namedParameterAttribute.Name)) propValue = named[namedParameterAttribute.Name]; } var numberedParameterAttribute = CommandInspector.GetNumberedParameter(prop); if (numberedParameterAttribute != null) { if (numberedParameterAttribute.Number < numbered.Length) propValue = numbered[numberedParameterAttribute.Number]; } var flagParameterAttribute = CommandInspector.GetFlagParameter(prop); if (flagParameterAttribute != null) { if (named.ContainsKey(flagParameterAttribute.Name)) { #if NET45 var origVal = (bool)prop.GetValue(cmd); #else var origVal = (bool)prop.GetValue(cmd, null); #endif propValue = !origVal; } } ConvertAndAssignParameter(prop, cmd, propValue); } AssignListParameter(cmd, properties, numbered); result = cmd.Run(); } if ((bool)(directive.StopOnError ?? false) && (result.Status == CommandStatus.Failure)) _executionFaulted = true; return result; }
protected virtual void InitCommand(ICommand command) { _command = command; _context.CurrentDatabase = _database; _command.Initialise(_context, new TextOutputFormatter()); }