using System; using System.Collections.Generic; using System.IO; namespace ExtractionTool { /// /// Set of options for the test executable /// internal sealed class Options { #region Properties /// /// Enable debug output for relevant operations /// public bool Debug { get; private set; } = false; /// /// Set of input paths to use for operations /// public List InputPaths { get; private set; } = []; /// /// Output path for archive extraction /// public string OutputPath { get; private set; } = string.Empty; #endregion /// /// Parse commandline arguments into an Options object /// public static Options? ParseOptions(string[] args) { // If we have invalid arguments if (args == null || args.Length == 0) return null; // Create an Options object var options = new Options(); // Parse the options and paths for (int index = 0; index < args.Length; index++) { string arg = args[index]; switch (arg) { case "-?": case "-h": case "--help": return null; case "-d": case "--debug": options.Debug = true; break; case "-o": case "--outdir": options.OutputPath = index + 1 < args.Length ? args[++index] : string.Empty; break; default: options.InputPaths.Add(arg); break; } } // Validate we have any input paths to work on if (options.InputPaths.Count == 0) { Console.WriteLine("At least one path is required!"); return null; } // Validate the output path bool validPath = ValidateExtractionPath(options); if (!validPath) return null; return options; } /// /// Display help text /// public static void DisplayHelp() { Console.WriteLine("Extraction Tool"); Console.WriteLine(); Console.WriteLine("ExtractionTool.exe file|directory ..."); Console.WriteLine(); Console.WriteLine("Options:"); Console.WriteLine("-?, -h, --help Display this help text and quit"); Console.WriteLine("-d, --debug Enable debug mode"); Console.WriteLine("-o, --outdir [PATH] Set output path for extraction (required)"); } /// /// Validate the extraction path /// private static bool ValidateExtractionPath(Options options) { // Null or empty output path if (string.IsNullOrEmpty(options.OutputPath)) { Console.WriteLine("Output directory required for extraction!"); Console.WriteLine(); return false; } // Malformed output path or invalid location try { options.OutputPath = Path.GetFullPath(options.OutputPath); Directory.CreateDirectory(options.OutputPath); } catch { Console.WriteLine("Output directory could not be created!"); Console.WriteLine(); return false; } return true; } } }