diff --git a/TGServiceInterface/Command.cs b/TGServiceInterface/Command.cs index ebdcefac50..7354451202 100644 --- a/TGServiceInterface/Command.cs +++ b/TGServiceInterface/Command.cs @@ -4,118 +4,90 @@ using System.Threading; namespace TGServiceInterface { + /// + /// Exit codes for s + /// public enum ExitCode { + /// + /// The ran successfully + /// Normal = 0, + /// + /// The connection to the service was interrupted during the + /// ConnectionError = 1, + /// + /// Invalid parameters for + /// BadCommand = 2, + /// + /// The command failed due to conditions on the service + /// ServerError = 3, } + + /// + /// Helper for creating a text tree + /// public abstract class Command { + /// + /// Proc that will show a message to the invoker. Do not call directly, use instead + /// public static ThreadLocal> OutputProcVar = new ThreadLocal>(); + /// + /// Write output to the invoker + /// + /// The output to display protected static void OutputProc(string message) { OutputProcVar.Value(message); } + /// + /// The text that invokes this . Set in constructor + /// public string Keyword { get; protected set; } - public Command[] Children { get; protected set; } = { }; + /// + /// The number of parameters this requires. Set in Constructor + /// public int RequiredParameters { get; protected set; } + /// + /// Caller of , can be used to modify the root behaviour of the + /// + /// List of parameters passed to the + /// public virtual ExitCode DoRun(IList parameters) { return Run(parameters); } + /// + /// Override to do the actions of the + /// + /// List of parameters passed to the . Guaranteed to have at least non-empty/whitespace entries + /// An describing the execution of the protected abstract ExitCode Run(IList parameters); + /// + /// Prints usage text of the to the invoker + /// public virtual void PrintHelp() { var argstr = GetArgumentString(); OutputProc(String.Format("{0} {1}- {2}", Keyword, argstr.Length > 0 ? argstr + " " : "", GetHelpText())); } + /// + /// Override to add argument text to the + /// Format is <required> <arguments> [optional] [arguments] + /// + /// Formatted argument text for the public virtual string GetArgumentString() { return ""; } + /// + /// Override to add usage text to the + /// + /// Formatted usage text for the public abstract string GetHelpText(); } - - public class RootCommand : Command - { - public static bool PrintHelpList = false; - protected override ExitCode Run(IList parameters) - { - if (parameters.Count > 0) - { - var LocalKeyword = parameters[0].Trim().ToLower(); - parameters.RemoveAt(0); - switch (LocalKeyword) - { - case "help": - case "?": - PrintHelp(); - return ExitCode.Normal; - default: - foreach (var c in Children) - if (c.Keyword == LocalKeyword) - { - if(parameters.Count> 0) - { - var possibleHelp = parameters[0].ToLower(); - if (possibleHelp == "help" || possibleHelp == "?") - { - c.PrintHelp(); - return ExitCode.Normal; - } - } - if (parameters.Count < c.RequiredParameters) - { - OutputProc("Not enough parameters!"); - return ExitCode.BadCommand; - } - return c.DoRun(parameters); - } - parameters.Insert(0, LocalKeyword); - break; - } - } - OutputProc(String.Format("Invalid command! Type '{0}?' or '{0}help' for available commands.", Keyword != null ? Keyword + " " : "")); - return ExitCode.BadCommand; - } - public override void PrintHelp() - { - var Final = new List(); - if (PrintHelpList) - { - foreach (var c in Children) - Final.Add(c.Keyword); - OutputProc("Available commands (type '?' or 'help' after command for more info): " + String.Join(", ", Final)); - } - else - { - var Prefixes = new List(); - var Postfixes = new List(); - int MaxPrefixLen = 0; - foreach (var c in Children) - { - var ns = c.Keyword + " " + c.GetArgumentString(); - MaxPrefixLen = Math.Max(MaxPrefixLen, ns.Length); - Prefixes.Add(ns); - Postfixes.Add(c.GetHelpText()); - } - - for (var I = 0; I < Prefixes.Count; ++I) - { - var lp = Prefixes[I]; - for (; lp.Length < MaxPrefixLen + 1; lp += " ") ; - Final.Add(lp + "- " + Postfixes[I]); - } - Final.Sort(); - Final.ForEach(OutputProc); - } - } - - public override string GetHelpText() - { - throw new NotImplementedException(); - } - } } diff --git a/TGServiceInterface/TGServiceInterface.csproj b/TGServiceInterface/TGServiceInterface.csproj index 411df41cfd..25feb1ec2d 100644 --- a/TGServiceInterface/TGServiceInterface.csproj +++ b/TGServiceInterface/TGServiceInterface.csproj @@ -58,6 +58,7 @@ +