Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions ConsoleApplicationBase/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void Main(string[] args)
// Any static classes containing commands for use from the
// console are located in the Commands namespace. Load
// references to each type in that namespace via reflection:
_commandLibraries = new Dictionary<string, Dictionary<string,
_commandLibraries = new Dictionary<string, Dictionary<string,
IEnumerable<ParameterInfo>>>();

// Use reflection to load all of the classes in the Commands namespace:
Expand Down Expand Up @@ -48,7 +48,7 @@ static void Main(string[] args)
static void Run()
{
while (true)
{
{
var consoleInput = ReadFromConsole();
if (string.IsNullOrWhiteSpace(consoleInput)) continue;

Expand All @@ -57,11 +57,24 @@ static void Run()
// Create a ConsoleCommand instance:
var cmd = new ConsoleCommand(consoleInput);

// Execute the command:
string result = Execute(cmd);

// Write out the result:
WriteToConsole(result);
switch (cmd.Name)
{
case "help":
case "?":
case "ayuda":
WriteToConsole(BuildHelpMessage());
break;
case "exit":
case "salir":
WriteToConsole("Closing program...");
return;
default:
// Execute the command:
string result = Execute(cmd);
// Write out the result:
WriteToConsole(result);
break;
}
}
catch (Exception ex)
{
Expand All @@ -85,12 +98,12 @@ static string Execute(ConsoleCommand command)
// Validate the command name:
if (!_commandLibraries.ContainsKey(command.LibraryClassName))
{
return badCommandMessage;
return badCommandMessage + BuildHelpMessage();
}
var methodDictionary = _commandLibraries[command.LibraryClassName];
if (!methodDictionary.ContainsKey(command.Name))
{
return badCommandMessage;
return badCommandMessage + System.Environment.NewLine + BuildHelpMessage(command.LibraryClassName);
}

// Make sure the corret number of required arguments are provided:
Expand All @@ -108,9 +121,12 @@ static string Execute(ConsoleCommand command)

if (requiredCount > providedCount)
{
return string.Format(
"Missing required argument. {0} required, {1} optional, {2} provided",
var message = string.Format(
"Missing required parameters. {0} required, {1} optinal, {2} provided",
requiredCount, optionalCount, providedCount);
string comando = command.LibraryClassName + "." + command.Name;
message += Environment.NewLine + paramInfoList.Aggregate<ParameterInfo, String>("Call: " + comando, (total, next) => total + " " + next.Name + " <" + next.ParameterType.Name + ">");
return message;
}

// Make sure all arguments are coerced to the proper type, and that there is a
Expand Down Expand Up @@ -196,8 +212,8 @@ static string Execute(ConsoleCommand command)
static object CoerceArgument(Type requiredType, string inputValue)
{
var requiredTypeCode = Type.GetTypeCode(requiredType);
string exceptionMessage =
string.Format("Cannnot coerce the input argument {0} to required type {1}",
string exceptionMessage =
string.Format("Cannnot coerce the input argument {0} to required type {1}",
inputValue, requiredType.Name);

object result = null;
Expand Down Expand Up @@ -365,7 +381,7 @@ static object CoerceArgument(Type requiredType, string inputValue)

public static void WriteToConsole(string message = "")
{
if(message.Length > 0)
if (message.Length > 0)
{
Console.WriteLine(message);
}
Expand All @@ -379,5 +395,26 @@ public static string ReadFromConsole(string promptMessage = "")
Console.Write(_readPrompt + promptMessage);
return Console.ReadLine();
}

static string BuildHelpMessage(string library = null)
{
var sb = new StringBuilder("Commands: ");
sb.AppendLine();
foreach (var item in _commandLibraries)
{
if (library != null && item.Key != library)
continue;
foreach (var cmd in item.Value)
{
sb.Append(ConsoleFormatting.Indent(1));
sb.Append(item.Key);
sb.Append(".");
sb.Append(cmd.Key);
sb.AppendLine();
}

}
return sb.ToString();
}
}
}