public void Execute(IConsoleOut console, string[] args) { console.WriteLine("Parsing arguments"); var allArguments = GetArguments(); try { ParseArguments(args, allArguments); console.WriteLine("Parsed arguments successfully"); } catch (CommandLineArgumentException ce) { console.WriteLine("InvalidArguments: " + ce.Message); console.WriteLine(Usage()); return; } console.WriteLine("Executing program with the following paramters:"); console.WriteLine(CurrentValues(allArguments)); try { var validArguments = allArguments.ToDictionary(x => x.Name); Execute(console, validArguments); console.WriteLine("Executed successfully"); } catch (Exception) { console.WriteLine("Execution failed with exception"); throw; } }
protected override void Execute(IConsoleOut console, IDictionary <string, Argument> args) { var reps = args[Param.Repetitions].ValueAs <int>(); var s = args[Param.SourceString]; for (var i = 0; i < reps; i++) { console.WriteLine(s.ToString()); } }
/// <summary> /// Shows message with colored info header /// </summary> public static void Info(IConsoleOut cout, string msg, int ln = 0) { var f = cout.NonNull(nameof(cout)).ForegroundColor; var b = cout.BackgroundColor; cout.ForegroundColor = ConsoleColor.Black; cout.BackgroundColor = ConsoleColor.Green; if (ln == 0) { cout.Write("Info:"); } else { cout.Write("{0:D3}-Info:".Args(ln)); } cout.BackgroundColor = ConsoleColor.Black; cout.ForegroundColor = ConsoleColor.Gray; cout.WriteLine(" " + msg); cout.ForegroundColor = f; cout.BackgroundColor = b; }
/// <summary> /// Shows message with colored warning header /// </summary> public static void Warning(IConsoleOut cout, string msg, int ln = 0) { var f = cout.NonNull(nameof(cout)).ForegroundColor; var b = cout.BackgroundColor; cout.ForegroundColor = ConsoleColor.Black; cout.BackgroundColor = ConsoleColor.Yellow; if (ln == 0) { cout.Write("WARNING:"); } else { cout.Write("{0:D3}-WARNING:".Args(ln)); } cout.BackgroundColor = ConsoleColor.Black; cout.ForegroundColor = ConsoleColor.Yellow; cout.WriteLine(" " + msg); cout.ForegroundColor = f; cout.BackgroundColor = b; }
/// <summary> /// Shows message with colored error header /// </summary> public static void Error(IConsoleOut cout, string msg, int ln = 0) { var f = cout.NonNull(nameof(cout)).ForegroundColor; var b = cout.BackgroundColor; cout.ForegroundColor = ConsoleColor.Black; cout.BackgroundColor = ConsoleColor.Red; if (ln == 0) { cout.Write("ERROR:"); } else { cout.Write("{0:D3}-ERROR:".Args(ln)); } cout.BackgroundColor = ConsoleColor.Black; cout.ForegroundColor = ConsoleColor.Red; cout.WriteLine(" " + msg); cout.ForegroundColor = f; cout.BackgroundColor = b; }
/// <summary> /// Outputs colored text from content supplied in an HTML-like grammar /// </summary> public static void WriteMarkupContent(IConsoleOut cout, string content, char open, char close) { cout.NonNull(nameof(cout)); TokenParser parser = new TokenParser(content, open, close); Stack <ConsoleColor> stack = new Stack <ConsoleColor>(); bool collapsespaces = false; foreach (TokenParser.Token tok in parser) { if (tok.IsSimpleText) { if (collapsespaces) { cout.Write(tok.Content.Trim()); } else { cout.Write(tok.Content); } continue; } string name = tok.Name.ToUpperInvariant().Trim(); if (name == "LITERAL") { collapsespaces = false; continue; } if (name == "HTML") { collapsespaces = true; continue; } if (name == "BR") { cout.WriteLine(); continue; } if ((name == "SP") || (name == "SPACE")) { string txt = " "; int cnt = 1; try { cnt = int.Parse(tok["COUNT"]); } catch { } while (txt.Length < cnt) { txt += " "; } cout.Write(txt); continue; } if (name == "PUSH") { stack.Push(cout.ForegroundColor); stack.Push(cout.BackgroundColor); continue; } if (name == "POP") { if (stack.Count > 1) { cout.BackgroundColor = stack.Pop(); cout.ForegroundColor = stack.Pop(); } continue; } if (name == "RESET") { cout.ResetColor(); continue; } if ((name == "F") || (name == "FORE") || (name == "FOREGROUND")) { try { ConsoleColor clr = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), tok["COLOR"], true); cout.ForegroundColor = clr; } catch { } continue; } if ((name == "B") || (name == "BACK") || (name == "BACKGROUND")) { try { ConsoleColor clr = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), tok["COLOR"], true); cout.BackgroundColor = clr; } catch { } continue; } if ((name == "J") || (name == "JUST") || (name == "JUSTIFY")) { try { int width = int.Parse(tok["WIDTH"]); direction dir = (direction)Enum.Parse(typeof(direction), tok["DIR"], true); string txt = tok["TEXT"]; switch (dir) { case direction.Right: { while (txt.Length < width) { txt = " " + txt; } break; } case direction.Center: { while (txt.Length < width) { txt = " " + txt + " "; } if (txt.Length > width) { txt = txt.Substring(0, txt.Length - 1); } break; } default: { while (txt.Length < width) { txt = txt + " "; } break; } } cout.Write(txt); } catch { } continue; } } }
protected abstract void Execute(IConsoleOut console, IDictionary <string, Argument> args);
/// <summary> /// Writes line break /// </summary> public static void NewLine(this IConsoleOut console) { console.NonNull(nameof(console)).WriteLine(); }