using System;
using System.Collections.Generic;
using System.Threading;
namespace TGServiceInterface
{
///
/// Helper for creating a text tree
///
public abstract class Command
{
///
/// Exit codes for s
///
public enum ExitCode : int
{
///
/// 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,
}
///
/// 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; }
///
/// 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
/// An describing the execution of 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();
}
}