From 6d97a51cc9eafc10121d4d5a06376bbb214824de Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 6 Apr 2024 00:55:16 -0400 Subject: [PATCH] Greatly simplify PreParseArgs --- TwitchDownloaderCLI/Tools/PreParseArgs.cs | 124 ++++++++-------------- 1 file changed, 44 insertions(+), 80 deletions(-) diff --git a/TwitchDownloaderCLI/Tools/PreParseArgs.cs b/TwitchDownloaderCLI/Tools/PreParseArgs.cs index 0f9576d6..793a7c65 100644 --- a/TwitchDownloaderCLI/Tools/PreParseArgs.cs +++ b/TwitchDownloaderCLI/Tools/PreParseArgs.cs @@ -9,7 +9,7 @@ internal static class PreParseArgs { internal static string[] Parse(string[] args, string processFileName) { - if (args.Any(x => x is "-m" or "--mode" or "--embed-emotes" or "--silent")) + if (args.Any(x => x is "-m" or "--mode" or "--embed-emotes" or "--silent" or "--verbose-ffmpeg")) { // A legacy syntax was used, convert to new syntax return Process(ConvertFromOldSyntax(args, processFileName)); @@ -20,10 +20,11 @@ internal static string[] Parse(string[] args, string processFileName) private static string[] Process(string[] args) { - if (args.Length == 0) - return args; + if (args.Length > 0) + { + args[0] = args[0].ToLower(); + } - args[0] = args[0].ToLower(); return args; } @@ -35,100 +36,63 @@ private static string[] ConvertFromOldSyntax(string[] args, string processFileNa { var processedArgs = args.ToList(); - if (args.Any(x => x.Equals("--embed-emotes"))) - { - Console.WriteLine("[INFO] The program has switched from --embed-emotes to -E / --embed-images, consider using those instead. Run \'{0} help\' for more information.", processFileName); - processedArgs = ConvertEmbedEmoteSyntax(processedArgs); - } - - if (args.Any(x => x is "-m" or "--mode")) + for (var i = 0; i < processedArgs.Count; i++) { - Console.WriteLine("[INFO] The program has switched from --mode to verbs (like \'git \'), consider using verbs instead. Run \'{0} help\' for more information.", processFileName); - processedArgs = ConvertModeSyntax(processedArgs); - } - - if (args.Any(x => x is "--silent")) - { - Console.WriteLine("[INFO] The program has switched from --silent to log levels, consider using log levels instead. '--log-level None' will be applied to the current session. Run \'{0} help\' for more information.", processFileName); - processedArgs = ConvertSilentSyntax(processedArgs); - } - - if (args.Any(x => x is "--verbose-ffmpeg")) - { - Console.WriteLine("[INFO] The program has switched from --verbose-ffmpeg to log levels, consider using log levels instead. '--log-level Status,Info,Warning,Error,Ffmpeg' will be applied to the current session. Run \'{0} help\' for more information.", processFileName); - processedArgs = ConvertVerboseFfmpegSyntax(processedArgs); + switch (processedArgs[i]) + { + case "--embed-emotes": + Console.WriteLine("[INFO] The program has switched from --embed-emotes to -E / --embed-images, consider using those instead. Run \'{0} help\' for more information.", processFileName); + ConvertEmbedEmoteSyntax(processedArgs, i); + break; + case "-m" or "--mode": + Console.WriteLine("[INFO] The program has switched from --mode to verbs (like \'git \'), consider using verbs instead. Run \'{0} help\' for more information.", processFileName); + ConvertModeSyntax(processedArgs, i); + break; + case "--silent": + Console.WriteLine("[INFO] The program has switched from --silent to log levels, consider using log levels instead. '--log-level None' will be applied to the current session. Run \'{0} help\' for more information.", processFileName); + ConvertSilentSyntax(processedArgs, i); + break; + case "--verbose-ffmpeg": + Console.WriteLine("[INFO] The program has switched from --verbose-ffmpeg to log levels, consider using log levels instead. '--log-level Status,Info,Warning,Error,Ffmpeg' will be applied to the current session. Run \'{0} help\' for more information.", processFileName); + ConvertVerboseFfmpegSyntax(processedArgs, i); + break; + } } return processedArgs.ToArray(); } - private static List ConvertEmbedEmoteSyntax(List args) + private static void ConvertEmbedEmoteSyntax(IList args, int index) { - var argsLength = args.Count; - - for (var i = 0; i < argsLength; i++) - { - if (args[i].Equals("--embed-emotes")) - { - args[i] = "-E"; - break; - } - } - - return args; + args[index] = "-E"; } - private static List ConvertModeSyntax(List args) + private static void ConvertModeSyntax(IList args, int index) { - var argsLength = args.Count; - var processedArgs = new string[argsLength - 1]; + // --mode + args.RemoveAt(index); - var j = 1; - for (var i = 0; i < argsLength; i++) - { - if (args[i].Equals("-m") || args[i].Equals("--mode")) - { - // Copy the run-mode to the verb position - processedArgs[0] = args[i + 1]; - i++; - continue; - } - processedArgs[j] = args[i]; - j++; - } - - return processedArgs.ToList(); + // run-mode + var runMode = args[index]; + args.RemoveAt(index); + args.Insert(0, runMode); } - private static List ConvertSilentSyntax(List args) + private static void ConvertSilentSyntax(IList args, int index) { - for (var i = 0; i < args.Count; i++) - { - if (args[i].Equals("--silent")) - { - args[i] = "--log-level"; - args.Insert(i + 1, nameof(LogLevel.None)); - } - } - - return args; + args[index] = "--log-level"; + args.Insert(index + 1, nameof(LogLevel.None)); } - private static List ConvertVerboseFfmpegSyntax(List args) + private static void ConvertVerboseFfmpegSyntax(IList args, int index) { - for (var i = 0; i < args.Count; i++) - { - if (args[i].Equals("--verbose-ffmpeg")) - { - // If the user is still using --verbose-ffmpeg they probably aren't using log levels yet, so its safe to assume that there won't be a double-parse error - args[i] = "--log-level"; - var logLevels = Enum.GetNames(typeof(LogLevel)) - .Where(x => x is not nameof(LogLevel.None) and not nameof(LogLevel.Verbose)); - args.Insert(i + 1, string.Join(',', logLevels)); - } - } + // If the user is still using --verbose-ffmpeg they probably aren't using log levels yet, so its safe to assume that there won't be a double-parse error + args[index] = "--log-level"; - return args; + var logLevels = Enum.GetNames(typeof(LogLevel)) + .Where(x => x is not nameof(LogLevel.None) and not nameof(LogLevel.Verbose)); + + args.Insert(index + 1, string.Join(',', logLevels)); } } }