From 8b3696851ef952331b41698792aab9a9b4b80bd7 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Tue, 10 Apr 2018 15:54:58 -0300 Subject: [PATCH 01/14] Fixed bug caused by passing the parameters in the wrong order while invoking Command output --- CommandsParser/BaseCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index a65fe14..ddc5b7a 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -130,7 +130,7 @@ public BaseCommand(string name, string description, List aliases) public virtual string Execute(string[] arguments) { string commandOutput = CommandOutput; - OnOutputAvailable(new OutputAvailableEventArgs(Name, commandOutput)); + OnOutputAvailable(new OutputAvailableEventArgs(commandOutput, Name)); return commandOutput; } From 9f64222ebccb4c2851909f5e804c1dee1e71b24e Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Tue, 10 Apr 2018 15:57:08 -0300 Subject: [PATCH 02/14] Version bump --- CommandsParser/CommandsParser.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CommandsParser/CommandsParser.csproj b/CommandsParser/CommandsParser.csproj index 856cb0f..5c32b57 100644 --- a/CommandsParser/CommandsParser.csproj +++ b/CommandsParser/CommandsParser.csproj @@ -2,7 +2,7 @@ netstandard1.0 - 0.4.0 + 0.4.1 From 32704a56cf52d0d72e3a3413e9a954a097406ef4 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Tue, 10 Apr 2018 15:58:36 -0300 Subject: [PATCH 03/14] Added release package --- ReleasePackages/CommandParser.0.4.1.nuspec | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 ReleasePackages/CommandParser.0.4.1.nuspec diff --git a/ReleasePackages/CommandParser.0.4.1.nuspec b/ReleasePackages/CommandParser.0.4.1.nuspec new file mode 100644 index 0000000..18d722d --- /dev/null +++ b/ReleasePackages/CommandParser.0.4.1.nuspec @@ -0,0 +1,18 @@ + + + + CommandParser + 0.4.1 + CommandParser + Jimmy Toledo + Jimmy Toledo + https://github.com/Jim-Dev/CommandLineParser/blob/master/LICENSE + https://github.com/Jim-Dev/CommandLineParser + false + CommandLineParser as the name suggests, is a commands parser, maps a string as a command name and/or a set of aliases to an action to be excecuted, in the same fashion that a terminal/console works. +Now compiled to target NetStandard 1.0! + + + + + \ No newline at end of file From c8ae898348e35b4bad9661ac991329f2d039aba2 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 23 Apr 2018 16:11:17 -0300 Subject: [PATCH 04/14] Added Echo command, added CmdParser reference to BaseCommand --- CommandsParser/BaseCommand.cs | 52 ++++++++----------- CommandsParser/CmdParser.cs | 31 +++++++---- CommandsParser/Commands/AliasCommand.cs | 6 ++- CommandsParser/Commands/EchoCommand.cs | 33 ++++++++++++ CommandsParser/Commands/HelpCommand.cs | 8 +-- .../Commands/ClearConsoleCommand.cs | 8 +-- CommandsParserSample/Commands/CloseCommand.cs | 8 +-- CommandsParserSample/Program.cs | 7 +-- 8 files changed, 98 insertions(+), 55 deletions(-) create mode 100644 CommandsParser/Commands/EchoCommand.cs diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index ddc5b7a..a71efe9 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -18,6 +18,8 @@ public class BaseCommand public delegate void OutputAvailableEventHandler(object sender, OutputAvailableEventArgs e); public event OutputAvailableEventHandler OutputAvailable; + protected CmdParser CmdParser; + protected string CommandOutput { get @@ -65,42 +67,30 @@ public string GetOutputBuffer() } } - public BaseCommand() - { - Name = DEFAULT_COMMAND_NAME; - Description = DEFAULT_COMMAND_DESCRIPTION; - Aliases = new List(); - Help = DEFAULT_COMMAND_HELP; - if (commandOutput == null) - commandOutput = new StringBuilder(); - } - public BaseCommand(string name) + public BaseCommand(CmdParser cmdParser, string name, string description, List aliases, string[] commandHelp) { - Name = name; - Description = DEFAULT_COMMAND_DESCRIPTION; - Aliases = new List(); - Help = DEFAULT_COMMAND_HELP; - if (commandOutput == null) - commandOutput = new StringBuilder(); - } - public BaseCommand(string name, string description) - { - Name = name; - Description = description; - Aliases = new List(); - Help = DEFAULT_COMMAND_HELP; - if (commandOutput == null) - commandOutput = new StringBuilder(); - } - public BaseCommand(string name, string description, List aliases) - { - Name = name; - Description = description; + this.CmdParser = cmdParser; + + if (name.Trim() != string.Empty) + Name = name; + else + name = DEFAULT_COMMAND_NAME; + + if (description.Trim() != string.Empty) + Description = description; + else + description = DEFAULT_COMMAND_DESCRIPTION; + if (aliases != null && aliases.Count > 0) Aliases = aliases; else Aliases = new List(); - Help = DEFAULT_COMMAND_HELP; + + if (commandHelp != null && commandHelp.Length > 0) + Help = commandHelp; + else + Help = DEFAULT_COMMAND_HELP; + if (commandOutput == null) commandOutput = new StringBuilder(); } diff --git a/CommandsParser/CmdParser.cs b/CommandsParser/CmdParser.cs index 4c319e0..5a0e0b7 100644 --- a/CommandsParser/CmdParser.cs +++ b/CommandsParser/CmdParser.cs @@ -18,6 +18,11 @@ public class CmdParser internal static List AvailableCommands; + public StandardOutput StdOutput + { + get { return stdOutput; } + } + public CmdParser() { stdOutput = new StandardOutput(); @@ -69,8 +74,9 @@ public static BaseCommand GetCommandByAlias(string commandAlias) private void AddDefaultCommands() { - AddCommand(new Commands.HelpCommand()); - AddCommand(new Commands.AliasCommand()); + AddCommand(new Commands.HelpCommand(this)); + AddCommand(new Commands.AliasCommand(this)); + AddCommand(new Commands.EchoCommand(this)); } public void AddCommand(BaseCommand command) @@ -103,16 +109,21 @@ public bool Execute(string input) string[] splitInput = input.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - BaseCommand commandToExecute = GetCommand(splitInput[0]); - if (commandToExecute != null) + if (splitInput.Length > 0) { - string[] args = new string[splitInput.Length - 1]; - Array.Copy(splitInput, 1, args, 0, args.Length); - stdOutput.Echo(commandToExecute.Execute(args), commandToExecute.Name); - return true; + string commandName = splitInput[0]; + BaseCommand commandToExecute = GetCommand(splitInput[0]); + if (commandToExecute != null) + { + string[] args = new string[splitInput.Length - 1]; + Array.Copy(splitInput, 1, args, 0, args.Length); + stdOutput.Echo(commandToExecute.Execute(args), commandToExecute.Name); + return true; + } + else + stdOutput.EchoLine(string.Format("ERROR, command \"{0}\", not found.", commandName), commandName); } - else - stdOutput.EchoLine(string.Format("ERROR, command \"{0}\", not found.", commandToExecute.Name), commandToExecute.Name); + return false; } diff --git a/CommandsParser/Commands/AliasCommand.cs b/CommandsParser/Commands/AliasCommand.cs index 3425e04..9941fd0 100644 --- a/CommandsParser/Commands/AliasCommand.cs +++ b/CommandsParser/Commands/AliasCommand.cs @@ -8,9 +8,11 @@ namespace CommandsParser.Commands { public class AliasCommand:BaseCommand { - public AliasCommand() - : base("alias", + public AliasCommand(CmdParser cmdParser) + : base(cmdParser, + "alias", "Display the list of all aliases for a given command or show if a command is an alias of another command", + null, null) { } diff --git a/CommandsParser/Commands/EchoCommand.cs b/CommandsParser/Commands/EchoCommand.cs new file mode 100644 index 0000000..ec8e83c --- /dev/null +++ b/CommandsParser/Commands/EchoCommand.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CommandsParser.Commands +{ + public class EchoCommand:BaseCommand + { + public EchoCommand(CmdParser cmdParser) + : base(cmdParser, + "echo", + "Echoes a message to the active Output", + null, + null) + { } + + public override string[] Help + { + get + { + return new string[] { + "Echoes a message to the active Output"}; + } + } + + public override string Execute(string[] arguments) + { + string output= string.Join(" ", arguments); + base.CmdParser.StdOutput.EchoLine(output); + return output; + } + } +} diff --git a/CommandsParser/Commands/HelpCommand.cs b/CommandsParser/Commands/HelpCommand.cs index bd90d87..df79b99 100644 --- a/CommandsParser/Commands/HelpCommand.cs +++ b/CommandsParser/Commands/HelpCommand.cs @@ -8,10 +8,12 @@ namespace CommandsParser.Commands { public class HelpCommand : BaseCommand { - public HelpCommand() - : base("help", + public HelpCommand(CmdParser cmdParser) + : base(cmdParser, + "help", "Displays the list of commands, or the help file for a specified command", - new List() { "man" }) + new List() { "man" }, + null) { } public override string[] Help diff --git a/CommandsParserSample/Commands/ClearConsoleCommand.cs b/CommandsParserSample/Commands/ClearConsoleCommand.cs index 27a6f3c..299d07e 100644 --- a/CommandsParserSample/Commands/ClearConsoleCommand.cs +++ b/CommandsParserSample/Commands/ClearConsoleCommand.cs @@ -10,10 +10,12 @@ namespace CommandsParserSample.Commands public class ClearConsoleCommand : BaseCommand { - public ClearConsoleCommand() - : base("clear", + public ClearConsoleCommand(CmdParser cmdParser) + : base(cmdParser, + "clear", "Clears the console", - new List() { "cls" }) + new List() { "cls" }, + null) { } public override string[] Help diff --git a/CommandsParserSample/Commands/CloseCommand.cs b/CommandsParserSample/Commands/CloseCommand.cs index 8ec9bdd..50ffae5 100644 --- a/CommandsParserSample/Commands/CloseCommand.cs +++ b/CommandsParserSample/Commands/CloseCommand.cs @@ -9,10 +9,12 @@ namespace CommandsParserSample.Commands { public class CloseCommand : BaseCommand { - public CloseCommand() - : base("close", + public CloseCommand(CmdParser cmdParser) + : base(cmdParser, + "close", "Close the application", - new List() { "exit", "shutdown" }) + new List() { "exit", "shutdown" }, + null) { } public override string[] Help diff --git a/CommandsParserSample/Program.cs b/CommandsParserSample/Program.cs index 67516eb..c8391c5 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -16,8 +16,8 @@ class Program static void Main(string[] args) { - cmdParser.AddCommand(new ClearConsoleCommand()); - cmdParser.AddCommand(new CloseCommand()); + cmdParser.AddCommand(new ClearConsoleCommand(cmdParser)); + cmdParser.AddCommand(new CloseCommand(cmdParser)); cmdParser.OnOutputAvailable += StdOutput_OnOutputAvailable; @@ -25,7 +25,8 @@ static void Main(string[] args) while (isRunning) { - cmdParser.Execute(Console.ReadLine()); + string input = Console.ReadLine(); + cmdParser.Execute(input); } } From 877d51d54695c3743cbf019fc376dcaca6a4b530 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 23 Apr 2018 16:52:04 -0300 Subject: [PATCH 05/14] Commands now use CmdParser StandardOutput module --- CommandsParser/BaseCommand.cs | 22 ++++++++++--------- CommandsParser/CmdParser.cs | 3 ++- CommandsParser/Commands/AliasCommand.cs | 21 ++++++++++++------ CommandsParser/Commands/EchoCommand.cs | 4 ++-- CommandsParser/Commands/HelpCommand.cs | 20 ++++++++++++----- CommandsParser/StandardOutput.cs | 9 ++++++++ .../Commands/ClearConsoleCommand.cs | 8 +++---- CommandsParserSample/Commands/CloseCommand.cs | 8 +++---- CommandsParserSample/Program.cs | 2 +- 9 files changed, 62 insertions(+), 35 deletions(-) diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index a71efe9..0ff184a 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -13,13 +13,14 @@ public class BaseCommand private const string DEFAULT_COMMAND_DESCRIPTION = "NO_DESCRIPTION"; private readonly string[] DEFAULT_COMMAND_HELP = { "UNDEFINED_HELP"}; - private StringBuilder commandOutput; + //private StringBuilder commandOutput; - public delegate void OutputAvailableEventHandler(object sender, OutputAvailableEventArgs e); - public event OutputAvailableEventHandler OutputAvailable; + //public delegate void OutputAvailableEventHandler(object sender, OutputAvailableEventArgs e); + //public event OutputAvailableEventHandler OutputAvailable; protected CmdParser CmdParser; + /* protected string CommandOutput { get @@ -29,7 +30,8 @@ protected string CommandOutput return commandResult; } } - + */ + /* protected void AppendToResult(string message) { commandOutput.Append(message); @@ -66,7 +68,7 @@ public string GetOutputBuffer() return string.Empty; } } - + */ public BaseCommand(CmdParser cmdParser, string name, string description, List aliases, string[] commandHelp) { this.CmdParser = cmdParser; @@ -90,9 +92,6 @@ public BaseCommand(CmdParser cmdParser, string name, string description, List /// /// - public virtual string Execute(string[] arguments) + public virtual void Execute(string[] arguments) { + /* string commandOutput = CommandOutput; OnOutputAvailable(new OutputAvailableEventArgs(commandOutput, Name)); return commandOutput; + */ } - + /* public void OnOutputAvailable(OutputAvailableEventArgs eventArgs) { OutputAvailable?.Invoke(this, eventArgs ); } + */ } } \ No newline at end of file diff --git a/CommandsParser/CmdParser.cs b/CommandsParser/CmdParser.cs index 5a0e0b7..e0734ad 100644 --- a/CommandsParser/CmdParser.cs +++ b/CommandsParser/CmdParser.cs @@ -117,7 +117,8 @@ public bool Execute(string input) { string[] args = new string[splitInput.Length - 1]; Array.Copy(splitInput, 1, args, 0, args.Length); - stdOutput.Echo(commandToExecute.Execute(args), commandToExecute.Name); + commandToExecute.Execute(args); + //stdOutput.Echo(commandToExecute.Execute(args), commandToExecute.Name); return true; } else diff --git a/CommandsParser/Commands/AliasCommand.cs b/CommandsParser/Commands/AliasCommand.cs index 9941fd0..be7be87 100644 --- a/CommandsParser/Commands/AliasCommand.cs +++ b/CommandsParser/Commands/AliasCommand.cs @@ -26,7 +26,7 @@ public override string[] Help } } - public override string Execute(string[] arguments) + public override void Execute(string[] arguments) { if (arguments.Length > 0) //Show help for arg[0] command { @@ -44,23 +44,26 @@ public override string Execute(string[] arguments) else sb.AppendFormat("{0},", command.Aliases[i]); } - - AppendLineToResult(sb.ToString()); + CmdParser.StdOutput.EchoLine(sb.ToString(),Name); + //AppendLineToResult(sb.ToString()); } else { - AppendLineToResult("No aliases found for command {0}", arguments[0]); + CmdParser.StdOutput.EchoLineFormat("No aliases found for command {0}", Name,arguments[0]); + //AppendLineToResult("No aliases found for command {0}", arguments[0]); } } else { - AppendLineToResult("Command {0} not found", arguments[0]); + CmdParser.StdOutput.EchoLineFormat("Command {0} not found", Name, arguments[0]); + //AppendLineToResult("Command {0} not found", arguments[0]); } } else //List all aliases { - AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); + CmdParser.StdOutput.EchoLineFormat("List of all {0} commands",Name, CmdParser.AvailableCommands.Count); + //AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); int commandIndex = 0; foreach (BaseCommand command in CmdParser.AvailableCommands) { @@ -74,14 +77,18 @@ public override string Execute(string[] arguments) else sb.AppendFormat("{0},", command.Aliases[i]); } - AppendLineToResult(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, sb.ToString())); + CmdParser.StdOutput.EchoLineFormat("{0:000}: {1} => {2}", Name, ++commandIndex, command.Name, sb.ToString()); + //CmdParser.StdOutput.EchoLine(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, sb.ToString()), Name); + //AppendLineToResult(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, sb.ToString())); } } + /* string output = CommandOutput; OnOutputAvailable(new Events.OutputAvailableEventArgs(Name, output)); return output; + */ } } } diff --git a/CommandsParser/Commands/EchoCommand.cs b/CommandsParser/Commands/EchoCommand.cs index ec8e83c..0a903a8 100644 --- a/CommandsParser/Commands/EchoCommand.cs +++ b/CommandsParser/Commands/EchoCommand.cs @@ -23,11 +23,11 @@ public override string[] Help } } - public override string Execute(string[] arguments) + public override void Execute(string[] arguments) { string output= string.Join(" ", arguments); base.CmdParser.StdOutput.EchoLine(output); - return output; + //return output; } } } diff --git a/CommandsParser/Commands/HelpCommand.cs b/CommandsParser/Commands/HelpCommand.cs index df79b99..87e820f 100644 --- a/CommandsParser/Commands/HelpCommand.cs +++ b/CommandsParser/Commands/HelpCommand.cs @@ -27,39 +27,47 @@ public override string[] Help } } - public override string Execute(string[] arguments) + public override void Execute(string[] arguments) { if (arguments.Length > 0) //Show help for arg[0] command { BaseCommand command = CmdParser.GetCommand(arguments[0]); if (command != null) { - AppendLineToResult("Displaying help for command {0}", arguments[0]); + CmdParser.StdOutput.EchoLineFormat("Displaying help for command {0}",Name, arguments[0]); + //AppendLineToResult("Displaying help for command {0}", arguments[0]); for (int i = 0; i < command.Help.Length; i++) { - AppendLineToResult(command.Help[i]); + CmdParser.StdOutput.EchoLine(command.Help[i]); + //AppendLineToResult(command.Help[i]); } } else { - AppendLineToResult("Command {0} not found", arguments[0]); + CmdParser.StdOutput.EchoLineFormat("Command {0} not found", Name, arguments[0]); + //AppendLineToResult("Command {0} not found", arguments[0]); } } else //List all commands { - AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); + CmdParser.StdOutput.EchoLineFormat("List of all {0} commands", Name, CmdParser.AvailableCommands.Count); + //AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); + int commandIndex = 0; foreach (BaseCommand command in CmdParser.AvailableCommands) { if (command != null) { - AppendLineToResult(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, command.Description)); + CmdParser.StdOutput.EchoLineFormat("{0:000}: {1} => {2}", Name, ++commandIndex, command.Name, command.Description); + //AppendLineToResult(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, command.Description)); } } } + /* string output = CommandOutput; OnOutputAvailable(new Events.OutputAvailableEventArgs(Name, output)); return output; + */ } } } \ No newline at end of file diff --git a/CommandsParser/StandardOutput.cs b/CommandsParser/StandardOutput.cs index 9bf6929..89c50a5 100644 --- a/CommandsParser/StandardOutput.cs +++ b/CommandsParser/StandardOutput.cs @@ -84,6 +84,10 @@ public void EchoFormat(string format, params object[] args)//Append (output) and { EchoFormat(format, DEFAULT_UNSPECIFIED_COMMAND, args); } + public void EchoLineFormat(string format, params object[] args)//Append (output) and Flush + { + EchoLineFormat(format, DEFAULT_UNSPECIFIED_COMMAND, args); + } public void Echo(string message, string commandSender)//Append (output) and Flush { @@ -100,6 +104,11 @@ public void EchoFormat(string format, string commandSender, params object[] args AppendOutputFormat(format, args); FlushOutput(commandSender); } + public void EchoLineFormat(string format, string commandSender, params object[] args)//Append (output) and Flush + { + AppendOutputFormat(format+Environment.NewLine, args); + FlushOutput(commandSender); + } public void FlushOutput(string commandSender) { string cmdResult = commandOutput.ToString(); diff --git a/CommandsParserSample/Commands/ClearConsoleCommand.cs b/CommandsParserSample/Commands/ClearConsoleCommand.cs index 299d07e..823dede 100644 --- a/CommandsParserSample/Commands/ClearConsoleCommand.cs +++ b/CommandsParserSample/Commands/ClearConsoleCommand.cs @@ -27,12 +27,12 @@ public override string[] Help } } - public override string Execute(string[] arguments) + public override void Execute(string[] arguments) { Console.Clear(); - string output = CommandOutput; - OnOutputAvailable(new CommandsParser.Events.OutputAvailableEventArgs(Name, output)); - return output; + //string output = CommandOutput; + //OnOutputAvailable(new CommandsParser.Events.OutputAvailableEventArgs(Name, output)); + //return output; } } diff --git a/CommandsParserSample/Commands/CloseCommand.cs b/CommandsParserSample/Commands/CloseCommand.cs index 50ffae5..96c9de1 100644 --- a/CommandsParserSample/Commands/CloseCommand.cs +++ b/CommandsParserSample/Commands/CloseCommand.cs @@ -26,12 +26,12 @@ public override string[] Help } } - public override string Execute(string[] arguments) + public override void Execute(string[] arguments) { Program.isRunning = false; - string output = CommandOutput; - OnOutputAvailable(new CommandsParser.Events.OutputAvailableEventArgs(Name, output)); - return output; + //string output = CommandOutput; + //OnOutputAvailable(new CommandsParser.Events.OutputAvailableEventArgs(Name, output)); + //return output; } } } diff --git a/CommandsParserSample/Program.cs b/CommandsParserSample/Program.cs index c8391c5..940b7dd 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -32,7 +32,7 @@ static void Main(string[] args) private static void StdOutput_OnOutputAvailable(object sender, CommandsParser.Events.OutputAvailableEventArgs e) { - Console.WriteLine(e.Output); + Console.Write(e.Output); } } From 947962fb7285f9109a6ff01ce0246b597adcd094 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 23 Apr 2018 16:58:39 -0300 Subject: [PATCH 06/14] code cleanup --- CommandsParser/BaseCommand.cs | 69 +------------------ CommandsParser/CmdParser.cs | 1 - CommandsParser/Commands/AliasCommand.cs | 13 ---- CommandsParser/Commands/EchoCommand.cs | 1 - CommandsParser/Commands/HelpCommand.cs | 10 --- .../Commands/ClearConsoleCommand.cs | 3 - CommandsParserSample/Commands/CloseCommand.cs | 3 - 7 files changed, 2 insertions(+), 98 deletions(-) diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index 0ff184a..03fc70c 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -13,62 +13,8 @@ public class BaseCommand private const string DEFAULT_COMMAND_DESCRIPTION = "NO_DESCRIPTION"; private readonly string[] DEFAULT_COMMAND_HELP = { "UNDEFINED_HELP"}; - //private StringBuilder commandOutput; - - //public delegate void OutputAvailableEventHandler(object sender, OutputAvailableEventArgs e); - //public event OutputAvailableEventHandler OutputAvailable; - protected CmdParser CmdParser; - /* - protected string CommandOutput - { - get - { - string commandResult = GetOutputBuffer(); - ClearOutputBuffer(); - return commandResult; - } - } - */ - /* - protected void AppendToResult(string message) - { - commandOutput.Append(message); - } - protected void AppendToResult(string messageFormat, params object[] args) - { - commandOutput.AppendFormat(messageFormat, args); - } - protected void AppendLineToResult() - { - commandOutput.AppendLine(); - } - protected void AppendLineToResult(string message) - { - commandOutput.AppendLine(message); - } - protected void AppendLineToResult(string message, params object[] args) - { - commandOutput.AppendLine(string.Format(message, args)); - } - - - protected void ClearOutputBuffer() - { - commandOutput.Clear(); - } - public string GetOutputBuffer() - { - if (commandOutput != null) - return commandOutput.ToString(); - else - { - commandOutput = new StringBuilder(); - return string.Empty; - } - } - */ public BaseCommand(CmdParser cmdParser, string name, string description, List aliases, string[] commandHelp) { this.CmdParser = cmdParser; @@ -117,18 +63,7 @@ public BaseCommand(CmdParser cmdParser, string name, string description, List /// public virtual void Execute(string[] arguments) - { - /* - string commandOutput = CommandOutput; - OnOutputAvailable(new OutputAvailableEventArgs(commandOutput, Name)); - return commandOutput; - */ - } - /* - public void OnOutputAvailable(OutputAvailableEventArgs eventArgs) - { - OutputAvailable?.Invoke(this, eventArgs ); - } - */ + {} + } } \ No newline at end of file diff --git a/CommandsParser/CmdParser.cs b/CommandsParser/CmdParser.cs index e0734ad..2271b96 100644 --- a/CommandsParser/CmdParser.cs +++ b/CommandsParser/CmdParser.cs @@ -118,7 +118,6 @@ public bool Execute(string input) string[] args = new string[splitInput.Length - 1]; Array.Copy(splitInput, 1, args, 0, args.Length); commandToExecute.Execute(args); - //stdOutput.Echo(commandToExecute.Execute(args), commandToExecute.Name); return true; } else diff --git a/CommandsParser/Commands/AliasCommand.cs b/CommandsParser/Commands/AliasCommand.cs index be7be87..194ccb8 100644 --- a/CommandsParser/Commands/AliasCommand.cs +++ b/CommandsParser/Commands/AliasCommand.cs @@ -45,25 +45,21 @@ public override void Execute(string[] arguments) sb.AppendFormat("{0},", command.Aliases[i]); } CmdParser.StdOutput.EchoLine(sb.ToString(),Name); - //AppendLineToResult(sb.ToString()); } else { CmdParser.StdOutput.EchoLineFormat("No aliases found for command {0}", Name,arguments[0]); - //AppendLineToResult("No aliases found for command {0}", arguments[0]); } } else { CmdParser.StdOutput.EchoLineFormat("Command {0} not found", Name, arguments[0]); - //AppendLineToResult("Command {0} not found", arguments[0]); } } else //List all aliases { CmdParser.StdOutput.EchoLineFormat("List of all {0} commands",Name, CmdParser.AvailableCommands.Count); - //AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); int commandIndex = 0; foreach (BaseCommand command in CmdParser.AvailableCommands) { @@ -78,17 +74,8 @@ public override void Execute(string[] arguments) sb.AppendFormat("{0},", command.Aliases[i]); } CmdParser.StdOutput.EchoLineFormat("{0:000}: {1} => {2}", Name, ++commandIndex, command.Name, sb.ToString()); - //CmdParser.StdOutput.EchoLine(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, sb.ToString()), Name); - //AppendLineToResult(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, sb.ToString())); - - } } - /* - string output = CommandOutput; - OnOutputAvailable(new Events.OutputAvailableEventArgs(Name, output)); - return output; - */ } } } diff --git a/CommandsParser/Commands/EchoCommand.cs b/CommandsParser/Commands/EchoCommand.cs index 0a903a8..376de0f 100644 --- a/CommandsParser/Commands/EchoCommand.cs +++ b/CommandsParser/Commands/EchoCommand.cs @@ -27,7 +27,6 @@ public override void Execute(string[] arguments) { string output= string.Join(" ", arguments); base.CmdParser.StdOutput.EchoLine(output); - //return output; } } } diff --git a/CommandsParser/Commands/HelpCommand.cs b/CommandsParser/Commands/HelpCommand.cs index 87e820f..df07ef4 100644 --- a/CommandsParser/Commands/HelpCommand.cs +++ b/CommandsParser/Commands/HelpCommand.cs @@ -35,23 +35,19 @@ public override void Execute(string[] arguments) if (command != null) { CmdParser.StdOutput.EchoLineFormat("Displaying help for command {0}",Name, arguments[0]); - //AppendLineToResult("Displaying help for command {0}", arguments[0]); for (int i = 0; i < command.Help.Length; i++) { CmdParser.StdOutput.EchoLine(command.Help[i]); - //AppendLineToResult(command.Help[i]); } } else { CmdParser.StdOutput.EchoLineFormat("Command {0} not found", Name, arguments[0]); - //AppendLineToResult("Command {0} not found", arguments[0]); } } else //List all commands { CmdParser.StdOutput.EchoLineFormat("List of all {0} commands", Name, CmdParser.AvailableCommands.Count); - //AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); int commandIndex = 0; foreach (BaseCommand command in CmdParser.AvailableCommands) @@ -59,15 +55,9 @@ public override void Execute(string[] arguments) if (command != null) { CmdParser.StdOutput.EchoLineFormat("{0:000}: {1} => {2}", Name, ++commandIndex, command.Name, command.Description); - //AppendLineToResult(string.Format("{0:000}: {1} => {2}", ++commandIndex, command.Name, command.Description)); } } } - /* - string output = CommandOutput; - OnOutputAvailable(new Events.OutputAvailableEventArgs(Name, output)); - return output; - */ } } } \ No newline at end of file diff --git a/CommandsParserSample/Commands/ClearConsoleCommand.cs b/CommandsParserSample/Commands/ClearConsoleCommand.cs index 823dede..373e3a8 100644 --- a/CommandsParserSample/Commands/ClearConsoleCommand.cs +++ b/CommandsParserSample/Commands/ClearConsoleCommand.cs @@ -30,9 +30,6 @@ public override string[] Help public override void Execute(string[] arguments) { Console.Clear(); - //string output = CommandOutput; - //OnOutputAvailable(new CommandsParser.Events.OutputAvailableEventArgs(Name, output)); - //return output; } } diff --git a/CommandsParserSample/Commands/CloseCommand.cs b/CommandsParserSample/Commands/CloseCommand.cs index 96c9de1..b142f5f 100644 --- a/CommandsParserSample/Commands/CloseCommand.cs +++ b/CommandsParserSample/Commands/CloseCommand.cs @@ -29,9 +29,6 @@ public override string[] Help public override void Execute(string[] arguments) { Program.isRunning = false; - //string output = CommandOutput; - //OnOutputAvailable(new CommandsParser.Events.OutputAvailableEventArgs(Name, output)); - //return output; } } } From 98250babd4d784282242d4bfd3a4e27b811d20f1 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 23 Apr 2018 17:45:57 -0300 Subject: [PATCH 07/14] Added command markers --- CommandsParser/CmdParser.cs | 7 +++++++ CommandsParserSample/Program.cs | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CommandsParser/CmdParser.cs b/CommandsParser/CmdParser.cs index 2271b96..6fd24d8 100644 --- a/CommandsParser/CmdParser.cs +++ b/CommandsParser/CmdParser.cs @@ -9,6 +9,9 @@ namespace CommandsParser { public class CmdParser { + public const string COMMAND_START_OUTPUT = ""; + public const string COMMAND_FINISH_OUTPUT = ""; + private Queue commandsHistory; private int maxCommandsInHistory = 3; @@ -117,7 +120,11 @@ public bool Execute(string input) { string[] args = new string[splitInput.Length - 1]; Array.Copy(splitInput, 1, args, 0, args.Length); + + StdOutput.EchoLine(COMMAND_START_OUTPUT, commandToExecute.Name); commandToExecute.Execute(args); + StdOutput.EchoLine(COMMAND_FINISH_OUTPUT, commandToExecute.Name); + return true; } else diff --git a/CommandsParserSample/Program.cs b/CommandsParserSample/Program.cs index 940b7dd..0c47889 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -32,7 +32,8 @@ static void Main(string[] args) private static void StdOutput_OnOutputAvailable(object sender, CommandsParser.Events.OutputAvailableEventArgs e) { - Console.Write(e.Output); + if (e.Output.Trim() != CmdParser.COMMAND_START_OUTPUT && e.Output.Trim() != CmdParser.COMMAND_FINISH_OUTPUT) + Console.Write(e.Output); } } From 344199c01c55713d3061bf7f4c61f7c6137aca36 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 23 Apr 2018 18:07:57 -0300 Subject: [PATCH 08/14] Re-Added commandExecuted events --- CommandsParser/BaseCommand.cs | 9 +++++++- CommandsParser/CmdParser.cs | 6 +++-- CommandsParser/Commands/AliasCommand.cs | 10 ++++---- CommandsParser/Commands/EchoCommand.cs | 2 +- CommandsParser/Commands/HelpCommand.cs | 10 ++++---- .../Events/CommandExecutedEventArgs.cs | 16 +++++++++++++ CommandsParser/StandardOutput.cs | 23 ++++++++++++++----- CommandsParserSample/Program.cs | 10 ++++++-- 8 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 CommandsParser/Events/CommandExecutedEventArgs.cs diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index 03fc70c..fbd2b0a 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -15,6 +15,9 @@ public class BaseCommand protected CmdParser CmdParser; + public delegate void CommandExecutedEventHandler(object sender, CommandExecutedEventArgs e); + public event CommandExecutedEventHandler CommandExecuted; + public BaseCommand(CmdParser cmdParser, string name, string description, List aliases, string[] commandHelp) { this.CmdParser = cmdParser; @@ -64,6 +67,10 @@ public BaseCommand(CmdParser cmdParser, string name, string description, List public virtual void Execute(string[] arguments) {} - + + public void OnCommandExecuted(CommandExecutedEventArgs eventArgs) + { + CommandExecuted?.Invoke(this, eventArgs); + } } } \ No newline at end of file diff --git a/CommandsParser/CmdParser.cs b/CommandsParser/CmdParser.cs index 6fd24d8..1386cd7 100644 --- a/CommandsParser/CmdParser.cs +++ b/CommandsParser/CmdParser.cs @@ -121,9 +121,11 @@ public bool Execute(string input) string[] args = new string[splitInput.Length - 1]; Array.Copy(splitInput, 1, args, 0, args.Length); - StdOutput.EchoLine(COMMAND_START_OUTPUT, commandToExecute.Name); + //StdOutput.EchoLine(COMMAND_START_OUTPUT, commandToExecute.Name); commandToExecute.Execute(args); - StdOutput.EchoLine(COMMAND_FINISH_OUTPUT, commandToExecute.Name); + commandToExecute?.OnCommandExecuted(new Events.CommandExecutedEventArgs(StdOutput.Output)); + stdOutput.FlushOutput(commandToExecute.Name); + //StdOutput.EchoLine(COMMAND_FINISH_OUTPUT, commandToExecute.Name); return true; } diff --git a/CommandsParser/Commands/AliasCommand.cs b/CommandsParser/Commands/AliasCommand.cs index 194ccb8..45db7c2 100644 --- a/CommandsParser/Commands/AliasCommand.cs +++ b/CommandsParser/Commands/AliasCommand.cs @@ -44,22 +44,22 @@ public override void Execute(string[] arguments) else sb.AppendFormat("{0},", command.Aliases[i]); } - CmdParser.StdOutput.EchoLine(sb.ToString(),Name); + CmdParser.StdOutput.AppendOutputLine(sb.ToString()); } else { - CmdParser.StdOutput.EchoLineFormat("No aliases found for command {0}", Name,arguments[0]); + CmdParser.StdOutput.AppendOutputLineFormat("No aliases found for command {0}",arguments[0]); } } else { - CmdParser.StdOutput.EchoLineFormat("Command {0} not found", Name, arguments[0]); + CmdParser.StdOutput.AppendOutputLineFormat("Command {0} not found", arguments[0]); } } else //List all aliases { - CmdParser.StdOutput.EchoLineFormat("List of all {0} commands",Name, CmdParser.AvailableCommands.Count); + CmdParser.StdOutput.AppendOutputLineFormat("List of all {0} commands", CmdParser.AvailableCommands.Count); int commandIndex = 0; foreach (BaseCommand command in CmdParser.AvailableCommands) { @@ -73,7 +73,7 @@ public override void Execute(string[] arguments) else sb.AppendFormat("{0},", command.Aliases[i]); } - CmdParser.StdOutput.EchoLineFormat("{0:000}: {1} => {2}", Name, ++commandIndex, command.Name, sb.ToString()); + CmdParser.StdOutput.AppendOutputLineFormat("{0:000}: {1} => {2}", ++commandIndex, command.Name, sb.ToString()); } } } diff --git a/CommandsParser/Commands/EchoCommand.cs b/CommandsParser/Commands/EchoCommand.cs index 376de0f..d64200a 100644 --- a/CommandsParser/Commands/EchoCommand.cs +++ b/CommandsParser/Commands/EchoCommand.cs @@ -26,7 +26,7 @@ public override string[] Help public override void Execute(string[] arguments) { string output= string.Join(" ", arguments); - base.CmdParser.StdOutput.EchoLine(output); + base.CmdParser.StdOutput.AppendOutputLine(output); } } } diff --git a/CommandsParser/Commands/HelpCommand.cs b/CommandsParser/Commands/HelpCommand.cs index df07ef4..2d18ba6 100644 --- a/CommandsParser/Commands/HelpCommand.cs +++ b/CommandsParser/Commands/HelpCommand.cs @@ -34,27 +34,27 @@ public override void Execute(string[] arguments) BaseCommand command = CmdParser.GetCommand(arguments[0]); if (command != null) { - CmdParser.StdOutput.EchoLineFormat("Displaying help for command {0}",Name, arguments[0]); + CmdParser.StdOutput.AppendOutputLineFormat("Displaying help for command {0}", arguments[0]); for (int i = 0; i < command.Help.Length; i++) { - CmdParser.StdOutput.EchoLine(command.Help[i]); + CmdParser.StdOutput.AppendOutputLine(command.Help[i]); } } else { - CmdParser.StdOutput.EchoLineFormat("Command {0} not found", Name, arguments[0]); + CmdParser.StdOutput.AppendOutputLineFormat("Command {0} not found", arguments[0]); } } else //List all commands { - CmdParser.StdOutput.EchoLineFormat("List of all {0} commands", Name, CmdParser.AvailableCommands.Count); + CmdParser.StdOutput.AppendOutputLineFormat("List of all {0} commands", CmdParser.AvailableCommands.Count); int commandIndex = 0; foreach (BaseCommand command in CmdParser.AvailableCommands) { if (command != null) { - CmdParser.StdOutput.EchoLineFormat("{0:000}: {1} => {2}", Name, ++commandIndex, command.Name, command.Description); + CmdParser.StdOutput.AppendOutputLineFormat("{0:000}: {1} => {2}", ++commandIndex, command.Name, command.Description); } } } diff --git a/CommandsParser/Events/CommandExecutedEventArgs.cs b/CommandsParser/Events/CommandExecutedEventArgs.cs new file mode 100644 index 0000000..8309f68 --- /dev/null +++ b/CommandsParser/Events/CommandExecutedEventArgs.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CommandsParser.Events +{ + public class CommandExecutedEventArgs:EventArgs + { + public string Output { get; private set; } + + public CommandExecutedEventArgs(string output) + { + Output = output; + } + } +} diff --git a/CommandsParser/StandardOutput.cs b/CommandsParser/StandardOutput.cs index 89c50a5..0427897 100644 --- a/CommandsParser/StandardOutput.cs +++ b/CommandsParser/StandardOutput.cs @@ -37,40 +37,51 @@ public string OutputPrefix set { this.outputPrefix = value; } } - private void AppendOutput(string message) + public void AppendOutput(string message) { AppendOutput(message, false); } - private void AppendOutputLine(string message) + public void AppendOutputLine(string message) { AppendOutputLine(message, false); } - private void AppendOutputFormat(string messageFormat, params object[] args) + public void AppendOutputFormat(string messageFormat, params object[] args) { AppendOutputFormat(messageFormat, false, args); } + public void AppendOutputLineFormat(string messageFormat, params object[] args) + { + AppendOutputLineFormat(messageFormat, false, args); + } - private void AppendOutput(string message, bool appendPrefix) + public void AppendOutput(string message, bool appendPrefix) { if (appendPrefix) commandOutput.Append(OutputPrefix + message); else commandOutput.Append(message); } - private void AppendOutputLine(string message, bool appendPrefix) + public void AppendOutputLine(string message, bool appendPrefix) { if (appendPrefix) commandOutput.AppendLine(OutputPrefix + message); else commandOutput.AppendLine(message); } - private void AppendOutputFormat(string messageFormat, bool appendPrefix, params object[] args) + public void AppendOutputFormat(string messageFormat, bool appendPrefix, params object[] args) { if (appendPrefix) commandOutput.AppendFormat(OutputPrefix + messageFormat, args); else commandOutput.AppendFormat(messageFormat, args); } + public void AppendOutputLineFormat(string messageFormat, bool appendPrefix, params object[] args) + { + if (appendPrefix) + commandOutput.AppendFormat(OutputPrefix + messageFormat + Environment.NewLine, args); + else + commandOutput.AppendFormat(messageFormat + Environment.NewLine, args); + } public void Echo(string message)//Append (output) and Flush { diff --git a/CommandsParserSample/Program.cs b/CommandsParserSample/Program.cs index 0c47889..04296dd 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -15,11 +15,12 @@ class Program static CmdParser cmdParser = new CmdParser(); static void Main(string[] args) { - - cmdParser.AddCommand(new ClearConsoleCommand(cmdParser)); + ClearConsoleCommand clsCommand = new ClearConsoleCommand(cmdParser); + cmdParser.AddCommand(clsCommand); cmdParser.AddCommand(new CloseCommand(cmdParser)); cmdParser.OnOutputAvailable += StdOutput_OnOutputAvailable; + clsCommand.CommandExecuted += ClsCommand_CommandExecuted; isRunning = true; @@ -30,6 +31,11 @@ static void Main(string[] args) } } + private static void ClsCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) + { + Console.WriteLine("output cleared!!!"); + } + private static void StdOutput_OnOutputAvailable(object sender, CommandsParser.Events.OutputAvailableEventArgs e) { if (e.Output.Trim() != CmdParser.COMMAND_START_OUTPUT && e.Output.Trim() != CmdParser.COMMAND_FINISH_OUTPUT) From 43a52e3d50d6a34b906636dfc1d9f527261275b4 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 23 Apr 2018 18:10:21 -0300 Subject: [PATCH 09/14] version number bumped --- CommandsParser/CommandsParser.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CommandsParser/CommandsParser.csproj b/CommandsParser/CommandsParser.csproj index 5c32b57..ea6a8dd 100644 --- a/CommandsParser/CommandsParser.csproj +++ b/CommandsParser/CommandsParser.csproj @@ -2,7 +2,7 @@ netstandard1.0 - 0.4.1 + 0.5.0 From 186ca256a727219629a29ff62f08e7ec89dedc4b Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 23 Apr 2018 18:12:27 -0300 Subject: [PATCH 10/14] Added release package --- ReleasePackages/CommandParser.0.5.0.nuspec | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 ReleasePackages/CommandParser.0.5.0.nuspec diff --git a/ReleasePackages/CommandParser.0.5.0.nuspec b/ReleasePackages/CommandParser.0.5.0.nuspec new file mode 100644 index 0000000..c3787c5 --- /dev/null +++ b/ReleasePackages/CommandParser.0.5.0.nuspec @@ -0,0 +1,18 @@ + + + + CommandParser + 0.5.0 + CommandParser + Jimmy Toledo + Jimmy Toledo + https://github.com/Jim-Dev/CommandLineParser/blob/master/LICENSE + https://github.com/Jim-Dev/CommandLineParser + false + CommandLineParser as the name suggests, is a commands parser, maps a string as a command name and/or a set of aliases to an action to be excecuted, in the same fashion that a terminal/console works. +Now compiled to target NetStandard 1.0! + + + + + \ No newline at end of file From 7327d6920789505c5dbe400cfb54413002dbfdca Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Sat, 14 Jul 2018 22:10:02 -0400 Subject: [PATCH 11/14] Added mutable commands, added command execution events, added new test commands on sample project --- CommandsParser/BaseCommand.cs | 66 +++++++++++++ CommandsParser/CmdParser.cs | 8 +- CommandsParser/Commands/EchoCommand.cs | 2 +- CommandsParserSample/Commands/TextCommand.cs | 92 +++++++++++++++++++ .../Commands/TickTockCommand.cs | 77 ++++++++++++++++ .../Commands/UppercaseCommand.cs | 43 +++++++++ .../CommandsParserSample.csproj | 3 + CommandsParserSample/Program.cs | 28 ++++++ 8 files changed, 317 insertions(+), 2 deletions(-) create mode 100644 CommandsParserSample/Commands/TextCommand.cs create mode 100644 CommandsParserSample/Commands/TickTockCommand.cs create mode 100644 CommandsParserSample/Commands/UppercaseCommand.cs diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index fbd2b0a..4ac204b 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -17,6 +17,65 @@ public class BaseCommand public delegate void CommandExecutedEventHandler(object sender, CommandExecutedEventArgs e); public event CommandExecutedEventHandler CommandExecuted; + public event CommandExecutedEventHandler CommandExecuting; + + private StringBuilder commandOutput; + + private bool isMuted = false; + + public string Output + { + get + { + if (commandOutput != null) + { + string output = commandOutput.ToString(); + ClearOutput(); + return output; + } + else + { + commandOutput = new StringBuilder(); + return string.Empty; + } + } + } + + public bool IsMuted + { + get { return this.isMuted; } + protected set { this.isMuted = value; } + } + public void AppendOutput(string message) + { + commandOutput.Append(message); + if(!IsMuted) + CmdParser.StdOutput.AppendOutput(message); + } + public void AppendOutputLine(string message) + { + commandOutput.AppendLine(message); + if (!IsMuted) + CmdParser.StdOutput.AppendOutputLine(message); + } + public void AppendOutputFormat(string messageFormat, params object[] args) + { + commandOutput.AppendFormat(messageFormat, args); + if (!IsMuted) + CmdParser.StdOutput.AppendOutputFormat(messageFormat, args); + } + public void AppendOutputLineFormat(string messageFormat, params object[] args) + { + commandOutput.AppendFormat(messageFormat+Environment.NewLine, args); + if (!IsMuted) + CmdParser.StdOutput.AppendOutputLineFormat(messageFormat, args); + } + private void ClearOutput() + { + commandOutput.Clear(); + } + + public BaseCommand(CmdParser cmdParser, string name, string description, List aliases, string[] commandHelp) { @@ -41,6 +100,8 @@ public BaseCommand(CmdParser cmdParser, string name, string description, List /// Removes all commands. /// diff --git a/CommandsParser/Commands/EchoCommand.cs b/CommandsParser/Commands/EchoCommand.cs index d64200a..79a3d9b 100644 --- a/CommandsParser/Commands/EchoCommand.cs +++ b/CommandsParser/Commands/EchoCommand.cs @@ -26,7 +26,7 @@ public override string[] Help public override void Execute(string[] arguments) { string output= string.Join(" ", arguments); - base.CmdParser.StdOutput.AppendOutputLine(output); + base.CmdParser.StdOutput.Echo(output); } } } diff --git a/CommandsParserSample/Commands/TextCommand.cs b/CommandsParserSample/Commands/TextCommand.cs new file mode 100644 index 0000000..015e409 --- /dev/null +++ b/CommandsParserSample/Commands/TextCommand.cs @@ -0,0 +1,92 @@ +using CommandsParser; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CommandsParserSample.Commands +{ + public class TextCommand:BaseCommand + { + private string[] textLines = new string[] + { + + "Lorem ipsum dolor sit, amet consectetur adipiscing, elit eros.", + "Aenean faucibus congue eu vel lectus pretium, ultrices tempor mattis nec.", + "Scelerisque sociosqu sem curabitur egestas, habitasse curae.", + "Ullamcorper sodales cum accumsan egestas scelerisque, congue libero quam nullam.", + "Magna congue lacinia netus dapibus non, lectus quis accumsan.", + "Laoreet lobortis ornare facilisis nascetur id, tempor venenatis egestas.", + "Semper netus justo posuere mattis, dictum et velit.", + "Phasellus sed ad vestibulum suscipit diam, hendrerit lacus natoque.", + "Placerat donec feugiat sociosqu eleifend mollis natoque, vivamus libero nec litora.", + "Diam cras ante lobortis id suscipit, congue cubilia litora.", + "Magnis feugiat commodo tellus blandit inceptos, quis auctor massa.", + "Pellentesque urna torquent felis, morbi integer maecenas, mus ultricies.", + "Eu montes pulvinar congue, quam malesuada at phasellus, platea ad.", + "Et per justo auctor lacinia, natoque at conubia.", + "Fames metus fringilla imperdiet etiam, semper conubia natoque.", + "Ante erat viverra feugiat inceptos bibendum, fusce ridiculus lobortis cubilia.", + "Lacinia tempus quam posuere nascetur, mus nulla.", + "Nascetur mauris netus risus inceptos, ut libero.", + "Elementum porta ut eget dictumst, per sociis.", + "Fringilla felis ligula mus fusce, nostra ridiculus malesuada.", + "Vitae lectus justo quisque, iaculis habitant turpis odio, ullamcorper torquent.", + "Nostra habitant elementum magnis id, risus pellentesque orci.", + "Auctor nullam posuere natoque sollicitudin nec, class eleifend eget.", + "Pharetra interdum phasellus ac metus, tristique natoque.", + "Malesuada ad egestas suspendisse, eros vivamus rutrum erat, cubilia ligula.", + "Inceptos nascetur viverra turpis donec potenti, rutrum risus pellentesque class.", + "Potenti rutrum duis torquent ut parturient, gravida et sem.", + "Duis tempus morbi ultrices leo, hendrerit ut vehicula.", + "Condimentum proin facilisi at in molestie, leo quis mollis egestas.", + "Ornare nam quis augue, lacus sem vulputate, sociis interdum.", + "Rutrum nisi vehicula venenatis in, torquent elementum.", + "Nulla litora justo semper vel sodales, euismod eleifend faucibus ridiculus." + }; + private Random rnd; + + public TextCommand(CmdParser cmdParser) + : base(cmdParser, + "text", + "Echoes a series of text lines", + new List() { "cls" }, + null) + { + rnd = new Random(); + IsMuted = true; + } + + public override string[] Help + { + get + { + return new string[] { + "Clears the console"}; + } + } + + public override void Execute(string[] arguments) + { + if (arguments.Length == 0) + AppendOutputLine(GetRandomString()); + else + { + int lineCount = int.Parse(arguments[0]); + for (int i = 0; i < lineCount; i++) + { + AppendOutputLine(GetRandomString()); + } + } + OnCommandExecuted(); + } + + private string GetRandomString() + { + int rndIndex = rnd.Next(0, textLines.Length); + + return textLines[rndIndex]; + } + } +} diff --git a/CommandsParserSample/Commands/TickTockCommand.cs b/CommandsParserSample/Commands/TickTockCommand.cs new file mode 100644 index 0000000..f9c8f22 --- /dev/null +++ b/CommandsParserSample/Commands/TickTockCommand.cs @@ -0,0 +1,77 @@ +using CommandsParser; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace CommandsParserSample.Commands +{ + public class TickTockCommand : BaseCommand + { + private int totalCycleCount = 0; + private int totalCycleLenght = 0; + private int cyclesCompleted = 0; + + private const int DEFAULT_CYCLES_COUNT = 3; + private const int DEFAULT_CYCLE_LENGHT = 500; + + private object lockObject; + Timer timer; + public TickTockCommand(CmdParser cmdParser) + : base(cmdParser, + "tick", + "Clears the console", + new List(), + null) + { } + + public override string[] Help + { + get + { + return new string[] { + "Clears the console"}; + } + } + + public override void Execute(string[] arguments) + { + if (arguments.Length>0) + { + if (arguments.Length>=2) + { + totalCycleCount = int.Parse(arguments[0]); + totalCycleLenght = int.Parse(arguments[1]); + } + } + else + { + totalCycleCount = DEFAULT_CYCLES_COUNT; + totalCycleLenght = DEFAULT_CYCLE_LENGHT; + } + timer = new Timer(TimerCallback, null, totalCycleLenght, Timeout.Infinite); + CmdParser.StdOutput.EchoLineFormat("cycle duration : {0}, cycles: {1}", totalCycleLenght, totalCycleCount); + + + } + + private void TimerCallback(Object stateInfo) + { + cyclesCompleted++; + if (cyclesCompleted <= totalCycleCount) + { + CmdParser.StdOutput.EchoLine("tick"); + timer.Change(totalCycleLenght, Timeout.Infinite); + + } + else + { + AppendOutputLine("tock"); + OnCommandExecuted(); + + } + } + } +} \ No newline at end of file diff --git a/CommandsParserSample/Commands/UppercaseCommand.cs b/CommandsParserSample/Commands/UppercaseCommand.cs new file mode 100644 index 0000000..04a6f92 --- /dev/null +++ b/CommandsParserSample/Commands/UppercaseCommand.cs @@ -0,0 +1,43 @@ +using CommandsParser; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CommandsParserSample.Commands +{ + public class UppercaseCommand:BaseCommand + { + public UppercaseCommand(CmdParser cmdParser) + : base(cmdParser, + "uppercase", + "Clears the console", + new List() { "upper" }, + null) + { + IsMuted = true; + } + + public override string[] Help + { + get + { + return new string[] { + "Clears the console"}; + } + } + + public override void Execute(string[] arguments) + { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < arguments.Length; i++) + { + sb.AppendFormat("{0} ", arguments[i].ToUpper()); + } + AppendOutputLine(sb.ToString()); + OnCommandExecuted(); + } + } +} diff --git a/CommandsParserSample/CommandsParserSample.csproj b/CommandsParserSample/CommandsParserSample.csproj index 7a6a566..547613c 100644 --- a/CommandsParserSample/CommandsParserSample.csproj +++ b/CommandsParserSample/CommandsParserSample.csproj @@ -46,6 +46,9 @@ + + + diff --git a/CommandsParserSample/Program.cs b/CommandsParserSample/Program.cs index 04296dd..518dd5b 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -16,11 +16,21 @@ class Program static void Main(string[] args) { ClearConsoleCommand clsCommand = new ClearConsoleCommand(cmdParser); + TextCommand textCommand = new TextCommand(cmdParser); + UppercaseCommand uppercaseCommand = new UppercaseCommand(cmdParser); + TickTockCommand tickTockCommand = new TickTockCommand(cmdParser); + cmdParser.AddCommand(clsCommand); cmdParser.AddCommand(new CloseCommand(cmdParser)); + cmdParser.AddCommand(textCommand); + cmdParser.AddCommand(uppercaseCommand); + cmdParser.AddCommand(tickTockCommand); cmdParser.OnOutputAvailable += StdOutput_OnOutputAvailable; clsCommand.CommandExecuted += ClsCommand_CommandExecuted; + textCommand.CommandExecuted += TextCommand_CommandExecuted; + uppercaseCommand.CommandExecuted += UppercaseCommand_CommandExecuted; + tickTockCommand.CommandExecuted += TickTockCommand_CommandExecuted; isRunning = true; @@ -31,6 +41,22 @@ static void Main(string[] args) } } + private static void TickTockCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) + { + Console.WriteLine(e.Output); + } + + private static void UppercaseCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) + { + Console.Write(e.Output); + } + + private static void TextCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) + { + cmdParser.Execute(string.Format("upper {0}", e.Output)); + + } + private static void ClsCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) { Console.WriteLine("output cleared!!!"); @@ -38,8 +64,10 @@ private static void ClsCommand_CommandExecuted(object sender, CommandsParser.Eve private static void StdOutput_OnOutputAvailable(object sender, CommandsParser.Events.OutputAvailableEventArgs e) { + if (e.Output.Trim() != CmdParser.COMMAND_START_OUTPUT && e.Output.Trim() != CmdParser.COMMAND_FINISH_OUTPUT) Console.Write(e.Output); + } } From ed6fd969b58170ba75029801c2068a0d92665a85 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Sat, 14 Jul 2018 22:32:46 -0400 Subject: [PATCH 12/14] Fixed tick tock command output error --- CommandsParser/BaseCommand.cs | 11 +++++++- .../Events/CommandExecutedEventArgs.cs | 16 ------------ .../Events/CommandExecutionEventArgs.cs | 25 +++++++++++++++++++ .../Commands/TickTockCommand.cs | 3 ++- 4 files changed, 37 insertions(+), 18 deletions(-) delete mode 100644 CommandsParser/Events/CommandExecutedEventArgs.cs create mode 100644 CommandsParser/Events/CommandExecutionEventArgs.cs diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index 4ac204b..ac17c0d 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -16,8 +16,9 @@ public class BaseCommand protected CmdParser CmdParser; public delegate void CommandExecutedEventHandler(object sender, CommandExecutedEventArgs e); + public delegate void CommandExecutingEventHandler(object sender, CommandExecutingEventArgs e); public event CommandExecutedEventHandler CommandExecuted; - public event CommandExecutedEventHandler CommandExecuting; + public event CommandExecutingEventHandler CommandExecuting; private StringBuilder commandOutput; @@ -138,5 +139,13 @@ public void OnCommandExecuted(CommandExecutedEventArgs eventArgs) { CommandExecuted?.Invoke(this, eventArgs); } + public void OnCommandExecuting(string[] arguments) + { + OnCommandExecuting(new CommandExecutingEventArgs(arguments)); + } + public void OnCommandExecuting(CommandExecutingEventArgs eventArgs) + { + CommandExecuting?.Invoke(this, eventArgs); + } } } \ No newline at end of file diff --git a/CommandsParser/Events/CommandExecutedEventArgs.cs b/CommandsParser/Events/CommandExecutedEventArgs.cs deleted file mode 100644 index 8309f68..0000000 --- a/CommandsParser/Events/CommandExecutedEventArgs.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CommandsParser.Events -{ - public class CommandExecutedEventArgs:EventArgs - { - public string Output { get; private set; } - - public CommandExecutedEventArgs(string output) - { - Output = output; - } - } -} diff --git a/CommandsParser/Events/CommandExecutionEventArgs.cs b/CommandsParser/Events/CommandExecutionEventArgs.cs new file mode 100644 index 0000000..220f919 --- /dev/null +++ b/CommandsParser/Events/CommandExecutionEventArgs.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CommandsParser.Events +{ + public class CommandExecutedEventArgs : EventArgs + { + public string Output { get; private set; } + + public CommandExecutedEventArgs(string output) + { + Output = output; + } + } + public class CommandExecutingEventArgs : EventArgs + { + public string[] Arguments { get; private set; } + + public CommandExecutingEventArgs(string[] arguments) + { + Arguments = arguments; + } + } +} diff --git a/CommandsParserSample/Commands/TickTockCommand.cs b/CommandsParserSample/Commands/TickTockCommand.cs index f9c8f22..6b8469e 100644 --- a/CommandsParserSample/Commands/TickTockCommand.cs +++ b/CommandsParserSample/Commands/TickTockCommand.cs @@ -68,7 +68,8 @@ private void TimerCallback(Object stateInfo) } else { - AppendOutputLine("tock"); + CmdParser.StdOutput.EchoLine("tock"); + cyclesCompleted = 0; OnCommandExecuted(); } From af02790867220b7b63891ac4f1d04d17a156ae27 Mon Sep 17 00:00:00 2001 From: Jimmy Toledo Date: Mon, 22 Apr 2019 00:25:24 -0300 Subject: [PATCH 13/14] Added Command execution events and Commands cancelation --- CommandsParser/BaseCommand.cs | 20 +++++++++++-- .../Events/CommandExecutionEventArgs.cs | 30 +++++++++++++++++-- .../Commands/ClearConsoleCommand.cs | 9 +++++- CommandsParserSample/Commands/TextCommand.cs | 4 +-- .../Commands/TickTockCommand.cs | 6 ++-- .../Commands/UppercaseCommand.cs | 6 ++-- CommandsParserSample/Program.cs | 21 ++++++++++++- 7 files changed, 81 insertions(+), 15 deletions(-) diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index ac17c0d..cf029a7 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -24,6 +24,14 @@ public class BaseCommand private bool isMuted = false; + private bool cancel = false; + + public bool Cancel + { + get { return this.cancel; } + set { this.cancel = value; } + } + public string Output { get @@ -42,6 +50,12 @@ public string Output } } + internal void CancelCommandExecution() + { + Cancel = true; + AppendOutputLine("Command canceled!"); + } + public bool IsMuted { get { return this.isMuted; } @@ -130,10 +144,10 @@ public BaseCommand(CmdParser cmdParser, string name, string description, List(), null) { } @@ -32,7 +32,7 @@ public override string[] Help get { return new string[] { - "Clears the console"}; + "Example of a delayed command"}; } } @@ -70,7 +70,7 @@ private void TimerCallback(Object stateInfo) { CmdParser.StdOutput.EchoLine("tock"); cyclesCompleted = 0; - OnCommandExecuted(); + OnCommandExecuted(new string[] { }); } } diff --git a/CommandsParserSample/Commands/UppercaseCommand.cs b/CommandsParserSample/Commands/UppercaseCommand.cs index 04a6f92..f25ff75 100644 --- a/CommandsParserSample/Commands/UppercaseCommand.cs +++ b/CommandsParserSample/Commands/UppercaseCommand.cs @@ -12,7 +12,7 @@ public class UppercaseCommand:BaseCommand public UppercaseCommand(CmdParser cmdParser) : base(cmdParser, "uppercase", - "Clears the console", + "Makes all the text in the arguments UPPERCASE", new List() { "upper" }, null) { @@ -24,7 +24,7 @@ public override string[] Help get { return new string[] { - "Clears the console"}; + "Makes all the text in the arguments UPPERCASE"}; } } @@ -37,7 +37,7 @@ public override void Execute(string[] arguments) sb.AppendFormat("{0} ", arguments[i].ToUpper()); } AppendOutputLine(sb.ToString()); - OnCommandExecuted(); + OnCommandExecuted(arguments); } } } diff --git a/CommandsParserSample/Program.cs b/CommandsParserSample/Program.cs index 518dd5b..160eece 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -27,8 +27,12 @@ static void Main(string[] args) cmdParser.AddCommand(tickTockCommand); cmdParser.OnOutputAvailable += StdOutput_OnOutputAvailable; + + clsCommand.CommandExecuting += ClsCommand_CommandExecuting; + clsCommand.CommandExecuted += ClsCommand_CommandExecuted; textCommand.CommandExecuted += TextCommand_CommandExecuted; + textCommand.CommandExecuting += TextCommand_CommandExecuting; uppercaseCommand.CommandExecuted += UppercaseCommand_CommandExecuted; tickTockCommand.CommandExecuted += TickTockCommand_CommandExecuted; @@ -41,6 +45,18 @@ static void Main(string[] args) } } + private static void TextCommand_CommandExecuting(object sender, CommandsParser.Events.CommandExecutingEventArgs e) + { + + } + + private static void ClsCommand_CommandExecuting(object sender, CommandsParser.Events.CommandExecutingEventArgs e) + { + Console.WriteLine("Calling cls with args {0}", e.MergedArgs); + if (e.Arguments.Length > 0) + e.Cancel = true; + } + private static void TickTockCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) { Console.WriteLine(e.Output); @@ -53,7 +69,10 @@ private static void UppercaseCommand_CommandExecuted(object sender, CommandsPars private static void TextCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) { - cmdParser.Execute(string.Format("upper {0}", e.Output)); + if (e.Arguments.Length > 0 && e.Arguments[0] == "upper") + cmdParser.Execute(string.Format("upper {0}", e.Output)); + else + Console.WriteLine(e.Output); } From 9c0a457f40223f939a6db05ac3fd4b6b9ab49467 Mon Sep 17 00:00:00 2001 From: JToledo Date: Mon, 10 Jun 2019 22:46:43 -0400 Subject: [PATCH 14/14] Implemented template for command execution (RunCommand) on BaseCommand, Implemented History Command on CmdParser) --- CommandsParser/BaseCommand.cs | 13 +++---- CommandsParser/CmdParser.cs | 32 ++++++++++------- CommandsParser/Commands/AliasCommand.cs | 1 - CommandsParser/Commands/EchoCommand.cs | 1 - CommandsParser/Commands/HelpCommand.cs | 3 +- CommandsParser/Commands/HistoryCommand.cs | 35 +++++++++++++++++++ .../Commands/ClearConsoleCommand.cs | 5 +-- CommandsParserSample/Commands/CloseCommand.cs | 3 +- CommandsParserSample/Commands/TextCommand.cs | 4 +-- .../Commands/TickTockCommand.cs | 1 - .../Commands/UppercaseCommand.cs | 4 +-- CommandsParserSample/Program.cs | 2 +- 12 files changed, 67 insertions(+), 37 deletions(-) create mode 100644 CommandsParser/Commands/HistoryCommand.cs diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index cf029a7..afda48d 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -90,9 +90,15 @@ private void ClearOutput() commandOutput.Clear(); } + public void RunCommand(string[] arguments) + { + OnCommandExecuting(arguments); + Execute(arguments); + OnCommandExecuted(arguments); + } - public BaseCommand(CmdParser cmdParser, string name, string description, List aliases, string[] commandHelp) + public BaseCommand(CmdParser cmdParser, string name, string description, List aliases) { this.CmdParser = cmdParser; @@ -111,11 +117,6 @@ public BaseCommand(CmdParser cmdParser, string name, string description, List(); - if (commandHelp != null && commandHelp.Length > 0) - Help = commandHelp; - else - Help = DEFAULT_COMMAND_HELP; - commandOutput = new StringBuilder(); } diff --git a/CommandsParser/CmdParser.cs b/CommandsParser/CmdParser.cs index cba0e54..c58ff88 100644 --- a/CommandsParser/CmdParser.cs +++ b/CommandsParser/CmdParser.cs @@ -12,8 +12,8 @@ public class CmdParser public const string COMMAND_START_OUTPUT = ""; public const string COMMAND_FINISH_OUTPUT = ""; - private Queue commandsHistory; - private int maxCommandsInHistory = 3; + private Queue executedCommandsHistory; + private int maxCommandsInHistory = 32; public event OutputAvailableEventHandler OnOutputAvailable; @@ -21,6 +21,13 @@ public class CmdParser internal static List AvailableCommands; + public bool UseAutomaticOutput = false; + + public string[] CommandsHistory + { + get { return this.executedCommandsHistory.ToArray(); } + } + public StandardOutput StdOutput { get { return stdOutput; } @@ -33,7 +40,7 @@ public CmdParser() AvailableCommands = new List(); - commandsHistory = new Queue(); + executedCommandsHistory = new Queue(); AddDefaultCommands(); } @@ -80,6 +87,7 @@ private void AddDefaultCommands() AddCommand(new Commands.HelpCommand(this)); AddCommand(new Commands.AliasCommand(this)); AddCommand(new Commands.EchoCommand(this)); + AddCommand(new Commands.HistoryCommand(this)); } public void AddCommand(BaseCommand command) @@ -108,25 +116,27 @@ public bool Execute(string input) { return false; } - + string[] splitInput = input.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (splitInput.Length > 0) { string commandName = splitInput[0]; + string joinedArguments = string.Join(" ", splitInput.Skip(1)); + BaseCommand commandToExecute = GetCommand(splitInput[0]); if (commandToExecute != null) { string[] args = new string[splitInput.Length - 1]; Array.Copy(splitInput, 1, args, 0, args.Length); - //StdOutput.EchoLine(COMMAND_START_OUTPUT, commandToExecute.Name); - //commandToExecute.CommandExecuted += CommandToExecute_CommandExecuted; - commandToExecute.Execute(args); - //commandToExecute?.OnCommandExecuted(new Events.CommandExecutedEventArgs(commandToExecute.Output)); + executedCommandsHistory.Enqueue(string.Format("{0} {1}", commandToExecute.Name, joinedArguments)); + if (executedCommandsHistory.Count > maxCommandsInHistory) + executedCommandsHistory.Dequeue(); + + commandToExecute.RunCommand(args); stdOutput.FlushOutput(commandToExecute.Name); - //StdOutput.EchoLine(COMMAND_FINISH_OUTPUT, commandToExecute.Name); return true; } @@ -137,10 +147,6 @@ public bool Execute(string input) return false; } - private void CommandToExecute_CommandExecuted(object sender, Events.CommandExecutedEventArgs e) - { - //StdOutput.AppendOutputLine(e.Output); - } /// /// Removes all commands. diff --git a/CommandsParser/Commands/AliasCommand.cs b/CommandsParser/Commands/AliasCommand.cs index 45db7c2..f2c50bb 100644 --- a/CommandsParser/Commands/AliasCommand.cs +++ b/CommandsParser/Commands/AliasCommand.cs @@ -12,7 +12,6 @@ public AliasCommand(CmdParser cmdParser) : base(cmdParser, "alias", "Display the list of all aliases for a given command or show if a command is an alias of another command", - null, null) { } diff --git a/CommandsParser/Commands/EchoCommand.cs b/CommandsParser/Commands/EchoCommand.cs index 79a3d9b..beb04c0 100644 --- a/CommandsParser/Commands/EchoCommand.cs +++ b/CommandsParser/Commands/EchoCommand.cs @@ -10,7 +10,6 @@ public EchoCommand(CmdParser cmdParser) : base(cmdParser, "echo", "Echoes a message to the active Output", - null, null) { } diff --git a/CommandsParser/Commands/HelpCommand.cs b/CommandsParser/Commands/HelpCommand.cs index 2d18ba6..73184d8 100644 --- a/CommandsParser/Commands/HelpCommand.cs +++ b/CommandsParser/Commands/HelpCommand.cs @@ -12,8 +12,7 @@ public HelpCommand(CmdParser cmdParser) : base(cmdParser, "help", "Displays the list of commands, or the help file for a specified command", - new List() { "man" }, - null) + new List() { "man" }) { } public override string[] Help diff --git a/CommandsParser/Commands/HistoryCommand.cs b/CommandsParser/Commands/HistoryCommand.cs new file mode 100644 index 0000000..3f09d1a --- /dev/null +++ b/CommandsParser/Commands/HistoryCommand.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CommandsParser.Commands +{ + public class HistoryCommand:BaseCommand + { + public HistoryCommand(CmdParser cmdParser) + : base(cmdParser, + "history", + "Echoes the command history for this parser", + null) + { } + + public override string[] Help + { + get + { + return new string[] { + "Echoes the command history for this parser"}; + } + } + + public override void Execute(string[] arguments) + { + int commandCount = 1; + foreach (string command in CmdParser.CommandsHistory) + { + CmdParser.StdOutput.AppendOutputLineFormat("{0}: {1}",commandCount,command); + commandCount++; + } + } + } +} diff --git a/CommandsParserSample/Commands/ClearConsoleCommand.cs b/CommandsParserSample/Commands/ClearConsoleCommand.cs index 5913ea3..fee4d58 100644 --- a/CommandsParserSample/Commands/ClearConsoleCommand.cs +++ b/CommandsParserSample/Commands/ClearConsoleCommand.cs @@ -14,8 +14,7 @@ public ClearConsoleCommand(CmdParser cmdParser) : base(cmdParser, "clear", "Clears the console", - new List() { "cls" }, - null) + new List() { "cls" }) { } public override string[] Help @@ -29,13 +28,11 @@ public override string[] Help public override void Execute(string[] arguments) { - OnCommandExecuting(arguments); if (Cancel) return; else { Console.Clear(); - OnCommandExecuted(arguments); } } } diff --git a/CommandsParserSample/Commands/CloseCommand.cs b/CommandsParserSample/Commands/CloseCommand.cs index b142f5f..33493bf 100644 --- a/CommandsParserSample/Commands/CloseCommand.cs +++ b/CommandsParserSample/Commands/CloseCommand.cs @@ -13,8 +13,7 @@ public CloseCommand(CmdParser cmdParser) : base(cmdParser, "close", "Close the application", - new List() { "exit", "shutdown" }, - null) + new List() { "exit", "shutdown" }) { } public override string[] Help diff --git a/CommandsParserSample/Commands/TextCommand.cs b/CommandsParserSample/Commands/TextCommand.cs index 9b60f11..33966aa 100644 --- a/CommandsParserSample/Commands/TextCommand.cs +++ b/CommandsParserSample/Commands/TextCommand.cs @@ -51,8 +51,7 @@ public TextCommand(CmdParser cmdParser) : base(cmdParser, "text", "Echoes a series of text lines", - new List() { "cls" }, - null) + new List() { "cls" }) { rnd = new Random(); IsMuted = true; @@ -79,7 +78,6 @@ public override void Execute(string[] arguments) AppendOutputLine(GetRandomString()); } } - OnCommandExecuted(arguments); } private string GetRandomString() diff --git a/CommandsParserSample/Commands/TickTockCommand.cs b/CommandsParserSample/Commands/TickTockCommand.cs index c008511..0447dc0 100644 --- a/CommandsParserSample/Commands/TickTockCommand.cs +++ b/CommandsParserSample/Commands/TickTockCommand.cs @@ -23,7 +23,6 @@ public TickTockCommand(CmdParser cmdParser) : base(cmdParser, "tick", "Example of a delayed command", - new List(), null) { } diff --git a/CommandsParserSample/Commands/UppercaseCommand.cs b/CommandsParserSample/Commands/UppercaseCommand.cs index f25ff75..9cf724b 100644 --- a/CommandsParserSample/Commands/UppercaseCommand.cs +++ b/CommandsParserSample/Commands/UppercaseCommand.cs @@ -13,8 +13,7 @@ public UppercaseCommand(CmdParser cmdParser) : base(cmdParser, "uppercase", "Makes all the text in the arguments UPPERCASE", - new List() { "upper" }, - null) + new List() { "upper" }) { IsMuted = true; } @@ -37,7 +36,6 @@ public override void Execute(string[] arguments) sb.AppendFormat("{0} ", arguments[i].ToUpper()); } AppendOutputLine(sb.ToString()); - OnCommandExecuted(arguments); } } } diff --git a/CommandsParserSample/Program.cs b/CommandsParserSample/Program.cs index 160eece..d6b51e3 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -78,7 +78,7 @@ private static void TextCommand_CommandExecuted(object sender, CommandsParser.Ev private static void ClsCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) { - Console.WriteLine("output cleared!!!"); + Console.WriteLine("output cleared!!! On executed"); } private static void StdOutput_OnOutputAvailable(object sender, CommandsParser.Events.OutputAvailableEventArgs e)