public override CommandResultCode Execute(IExecutionContext context, CommandArguments args) { String toExecute = (String)parent.aliases[name]; if (toExecute == null) { return(CommandResultCode.ExecutionFailed); } // not session-proof: if (parent.currentExecutedAliases.Contains(name)) { parent.Dispatcher.Application.Error.WriteLine("Recursive call to aliases [" + name + "]. Stopping this senseless venture."); parent.currentExecutedAliases.Clear(); return(CommandResultCode.ExecutionFailed); } string commandText = args.ToString(); commandText = toExecute + " " + commandText; parent.Dispatcher.Application.Error.WriteLine("execute alias: " + commandText); parent.currentExecutedAliases.Add(name); parent.Dispatcher.ExecuteCommand(context, commandText); parent.currentExecutedAliases.Clear(); return(CommandResultCode.Success); }
/// <summary> /// Executes the given command. /// </summary> /// <param name="context">The session this command is executed from.</param> /// <param name="args">The rest parameters following the command.</param> /// <remarks> /// The command is given completely without the final delimiter /// (which would be newline or semicolon). Before this method is /// called, the <see cref="CommandDispatcher"/> checks with the /// <see cref="IsComplete(String)"/> method, if this command is /// complete. /// </remarks> /// <returns> /// Returns a <see cref="CommandResultCode">code</see> indicating the /// result of the command execution. /// </returns> public abstract CommandResultCode Execute(IExecutionContext context, CommandArguments args);
public override CommandResultCode Execute(IExecutionContext context, CommandArguments args) { if (!args.MoveNext()) { return(CommandResultCode.SyntaxError); } string varname = args.Current; string[] newArgs = new string[args.Count - 1]; while (args.MoveNext()) { newArgs[args.CurrentIndex - 1] = args.Current; } string param = String.Join(" ", newArgs); int pos = 0; int paramLength = param.Length; // skip whitespace after 'set' while (pos < paramLength && Char.IsWhiteSpace(param[pos])) { ++pos; } // skip non-whitespace after 'set ': variable name while (pos < paramLength && !Char.IsWhiteSpace(param[pos])) { ++pos; } // skip whitespace before vlue.. while (pos < paramLength && Char.IsWhiteSpace(param[pos])) { ++pos; } String value = param.Substring(pos); if (value.StartsWith("\"") && value.EndsWith("\"")) { value = value.Substring(1, value.Length - 2); } else if (value.StartsWith("\'") && value.EndsWith("\'")) { value = value.Substring(1, value.Length - 2); } try { PropertyRegistry properties = Properties; if (properties == null) { throw new Exception("The current context doesn't support properties."); } properties.SetProperty(varname, value); } catch (Exception e) { Application.Error.WriteLine(e.Message); return(CommandResultCode.ExecutionFailed); } return(CommandResultCode.Success); }
public override CommandResultCode Execute(IExecutionContext context, CommandArguments args) { if (args.Count > 1) { return(CommandResultCode.SyntaxError); } string commandName = null; if (args.MoveNext()) { commandName = args.Current; } // nothing given: provide generic help. Application.Error.WriteLine(); int maxPad = 0; if (commandName == null) { ICollection <Command> commands = Application.Commands.RegisteredCommands; // process the command groups first... Dictionary <string, List <CommandHelp> > groups = new Dictionary <string, List <CommandHelp> >(); foreach (Command command in commands) { string groupName = command.GroupName; if (groupName == null || groupName.Length == 0) { groupName = "commands"; } List <CommandHelp> list; if (!groups.TryGetValue(groupName, out list)) { list = new List <CommandHelp>(); groups[groupName] = list; } CommandHelp commandHelp = new CommandHelp(); StringBuilder cmdPrint = new StringBuilder(" "); string[] aliases = command.Aliases; cmdPrint.Append(command.Name); if (aliases != null && aliases.Length > 0) { cmdPrint.Append(" | "); for (int i = 0; i < aliases.Length; i++) { if (i != 0) { cmdPrint.Append(" | "); } cmdPrint.Append(aliases[i]); } } commandHelp.Name = cmdPrint.ToString(); string description = command.ShortDescription; if (description == null) { // no description ... try to get the groups description... } commandHelp.Description = description; maxPad = Math.Max(maxPad, cmdPrint.Length); list.Add(commandHelp); } foreach (KeyValuePair <string, List <CommandHelp> > entry in groups) { string groupName = entry.Key; Application.Error.Write(groupName); Application.Error.Write(":"); Application.Error.WriteLine(); List <CommandHelp> commandList = entry.Value; foreach (CommandHelp command in commandList) { Application.Error.Write(" "); Application.Error.Write(command.Name); if (command.Description != null) { for (int i = 0; i < maxPad - command.Name.Length; ++i) { Application.Error.Write(" "); } Application.Error.Write(" : "); Application.Error.Write(command.Description); } Application.Error.WriteLine(); } } } else { CommandDispatcher disp = Application.Commands; string cmdString = disp.CompleteCommandName(commandName); Command c = disp.GetCommand(cmdString); if (c == null) { Application.Error.WriteLine("Help: unknown command '" + cmdString + "'"); Application.Error.WriteLine(); return(CommandResultCode.ExecutionFailed); } WriteDescription(c); } Application.Error.WriteLine(); return(CommandResultCode.Success); }