Skip to content

Commit

Permalink
Greatly simplify PreParseArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Apr 6, 2024
1 parent 4f23a06 commit 6d97a51
Showing 1 changed file with 44 additions and 80 deletions.
124 changes: 44 additions & 80 deletions TwitchDownloaderCLI/Tools/PreParseArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand All @@ -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 <mode> to verbs (like \'git <verb>\'), 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 <mode> to verbs (like \'git <verb>\'), 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<string> ConvertEmbedEmoteSyntax(List<string> args)
private static void ConvertEmbedEmoteSyntax(IList<string> 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<string> ConvertModeSyntax(List<string> args)
private static void ConvertModeSyntax(IList<string> 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<string> ConvertSilentSyntax(List<string> args)
private static void ConvertSilentSyntax(IList<string> 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<string> ConvertVerboseFfmpegSyntax(List<string> args)
private static void ConvertVerboseFfmpegSyntax(IList<string> 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));
}
}
}

0 comments on commit 6d97a51

Please sign in to comment.