public static void Main(string[] args) { Options commandLineOptions = new Options(); ICommandLineParser commandParser = new CommandLineParser(); if (commandParser.ParseArguments(args, commandLineOptions, Console.Error)) { if (ValidateOptions(commandLineOptions)) { try { TaskProcessor concatTask = new TaskProcessor(); concatTask.ProcessTask(commandLineOptions); } catch (Exception ex) { StringBuilder errorMessage = new StringBuilder(); errorMessage.AppendLine(messageUnexpectedError); if (commandLineOptions.debugMessages) { errorMessage.AppendFormat(messageUnhandledException, ex.ToString(), ex.Message, ex.StackTrace); } System.Console.Error.WriteLine(errorMessage.ToString()); Environment.ExitCode = 1; } } } else { // Command line params could not be parsed, // or help was requested Environment.ExitCode = -1; } }
public void ProcessTask(Options commandLineOptions) { var splitTools = new CoreTools(); try { splitTools.EvenOddMerge(commandLineOptions.Items[0], commandLineOptions.Items[1], commandLineOptions.Items[2], commandLineOptions.skipExtraPages); } catch (UnauthorizedAccessException) { System.Console.Error.WriteLine(Environment.NewLine + "Access denied."); } catch (System.IO.FileNotFoundException) { System.Console.Error.WriteLine(Environment.NewLine + "File not found."); } catch (System.IO.DirectoryNotFoundException) { System.Console.Error.WriteLine(Environment.NewLine + "Directory not found."); } catch (IOException ioException) { // PDF file is not valid, or was not found if (ioException.Message.Contains("PDF")) { System.Console.Error.WriteLine(Environment.NewLine + "Input file is not a valid PDF."); } else if (ioException.Message.Contains("not found as file or resource")) { System.Console.Error.WriteLine(Environment.NewLine + ioException.Message); } else { // Some other IOException we weren't expecting throw; } } }
private static bool ValidateOptions(Options commandLineOptions) { bool validatedOK = false; StringBuilder errorMessage = new StringBuilder(); if (commandLineOptions.Items.Count == 3) { // Make sure the input files are actually readable for (int loop = 0; loop < commandLineOptions.Items.Count - 1; loop++) { try { using (FileStream inputFile = new FileStream(commandLineOptions.Items[loop], FileMode.Open, FileAccess.Read)) { inputFile.Close(); } } catch { errorMessage.AppendLine(String.Format(messageFileNotFound, commandLineOptions.Items[loop])); } } // Make sure we can actually write the desired output file try { using (FileStream outputFile = new FileStream(commandLineOptions.Items[commandLineOptions.Items.Count - 1], FileMode.Create, FileAccess.ReadWrite)) { outputFile.Close(); } } catch { errorMessage.AppendLine(String.Format(messageCouldNotCreateFile, commandLineOptions.Items[commandLineOptions.Items.Count - 1])); } finally { try { File.Delete(commandLineOptions.Items[commandLineOptions.Items.Count - 1]); } catch { } } if (String.IsNullOrEmpty(errorMessage.ToString())) validatedOK = true; } else { switch (commandLineOptions.Items.Count) { case 2: // No output file specified errorMessage.AppendLine(messageNoOutputFileSpecifed); break; case 1: default: errorMessage.AppendLine(messageInsufficientInputFiles); break; } } if (!validatedOK) System.Console.Error.Write(errorMessage.ToString()); return validatedOK; }