public FileProcessor(Options options) { // Get the input and output file. this.InputFile = options.InputFile; this.OutputFile = !string.IsNullOrWhiteSpace(options.OutputFile) ? options.OutputFile : string.Format("out.{0}.{1}", DateTime.Now.ToString("MMddyyyyHHmm"), options.InputFile); this.FileReader = new StreamReader(this.InputFile); this.FileWriter = new StreamWriter(this.OutputFile); this.FileWriter.AutoFlush = true; this.OutputStartCharacterIndex = options.OutputStartCharacterIndex; this.OutputEndCharacterIndex = options.OutputEndCharacterIndex; // Set up the filters in a way that we can use them easily. this.StartCharacterIndices = new List<int>(); this.FiltersByStartCharacterIndex = new Dictionary<int, List<Filter>>(); foreach (var f in options.Filters) { List<Filter> filters; if (!this.FiltersByStartCharacterIndex.TryGetValue(f.StartCharacterIndex, out filters)) { filters = new List<Filter>(); this.FiltersByStartCharacterIndex.Add(f.StartCharacterIndex, filters); this.StartCharacterIndices.Add(f.StartCharacterIndex); } filters.Add(f); } StartCharacterIndices.OrderBy(index => index); }
public static int Main(string[] args) { var options = new Options(); var parser = new CommandLine.Parser(settings => { settings.CaseSensitive = true; settings.HelpWriter = System.Console.Out; }); try { if (parser.ParseArguments(args, options)) { // Validate the arguments. options.ValidateOptions(); try { var message = string.Format("Processing file {0}...", options.InputFile); System.Console.WriteLine(message); Trace.TraceInformation(message); var processor = new FileProcessor(options); processor.Process(); message = string.Format("Done processing file {0}.", options.InputFile); System.Console.WriteLine(message); Trace.TraceInformation(message); } catch (Exception ex) { Trace.TraceError(string.Format("Exception occurred while trying to process the file. Exception: {0} {1}", ex.Message, ex.StackTrace)); System.Console.Error.WriteLine("An error occurred while trying to process the file."); return (int)ExitCode.Errored; } } } catch (ArgumentNullException ex) { Trace.TraceError(string.Format("ArgumentNullException occurred while trying to parse arguments passed to the program. Exception: {0} {1}", ex.Message, ex.StackTrace)); System.Console.Error.WriteLine("An error occurred while trying to parse the arguments passed to the application."); return (int)ExitCode.ArgumentError; } catch (InvalidArgumentException ex) { Trace.TraceError(string.Format("InvalidArgumentException occurred while trying to parse arguments passed to the program. Exception: {0} {1}", ex.Message, ex.StackTrace)); System.Console.Error.WriteLine("The following argument is invalid: {0} Error: {1}", ex.InvalidParameterName, ex.Message); return (int)ExitCode.ArgumentError; } return (int)ExitCode.Success; }