public static RuntimeParameters ParseRuntimeParameters(string command, string input, string output) { RuntimeParameters pr = new RuntimeParameters(); if (command.Equals("Compress", StringComparison.InvariantCultureIgnoreCase)) { pr.Command = RunCommand.Compress; pr.input = input.TrimStart('\\'); pr.output = output.TrimStart('\\'); // check if input exist var inputFullPath = !Utils.IsFullPath(pr.input) ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, pr.input) : pr.input; if (!File.Exists(inputFullPath)) { throw new Exception($"File {inputFullPath} not found"); } } else if (command.Equals("Decompress", StringComparison.InvariantCultureIgnoreCase)) { pr.Command = RunCommand.Decompress; pr.input = input.TrimStart('\\'); pr.output = output.TrimStart('\\'); var outputFullPath = !Utils.IsFullPath(pr.output) ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, pr.output) : pr.output; if (File.Exists(outputFullPath)) { throw new Exception($"File {outputFullPath} already exist"); } } else { throw new Exception($"Command {command} not recognized"); } return(pr); }
// tests // compress C:\Users\irina\Desktop\CompressionApp\CompressionApp\data\Data.csv data // decompress C:\Users\irina\Desktop\CompressionApp\CompressionApp\data\data_1.zip data.csv.txt static void Main(string[] args) { RuntimeParameters parameters = null; int result = 0; try { if (args.Length < 3) { throw new Exception("Not enough parameters"); } parameters = RuntimeParameters.ParseRuntimeParameters(args[0], args[1], args[2]); } catch (Exception ex) { Console.WriteLine("Error occurred:" + ex.Message); Console.WriteLine($"Result:{result}"); PrintProgramUsage(); PreventFromClosing(); return; } try { Compressor compressor = new Compressor(parameters.input, parameters.output); if (parameters.Command == RunCommand.Compress) { result = compressor.Compress(); } else { result = compressor.Decompress(); } } catch (Exception ex) { Console.WriteLine("Error occurred:" + ex.Message); Console.WriteLine($"Result:{result}"); PrintProgramUsage(); PreventFromClosing(); return; } Console.WriteLine($"Result:{result}"); PreventFromClosing(); }