diff --git a/CommandsParser/BaseCommand.cs b/CommandsParser/BaseCommand.cs index a65fe14..afda48d 100644 --- a/CommandsParser/BaseCommand.cs +++ b/CommandsParser/BaseCommand.cs @@ -13,96 +13,111 @@ public class BaseCommand private const string DEFAULT_COMMAND_DESCRIPTION = "NO_DESCRIPTION"; private readonly string[] DEFAULT_COMMAND_HELP = { "UNDEFINED_HELP"}; + 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 CommandExecutingEventHandler CommandExecuting; + private StringBuilder commandOutput; - public delegate void OutputAvailableEventHandler(object sender, OutputAvailableEventArgs e); - public event OutputAvailableEventHandler OutputAvailable; + private bool isMuted = false; + + private bool cancel = false; - protected string CommandOutput + public bool Cancel + { + get { return this.cancel; } + set { this.cancel = value; } + } + + public string Output { get { - string commandResult = GetOutputBuffer(); - ClearOutputBuffer(); - return commandResult; + if (commandOutput != null) + { + string output = commandOutput.ToString(); + ClearOutput(); + return output; + } + else + { + commandOutput = new StringBuilder(); + return string.Empty; + } } } - protected void AppendToResult(string message) + internal void CancelCommandExecution() { - commandOutput.Append(message); + Cancel = true; + AppendOutputLine("Command canceled!"); } - protected void AppendToResult(string messageFormat, params object[] args) + + public bool IsMuted { - commandOutput.AppendFormat(messageFormat, args); + get { return this.isMuted; } + protected set { this.isMuted = value; } } - protected void AppendLineToResult() + public void AppendOutput(string message) { - commandOutput.AppendLine(); + commandOutput.Append(message); + if(!IsMuted) + CmdParser.StdOutput.AppendOutput(message); } - protected void AppendLineToResult(string message) + public void AppendOutputLine(string message) { commandOutput.AppendLine(message); + if (!IsMuted) + CmdParser.StdOutput.AppendOutputLine(message); } - protected void AppendLineToResult(string message, params object[] args) + public void AppendOutputFormat(string messageFormat, params object[] args) { - commandOutput.AppendLine(string.Format(message, args)); + commandOutput.AppendFormat(messageFormat, args); + if (!IsMuted) + CmdParser.StdOutput.AppendOutputFormat(messageFormat, args); } - - - protected void ClearOutputBuffer() + public void AppendOutputLineFormat(string messageFormat, params object[] args) { - commandOutput.Clear(); + commandOutput.AppendFormat(messageFormat+Environment.NewLine, args); + if (!IsMuted) + CmdParser.StdOutput.AppendOutputLineFormat(messageFormat, args); } - public string GetOutputBuffer() + private void ClearOutput() { - if (commandOutput != null) - return commandOutput.ToString(); - else - { - commandOutput = new StringBuilder(); - return string.Empty; - } + commandOutput.Clear(); } - public BaseCommand() + public void RunCommand(string[] arguments) { - Name = DEFAULT_COMMAND_NAME; - Description = DEFAULT_COMMAND_DESCRIPTION; - Aliases = new List(); - Help = DEFAULT_COMMAND_HELP; - if (commandOutput == null) - commandOutput = new StringBuilder(); + OnCommandExecuting(arguments); + Execute(arguments); + OnCommandExecuted(arguments); } - public BaseCommand(string name) - { - 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) + + + public BaseCommand(CmdParser cmdParser, 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 (commandOutput == null) - commandOutput = new StringBuilder(); + + commandOutput = new StringBuilder(); } @@ -127,16 +142,25 @@ public BaseCommand(string name, string description, List aliases) /// /// /// - public virtual string Execute(string[] arguments) + public virtual void Execute(string[] arguments) + {} + + public void OnCommandExecuted(string[] arguments) { - string commandOutput = CommandOutput; - OnOutputAvailable(new OutputAvailableEventArgs(Name, commandOutput)); - return commandOutput; + string output = Output; + OnCommandExecuted(new CommandExecutedEventArgs(output,arguments)); } - - public void OnOutputAvailable(OutputAvailableEventArgs eventArgs) + public void OnCommandExecuted(CommandExecutedEventArgs eventArgs) + { + CommandExecuted?.Invoke(this, eventArgs); + } + public void OnCommandExecuting(string[] arguments) + { + OnCommandExecuting(new CommandExecutingEventArgs(this,arguments)); + } + public void OnCommandExecuting(CommandExecutingEventArgs eventArgs) { - OutputAvailable?.Invoke(this, eventArgs ); + CommandExecuting?.Invoke(this, eventArgs); } } } \ No newline at end of file diff --git a/CommandsParser/CmdParser.cs b/CommandsParser/CmdParser.cs index 4c319e0..c58ff88 100644 --- a/CommandsParser/CmdParser.cs +++ b/CommandsParser/CmdParser.cs @@ -9,8 +9,11 @@ namespace CommandsParser { public class CmdParser { - private Queue commandsHistory; - private int maxCommandsInHistory = 3; + public const string COMMAND_START_OUTPUT = ""; + public const string COMMAND_FINISH_OUTPUT = ""; + + private Queue executedCommandsHistory; + private int maxCommandsInHistory = 32; public event OutputAvailableEventHandler OnOutputAvailable; @@ -18,6 +21,18 @@ 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; } + } + public CmdParser() { stdOutput = new StandardOutput(); @@ -25,7 +40,7 @@ public CmdParser() AvailableCommands = new List(); - commandsHistory = new Queue(); + executedCommandsHistory = new Queue(); AddDefaultCommands(); } @@ -69,8 +84,10 @@ 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)); + AddCommand(new Commands.HistoryCommand(this)); } public void AddCommand(BaseCommand command) @@ -99,23 +116,38 @@ public bool Execute(string input) { return false; } - + 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]; + 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); + + executedCommandsHistory.Enqueue(string.Format("{0} {1}", commandToExecute.Name, joinedArguments)); + if (executedCommandsHistory.Count > maxCommandsInHistory) + executedCommandsHistory.Dequeue(); + + commandToExecute.RunCommand(args); + stdOutput.FlushOutput(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; } + /// /// Removes all commands. /// diff --git a/CommandsParser/Commands/AliasCommand.cs b/CommandsParser/Commands/AliasCommand.cs index 3425e04..f2c50bb 100644 --- a/CommandsParser/Commands/AliasCommand.cs +++ b/CommandsParser/Commands/AliasCommand.cs @@ -8,8 +8,9 @@ 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) { } @@ -24,7 +25,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 { @@ -42,23 +43,22 @@ public override string Execute(string[] arguments) else sb.AppendFormat("{0},", command.Aliases[i]); } - - AppendLineToResult(sb.ToString()); + CmdParser.StdOutput.AppendOutputLine(sb.ToString()); } else { - AppendLineToResult("No aliases found for command {0}", arguments[0]); + CmdParser.StdOutput.AppendOutputLineFormat("No aliases found for command {0}",arguments[0]); } } else { - AppendLineToResult("Command {0} not found", arguments[0]); + CmdParser.StdOutput.AppendOutputLineFormat("Command {0} not found", arguments[0]); } } else //List all aliases { - AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); + CmdParser.StdOutput.AppendOutputLineFormat("List of all {0} commands", CmdParser.AvailableCommands.Count); int commandIndex = 0; foreach (BaseCommand command in CmdParser.AvailableCommands) { @@ -72,14 +72,9 @@ 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.AppendOutputLineFormat("{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 new file mode 100644 index 0000000..beb04c0 --- /dev/null +++ b/CommandsParser/Commands/EchoCommand.cs @@ -0,0 +1,31 @@ +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) + { } + + public override string[] Help + { + get + { + return new string[] { + "Echoes a message to the active Output"}; + } + } + + public override void Execute(string[] arguments) + { + string output= string.Join(" ", arguments); + base.CmdParser.StdOutput.Echo(output); + } + } +} diff --git a/CommandsParser/Commands/HelpCommand.cs b/CommandsParser/Commands/HelpCommand.cs index bd90d87..73184d8 100644 --- a/CommandsParser/Commands/HelpCommand.cs +++ b/CommandsParser/Commands/HelpCommand.cs @@ -8,8 +8,9 @@ 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" }) { } @@ -25,39 +26,37 @@ 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.AppendOutputLineFormat("Displaying help for command {0}", arguments[0]); for (int i = 0; i < command.Help.Length; i++) { - AppendLineToResult(command.Help[i]); + CmdParser.StdOutput.AppendOutputLine(command.Help[i]); } } else { - AppendLineToResult("Command {0} not found", arguments[0]); + CmdParser.StdOutput.AppendOutputLineFormat("Command {0} not found", arguments[0]); } } else //List all commands { - AppendLineToResult("List of all " + CmdParser.AvailableCommands.Count + " commands:"); + CmdParser.StdOutput.AppendOutputLineFormat("List of all {0} commands", CmdParser.AvailableCommands.Count); + 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.AppendOutputLineFormat("{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/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/CommandsParser/CommandsParser.csproj b/CommandsParser/CommandsParser.csproj index 856cb0f..ea6a8dd 100644 --- a/CommandsParser/CommandsParser.csproj +++ b/CommandsParser/CommandsParser.csproj @@ -2,7 +2,7 @@ netstandard1.0 - 0.4.0 + 0.5.0 diff --git a/CommandsParser/Events/CommandExecutionEventArgs.cs b/CommandsParser/Events/CommandExecutionEventArgs.cs new file mode 100644 index 0000000..0d0f4b6 --- /dev/null +++ b/CommandsParser/Events/CommandExecutionEventArgs.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CommandsParser.Events +{ + public class CommandExecutedEventArgs : EventArgs + { + public string Output { get; private set; } + public string[] Arguments { get; private set; } + + public CommandExecutedEventArgs(string output,string[] arguments) + { + Output = output; + Arguments = arguments; + } + } + public class CommandExecutingEventArgs : EventArgs + { + private bool cancel = false; + public string[] Arguments { get; private set; } + + public string MergedArgs + { + get + { + StringBuilder sb = new StringBuilder(); + foreach (string arg in Arguments) + { + sb.AppendFormat("{0} ", arg); + } + return sb.ToString().Trim(); + } + } + private BaseCommand commandOwner; + public bool Cancel + { + get { return this.cancel; } + set { this.cancel = value; + if (value) + commandOwner.CancelCommandExecution(); + } + } + + public CommandExecutingEventArgs(BaseCommand commandOwner, string[] arguments) + { + this.commandOwner = commandOwner; + Arguments = arguments; + } + } +} diff --git a/CommandsParser/StandardOutput.cs b/CommandsParser/StandardOutput.cs index 9bf6929..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 { @@ -84,6 +95,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 +115,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 27a6f3c..fee4d58 100644 --- a/CommandsParserSample/Commands/ClearConsoleCommand.cs +++ b/CommandsParserSample/Commands/ClearConsoleCommand.cs @@ -10,8 +10,9 @@ namespace CommandsParserSample.Commands public class ClearConsoleCommand : BaseCommand { - public ClearConsoleCommand() - : base("clear", + public ClearConsoleCommand(CmdParser cmdParser) + : base(cmdParser, + "clear", "Clears the console", new List() { "cls" }) { } @@ -25,12 +26,14 @@ 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; + if (Cancel) + return; + else + { + Console.Clear(); + } } } diff --git a/CommandsParserSample/Commands/CloseCommand.cs b/CommandsParserSample/Commands/CloseCommand.cs index 8ec9bdd..33493bf 100644 --- a/CommandsParserSample/Commands/CloseCommand.cs +++ b/CommandsParserSample/Commands/CloseCommand.cs @@ -9,8 +9,9 @@ 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" }) { } @@ -24,12 +25,9 @@ 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; } } } diff --git a/CommandsParserSample/Commands/TextCommand.cs b/CommandsParserSample/Commands/TextCommand.cs new file mode 100644 index 0000000..33966aa --- /dev/null +++ b/CommandsParserSample/Commands/TextCommand.cs @@ -0,0 +1,90 @@ +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" }) + { + rnd = new Random(); + IsMuted = true; + } + + public override string[] Help + { + get + { + return new string[] { + "Echoes a series of text lines"}; + } + } + + 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()); + } + } + } + + 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..0447dc0 --- /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", + "Example of a delayed command", + null) + { } + + public override string[] Help + { + get + { + return new string[] { + "Example of a delayed command"}; + } + } + + 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 + { + CmdParser.StdOutput.EchoLine("tock"); + cyclesCompleted = 0; + OnCommandExecuted(new string[] { }); + + } + } + } +} \ No newline at end of file diff --git a/CommandsParserSample/Commands/UppercaseCommand.cs b/CommandsParserSample/Commands/UppercaseCommand.cs new file mode 100644 index 0000000..9cf724b --- /dev/null +++ b/CommandsParserSample/Commands/UppercaseCommand.cs @@ -0,0 +1,41 @@ +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", + "Makes all the text in the arguments UPPERCASE", + new List() { "upper" }) + { + IsMuted = true; + } + + public override string[] Help + { + get + { + return new string[] { + "Makes all the text in the arguments UPPERCASE"}; + } + } + + 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()); + } + } +} 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 67516eb..d6b51e3 100644 --- a/CommandsParserSample/Program.cs +++ b/CommandsParserSample/Program.cs @@ -15,24 +15,79 @@ class Program static CmdParser cmdParser = new CmdParser(); 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(new ClearConsoleCommand()); - cmdParser.AddCommand(new CloseCommand()); + cmdParser.AddCommand(clsCommand); + cmdParser.AddCommand(new CloseCommand(cmdParser)); + cmdParser.AddCommand(textCommand); + cmdParser.AddCommand(uppercaseCommand); + 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; + isRunning = true; while (isRunning) { - cmdParser.Execute(Console.ReadLine()); + string input = Console.ReadLine(); + cmdParser.Execute(input); } } - private static void StdOutput_OnOutputAvailable(object sender, CommandsParser.Events.OutputAvailableEventArgs e) + 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); } + + 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) + { + if (e.Arguments.Length > 0 && e.Arguments[0] == "upper") + cmdParser.Execute(string.Format("upper {0}", e.Output)); + else + Console.WriteLine(e.Output); + + } + + private static void ClsCommand_CommandExecuted(object sender, CommandsParser.Events.CommandExecutedEventArgs e) + { + Console.WriteLine("output cleared!!! On executed"); + } + + 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); + + } } 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 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