/// <summary> /// Main entry point wrapped for errorcode /// </summary> /// <param name="args">command line args</param> /// <returns>errorcode of the function</returns> static ErrorCode MainWrapped(string[] args) { //parse command line input if (!CommandlineDictionary.TryParse(args, out CommandlineDictionary commandline, '-', '/')) { //command line parse failed return(ErrorCode.InvalidCommandline); } //check if called for help page if (commandline.TryGetAny(out bool helpPageRequested, "help", "?") && helpPageRequested) { //show help and exit ShowHelp(); return(ErrorCode.NoError); } //process single file or batch, depending if -bd and -bm are set AND -i is NOT set bool isSingle = !commandline.HasAnyKey("batchdir", "bd") && !commandline.HasAnyKey("batchmask", "bm") && commandline.HasAnyKey("input", "i"); bool isBatch = commandline.HasAnyKey("batchdir", "bd") && commandline.HasAnyKey("batchmask", "bm") && !commandline.HasAnyKey("input", "i"); //check if command line is valid if (isSingle == isBatch) { //invalid command line return(ErrorCode.InvalidCommandline); } //process batch or single if (isSingle) { return(ProcessSingle(commandline)); } if (isBatch) { return(ProcessBatch(commandline)); } //how did we end up here?? (this should NEVER happen, assume command line is invalid...) return(ErrorCode.InvalidCommandline); }
/// <summary> /// Process multiple files with the given command line /// </summary> /// <param name="commandline">the command line to use</param> /// <returns>errorcode of the processing step</returns> static ErrorCode ProcessBatch(CommandlineDictionary commandline) { Console.WriteLine("Process BATCH..."); //get batch input directory and filename mask if (!commandline.TryGetAny(out string batchDir, "batchdir", "bd") || !Directory.Exists(batchDir)) { //batch directory not found! return(ErrorCode.InputNotFound); } if (!commandline.TryGetAny(out string batchMask, "batchmask", "bm")) { //batch mask not found return(ErrorCode.InvalidCommandline); } //get batch output directory if (!commandline.TryGetAny(out string batchOutputDir, "output", "o")) { //no output directory given, default to batchdir/a4k/ batchOutputDir = Path.Combine(batchDir, "a4k"); } //process each file in the batch directory that matches the mask int fileCounter = 0; ErrorCode errors = ErrorCode.NoError; foreach (string sourceFile in Directory.EnumerateFiles(batchDir, batchMask, SearchOption.TopDirectoryOnly)) { //get output filename string targetFile = Path.Combine(batchOutputDir, Path.GetFileName(sourceFile)); //process the file errors |= ProcessFile(sourceFile, targetFile, commandline); fileCounter++; } Console.WriteLine($"Processed {fileCounter} files in {batchDir}!"); return(errors); }
/// <summary> /// Process a single file based on the command line given /// </summary> /// <param name="sourcePath">the file to process</param> /// <param name="targetPath">the path to save the processed image to (if the file exists AND -overwrite is NOT set, errorcode "OutputFileExists" will be returned)</param> /// <param name="commandline">the commandline to use</param> /// <returns>errorcode of the file processing action</returns> static ErrorCode ProcessFile(string sourcePath, string targetPath, CommandlineDictionary commandline) { Console.WriteLine($"Processing \"{sourcePath}\", saving to \"{targetPath}\"..."); #region Prepare Input and Output file paths //check input file exists if (!File.Exists(sourcePath)) { return(ErrorCode.InputNotFound); } //prepare any directories needed for the output path string targetDir = Path.GetDirectoryName(targetPath); if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } //check output file can be written to (does not exist or am allowed to overwrite) if (File.Exists(targetPath)) { //delete the file if we are allowed to overwrite it if (commandline.TryGetAny(out bool allowOverwrite, "overwrite", "o") && allowOverwrite) { //have overwrite flag and is set to true, delete the file and be A-OK try { File.Delete(targetPath); } catch (Exception) { //delete failed, return error code return(ErrorCode.OutputFileExists); } } else { //are not allowed to overwrite, return error code return(ErrorCode.OutputFileExists); } }
/// <summary> /// Process a single file with the given command line /// </summary> /// <param name="commandline">the command line to use</param> /// <returns>errorcode of the processing step</returns> static ErrorCode ProcessSingle(CommandlineDictionary commandline) { Console.WriteLine("Process SINGLE..."); //get input file path if (!commandline.TryGetAny(out string inputFilePath, "input", "i") || !File.Exists(inputFilePath)) { //input file does NOT exist return(ErrorCode.InputNotFound); } //get the output path if (!commandline.TryGetAny(out string outputFilePath, "output", "o")) { //default to input file path + _a4k outputFilePath = Path.Combine(Path.GetDirectoryName(inputFilePath), $"{Path.GetFileNameWithoutExtension(inputFilePath)}_a4k{Path.GetExtension(inputFilePath)}"); } //process the file return(ProcessFile(inputFilePath, outputFilePath, commandline)); }