private void OnTabComplete(object sender, TabCompleteEventArgs e) { try { string completeCommandString = application.PartialLine.Trim(); bool variableExpansion = false; // ok, do we have a variable expansion ? int pos = e.Text.Length - 1; while (pos > 0 && (e.Text[pos] != '$') && Char.IsLetter(e.Text[pos])) { --pos; } // either $... or ${... if ((pos >= 0 && e.Text[pos] == '$')) { variableExpansion = true; } else if ((pos >= 1) && e.Text[pos - 1] == '$' && e.Text[pos] == '{') { variableExpansion = true; --pos; } if (variableExpansion) { if (application is ISettingsHandler) { ApplicationSettings settings = ((ISettingsHandler)application).Settings; if (e.State == 0) { variablePrefix = e.Text.Substring(0, pos); String varname = e.Text.Substring(pos); possibleValues = settings.CompleteUserVariable(varname); } if (possibleValues.MoveNext()) { e.Output = variablePrefix + ((String)possibleValues.Current); } else { possibleValues.Reset(); } } } // the first word.. the command. else if (completeCommandString.Equals(e.Text)) { string text = e.Text.ToLower(); if (e.State == 0) { possibleValues = GetRegisteredCommandNames(text).GetEnumerator(); } while (HasNext(possibleValues)) { String nextKey = (String)possibleValues.Current; if (nextKey == null || nextKey.Length == 0) // don't complete the 'empty' thing. { continue; } if (text.Length < 1) { Command c = (Command)commandMap[nextKey]; if (!c.CommandCompletion) { continue; } if (c.RequiresContext && (application.ActiveContext == null || !application.ActiveContext.IsIsolated)) { continue; } } if (nextKey.StartsWith(text)) { e.Output = nextKey; break; } } } // .. otherwise get completion from the specific command. else { string text = e.Text.ToLower(); if (e.State == 0) { Command cmd = GetCommand(completeCommandString); if (cmd != null) { possibleValues = cmd.Complete(this, completeCommandString, text); } } while (HasNext(possibleValues)) { string key = (string)possibleValues.Current; if (key.ToLower().StartsWith(text)) { e.Output = key; break; } } } } catch (Exception ex) { Application.Error.WriteLine("An error occurred while TAB-completing: {0}", ex.Message); e.Error = true; possibleValues = null; throw; } }
private void WriteDescription(Command c) { string desc = c.LongDescription; if (desc == null) { if (c.ShortDescription != null) { desc = "\t[short description]: " + c.ShortDescription; } } string[] synopsis = c.Synopsis; if (synopsis != null && synopsis.Length > 0) { Application.Error.WriteBold("SYNOPSIS"); Application.Error.WriteLine(); for (int i = 0; i < synopsis.Length; i++) { Application.Error.WriteLine("\t" + synopsis[i]); } Application.Error.WriteLine(); } if (desc != null) { Application.Error.WriteBold("DESCRIPTION"); Application.Error.WriteLine(); StringReader reader = new StringReader(desc); string line; while ((line = reader.ReadLine()) != null) { Application.Error.Write("\t"); Application.Error.WriteLine(line); } if (c.RequiresContext) { Application.Error.WriteLine("\t[Requires valid context]"); } } if (desc == null && synopsis == null) { Application.Error.WriteLine("no detailed help for '" + c.Name + "'"); } }
public void ExecuteCommand(IExecutionContext context, string commandText) { if (String.IsNullOrEmpty(commandText)) { return; } // remove trailing command separator and whitespaces. StringBuilder cmdBuilder = new StringBuilder(commandText.Trim()); int i; for (i = cmdBuilder.Length - 1; i > 0; --i) { char c = cmdBuilder[i]; if (c != CommandSeparator && !Char.IsWhiteSpace(c)) { break; } } if (i < 0) { return; } cmdBuilder.Length = i + 1; string cmd = cmdBuilder.ToString(); string cmdName = CompleteCommandName(cmd); Command command = GetCommandFromCooked(cmdName); if (command != null) { try { string[] args = new string[0]; string parameters = cmd.Substring(cmdName.Length); if (parameters.Length > 0) { parameters = parameters.Trim(); args = parameters.Split(' '); ArrayList argsList = new ArrayList(); for (int j = 0; j < args.Length; j++) { args[j] = args[j].Trim(); if (args[j].Length > 0) { argsList.Add(args[j]); } } args = (string[])argsList.ToArray(typeof(string)); } if (command.RequiresContext && (context == null || !context.IsIsolated)) { Application.Error.WriteLine(cmdName + " requires a valid isolated context."); return; } OnExecuting(context, commandText); CommandResultCode result = command.Execute(context, new CommandArguments(args)); OnExecuted(context, commandText, result); switch (result) { case CommandResultCode.SyntaxError: { string[] synopsis = command.Synopsis; if (synopsis != null && synopsis.Length > 0) { Application.Error.WriteLine(cmdName + " usage: "); for (int j = 0; j < synopsis.Length; j++) { Application.Error.Write(" "); Application.Error.WriteLine(synopsis[j]); } } else { Application.Error.WriteLine(cmdName + " syntax error."); } break; } case CommandResultCode.ExecutionFailed: { // if we are in batch mode, then no message is written // to the screen by default. Thus we don't know, _what_ // command actually failed. So in this case, write out // the offending command. if (IsInBatch) { Application.Error.WriteLine("-- failed command: "); Application.Error.WriteLine(commandText); } break; } } } catch (Exception e) { #if DEBUG System.Console.Error.WriteLine(e.Message); System.Console.Error.WriteLine(e.StackTrace); #endif Application.Error.WriteLine(e.ToString()); OnExecuted(context, commandText, CommandResultCode.ExecutionFailed); } } }
public CommandInfo(Type cmdType, Command command) { CommandType = cmdType; Command = command; }
internal void RegisterAdditionalCommand(string commandName, Command command) { commandMap.Add(commandName, command); }
public CommandInfo(Type cmdType, Command command) { CommandType = cmdType; Command = command; }
internal void RegisterAdditionalCommand(string commandName, Command command) { commandMap.Add(commandName, command); }
public void Unregister(Command command) { if (command == null) throw new ArgumentNullException("command"); Unregister(command.GetType()); }
public void Register(Command command) { if (Application.IsRunning) throw new InvalidOperationException("The application is running."); if (command == null) throw new ArgumentNullException("command"); try { command.Init(); } catch(Exception e) { throw new ArgumentException("An error occurred while initializing the command: " + e.Message); } if (command is IInterruptable) Application.Interruption.Push(command as IInterruptable); if (command.Application != null && command.Application != application) throw new ArgumentException("The command instance is already registered by another application."); command.SetApplicationContext(application); CommandInfo commandInfo = new CommandInfo(command.GetType(), command); commands.Add(commandInfo); string name = commandInfo.Command.Name; commandMap.Add(name, commandInfo.Command); if (commandInfo.Command.HasAliases) { string[] cmdAliases = commandInfo.Command.Aliases; for (int i = 0; i < cmdAliases.Length; ++i) { if (commandMap.ContainsKey(cmdAliases[i])) throw new ArgumentException("attempt to register command '" + cmdAliases[i] + "', that is already used"); commandMap.Add(cmdAliases[i], commandInfo.Command); } } }