private static string GetTemplateValue(CommandLineArgs cla) { string template=""; if (!cla.Template.IsNullOrEmpty()) // template provided -- no header skipping required { template = cla.Template; if (cla.Verbose) Console.WriteLine("Provided template was '" + template + "'"); } if (!cla.Pattern.IsNullOrEmpty()) // template provided -- no header skipping required { template = cla.Pattern; if (cla.Verbose) Console.WriteLine("Provided pattern was '" + template + "'"); } if (!cla.InputFilePath.IsNullOrEmpty()) // input file provided { template = File.ReadAllText(cla.InputFilePath); if (cla.Verbose) Console.WriteLine("Provided template was '" + template + "'"); } return template; }
private static void Main(string[] args) { var cla = new CommandLineArgs(); Stopwatch sw = null; #if DEBUG Debugger.Launch(); #endif try { var result = CommandLine.Parser.Default.ParseArguments(args, cla); if (!result) { Console.WriteLine("Parse failed! Use --help flag for instructions on usage."); return; } if (cla.Verbose) { sw = new Stopwatch(); sw.Start(); } if (cla.ListNamedPatterns) { var path = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "default.tdg-patterns"; var namedParameters = FileReader.LoadNamedPatterns(path); Console.WriteLine("Named Parameters:"); foreach (var namedParameter in namedParameters.Patterns.OrderBy(x => x.Name)) { Console.WriteLine(namedParameter.Name); } } else { var template = GetTemplateValue(cla); if (!template.IsNullOrEmpty() && !cla.OutputFilePath.IsNullOrEmpty()) // output path provided. { OutputToFile(cla, template); } else if (!template.IsNullOrEmpty()) { OutputToConsole(cla, template); } else { Console.WriteLine(cla.GetUsage()); } } if (cla.Verbose) { sw.Stop(); Console.WriteLine("Generation took {0} milliseconds", sw.ElapsedMilliseconds); } } catch (GenerationException gex) { Console.WriteLine(string.Format("Error:\n{0}", gex.Message)); } catch (Exception ex) { Console.WriteLine(string.Format("Error:{0}\n\nStackTrace:{1}", ex.Message, ex.StackTrace)); } }
private static void Main(string[] args) { var cla = new CommandLineArgs(); var sw = new Stopwatch(); sw.Start(); #if DEBUG Debugger.Launch(); #endif try { // test for piped content. var arguments = GetArguments(args); var result = cla.CliParse(arguments); if (!result.Successful) { Console.WriteLine("Parse failed! Use --help flag for instructions on usage."); return; } if (result.ShowHelp) { var usage = cla.GetUsage(); Console.WriteLine(usage); return; } if (cla.ShowPatternHelp) { var usage = cla.GetPatternUsage(); Console.Write(usage); return; } if (cla.ListNamedPatterns) { var paths = new List <string>(); paths.Add("default"); if (!string.IsNullOrEmpty(cla.NamedPatterns)) { cla.NamedPatterns.Split(';').ToList().ForEach(paths.Add); } Console.WriteLine("Named Parameters:"); foreach (var file in paths) { var correctedPath = FileReader.GetPatternFilePath(file); var namedParameters = FileReader.LoadNamedPatterns(correctedPath); foreach (var namedParameter in namedParameters.Patterns) { Console.WriteLine(namedParameter.Name); } } } else { var template = GetTemplateValue(cla); if (!string.IsNullOrEmpty(template)) // output path provided. { if (cla.Verbose) { Console.WriteLine("Creating " + cla.Count.ToString() + " items."); } if (!string.IsNullOrEmpty(cla.OutputFilePath)) { OutputToFile(cla, template); } else { OutputToConsole(cla, template); } } else { Console.WriteLine(cla.GetUsage()); } } if (cla.Verbose) { if (sw != null) { sw.Stop(); Console.WriteLine("Generation took {0} milliseconds", sw.ElapsedMilliseconds); } } } catch (GenerationException gex) { Console.WriteLine("Error:\n{0}", gex.Message); } catch (Exception ex) { Console.WriteLine("Error:{0}\n\nStackTrace:{1}", ex.Message, ex.StackTrace); } }
private static void Main(string[] args) { var cla = new CommandLineArgs(); Stopwatch sw = null; #if DEBUG Debugger.Launch(); #endif try { var result = CommandLine.Parser.Default.ParseArguments(args, cla); if (!result) { Console.WriteLine("Parse failed! Use --help flag for instructions on usage."); return; } if (cla.Verbose) { sw = new Stopwatch(); sw.Start(); } if (cla.ListNamedPatterns) { var path = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "default.tdg-patterns"; var namedParameters = FileReader.LoadNamedPatterns(path); Console.WriteLine("Named Parameters:"); foreach (var namedParameter in namedParameters.Patterns.OrderBy(x => x.Name)) { Console.WriteLine(namedParameter.Name); } } else { var template = GetTemplateValue(cla); if (!template.IsNullOrEmpty() && !cla.OutputFilePath.IsNullOrEmpty()) // output path provided. { OutputToFile(cla, template); } else if (!template.IsNullOrEmpty()) { OutputToConsole(cla, template); } else { Console.WriteLine(cla.GetUsage()); } } if (cla.Verbose) { sw.Stop(); Console.WriteLine("Generation took {0} milliseconds", sw.ElapsedMilliseconds); } } catch (GenerationException gex) { Console.WriteLine(string.Format("Error:\n{0}", gex.Message)); } catch (Exception ex) { Console.WriteLine(string.Format("Error:{0}\n\nStackTrace:{1}",ex.Message, ex.StackTrace)); } }
private static void OutputToFile(CommandLineArgs cla, string template) { using (var fs = new StreamWriter(cla.OutputFilePath)) { int ct = 0; while (ct < cla.Count) { var item = AlphaNumericGenerator.GenerateFromTemplate(template); fs.WriteLine(item); ct++; } } }
private static void OutputToConsole(CommandLineArgs cla, string template) { Func<string, string> generateFrom = AlphaNumericGenerator.GenerateFromTemplate; if (!cla.Pattern.IsNullOrEmpty()) { generateFrom = AlphaNumericGenerator.GenerateFromPattern; } int ct = 0; while (ct < cla.Count) { var item = generateFrom(template); Console.WriteLine(item); ct++; } }