Skip to content

Commit

Permalink
Try out custom options classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Jun 25, 2024
1 parent 9451629 commit a92159b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Move GetDefaultSpeedForMediaType to common location
- Move some Check-specific methods
- Add some custom CLI parameters
- Try out custom options classes

### 3.2.0 (2024-06-20)

Expand Down
94 changes: 69 additions & 25 deletions MPF.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using BinaryObjectScanner;
using MPF.Frontend;
using MPF.Frontend.Tools;
Expand Down Expand Up @@ -100,20 +99,24 @@ public static void Main(string[] args)
Console.WriteLine(message);

// Process any custom parameters
(string? devicePath, string? customParams, int startIndex) = LoadFromArguments(args, options, startIndex: 2);
(CommandOptions opts, int startIndex) = LoadFromArguments(args, options, startIndex: 2);

// Get the explicit output options
string filepath = args[startIndex].Trim('"');
// Ensure we have the values we need
if (opts.CustomParams == null && (opts.DevicePath == null || opts.DevicePath == null))
{
DisplayHelp("Both a device path and file path need to be supplied, exiting...");
return;
}

// Get the speed from the options
int speed = FrontendTool.GetDefaultSpeedForMediaType(mediaType, options);
int speed = opts.DriveSpeed ?? FrontendTool.GetDefaultSpeedForMediaType(mediaType, options);

// Populate an environment
var drive = Drive.Create(null, devicePath ?? string.Empty);
var env = new DumpEnvironment(options, filepath, drive, knownSystem, mediaType, options.InternalProgram, parameters: null);
var drive = Drive.Create(null, opts.DevicePath ?? string.Empty);
var env = new DumpEnvironment(options, opts.FilePath, drive, knownSystem, mediaType, options.InternalProgram, parameters: null);

// Process the parameters
string? paramStr = customParams ?? env.GetFullParameters(speed);
string? paramStr = opts.CustomParams ?? env.GetFullParameters(speed);
if (string.IsNullOrEmpty(paramStr))
{
DisplayHelp("No valid environment could be created, exiting...");
Expand Down Expand Up @@ -160,7 +163,7 @@ private static void DisplayHelp(string? error = null)
Console.WriteLine(error);

Console.WriteLine("Usage:");
Console.WriteLine("MPF.CLI <mediatype> <system> [options] </path/to/output.cue/iso>");
Console.WriteLine("MPF.CLI <mediatype> <system> [options]");
Console.WriteLine();
Console.WriteLine("Standalone Options:");
Console.WriteLine("-h, -? Show this help text");
Expand All @@ -172,8 +175,10 @@ private static void DisplayHelp(string? error = null)

Console.WriteLine("CLI Options:");
Console.WriteLine("-u, --use <program> Override default dumping program");
Console.WriteLine("-d, --device <devicepath> Physical drive path (Required if no custom parameters set)");
Console.WriteLine("-f, --file \"<filepath>\" Output file path (Required if no custom parameters set)");
Console.WriteLine("-s, --speed <speed> Override default dumping speed");
Console.WriteLine("-c, --custom \"<params>\" Custom parameters to use");
Console.WriteLine("-p, --path <drivepath> Physical drive path if not defined in custom parameters");
Console.WriteLine();

Console.WriteLine("Custom parameters, if used, will fully replace the default parameters for the dumping");
Expand All @@ -184,18 +189,18 @@ private static void DisplayHelp(string? error = null)
/// <summary>
/// Load the current set of options from application arguments
/// </summary>
private static (string? devicePath, string? customParams, int nextIndex) LoadFromArguments(string[] args, Frontend.Options options, int startIndex = 0)
private static (CommandOptions opts, int nextIndex) LoadFromArguments(string[] args, Frontend.Options options, int startIndex = 0)
{
// Create return values
string? parsedPath = null, customParams = null;
var opts = new CommandOptions();

// If we have no arguments, just return
if (args == null || args.Length == 0)
return (parsedPath, customParams, 0);
return (opts, 0);

// If we have an invalid start index, just return
if (startIndex < 0 || startIndex >= args.Length)
return (parsedPath, customParams, startIndex);
return (opts, startIndex);

// Loop through the arguments and parse out values
for (; startIndex < args.Length; startIndex++)
Expand All @@ -213,25 +218,53 @@ private static (string? devicePath, string? customParams, int nextIndex) LoadFro
startIndex++;
}

// Use a custom parameters
else if (args[startIndex].StartsWith("-c=") || args[startIndex].StartsWith("--custom="))
// Use a device path
else if (args[startIndex].StartsWith("-d=") || args[startIndex].StartsWith("--device="))
{
customParams = args[startIndex].Split('=')[1].Trim('"');
opts.DevicePath = args[startIndex].Split('=')[1].Trim('"');
}
else if (args[startIndex] == "-c" || args[startIndex] == "--custom")
else if (args[startIndex] == "-d" || args[startIndex] == "--device")
{
customParams = args[startIndex + 1].Trim('"');
opts.DevicePath = args[startIndex + 1].Trim('"');
startIndex++;
}

// Use a device path
else if (args[startIndex].StartsWith("-p=") || args[startIndex].StartsWith("--path="))
// Use a file path
else if (args[startIndex].StartsWith("-f=") || args[startIndex].StartsWith("--file="))
{
parsedPath = args[startIndex].Split('=')[1].Trim('"');
opts.FilePath = args[startIndex].Split('=')[1].Trim('"');
}
else if (args[startIndex] == "-p" || args[startIndex] == "--path")
else if (args[startIndex] == "-f" || args[startIndex] == "--file")
{
parsedPath = args[startIndex + 1].Trim('"');
opts.FilePath = args[startIndex + 1].Trim('"');
startIndex++;
}

// Set an override speed
else if (args[startIndex].StartsWith("-s=") || args[startIndex].StartsWith("--speed="))
{
if (!int.TryParse(args[startIndex].Split('=')[1].Trim('"'), out int speed))
speed = -1;

opts.DriveSpeed = speed;
}
else if (args[startIndex] == "-s" || args[startIndex] == "--speed")
{
if (!int.TryParse(args[startIndex + 1].Trim('"'), out int speed))
speed = -1;

opts.DriveSpeed = speed;
startIndex++;
}

// Use a custom parameters
else if (args[startIndex].StartsWith("-c=") || args[startIndex].StartsWith("--custom="))
{
opts.CustomParams = args[startIndex].Split('=')[1].Trim('"');
}
else if (args[startIndex] == "-c" || args[startIndex] == "--custom")
{
opts.CustomParams = args[startIndex + 1].Trim('"');
startIndex++;
}

Expand All @@ -242,7 +275,18 @@ private static (string? devicePath, string? customParams, int nextIndex) LoadFro
}
}

return (parsedPath, customParams, startIndex);
return (opts, startIndex);
}

private class CommandOptions
{
public string? DevicePath { get; set; } = null;

public string? FilePath { get; set; } = null;

public int? DriveSpeed { get; set; } = null;

public string? CustomParams { get; set; } = null;
}
}
}
74 changes: 41 additions & 33 deletions MPF.Check/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static void Main(string[] args)
}

// Loop through and process options
(var options, var seedInfo, var path, int startIndex) = LoadFromArguments(args, startIndex: 2);
if (options.InternalProgram == InternalProgram.NONE)
(CommandOptions opts, int startIndex) = LoadFromArguments(args, startIndex: 2);
if (opts.Options.InternalProgram == InternalProgram.NONE)
{
DisplayHelp("A program name needs to be provided");
return;
Expand All @@ -46,9 +46,9 @@ public static void Main(string[] args)

// Validate the supplied credentials
#if NETFRAMEWORK
(bool? _, string? message) = RedumpWebClient.ValidateCredentials(options.RedumpUsername ?? string.Empty, options.RedumpPassword ?? string.Empty);
(bool? _, string? message) = RedumpWebClient.ValidateCredentials(opts.Options.RedumpUsername ?? string.Empty, opts.Options.RedumpPassword ?? string.Empty);
#else
(bool? _, string? message) = RedumpHttpClient.ValidateCredentials(options.RedumpUsername ?? string.Empty, options.RedumpPassword ?? string.Empty).ConfigureAwait(false).GetAwaiter().GetResult();
(bool? _, string? message) = RedumpHttpClient.ValidateCredentials(opts.Options.RedumpUsername ?? string.Empty, opts.Options.RedumpPassword ?? string.Empty).ConfigureAwait(false).GetAwaiter().GetResult();
#endif
if (!string.IsNullOrEmpty(message))
Console.WriteLine(message);
Expand All @@ -68,10 +68,10 @@ public static void Main(string[] args)

// Now populate an environment
Drive? drive = null;
if (!string.IsNullOrEmpty(path))
drive = Drive.Create(null, path!);
if (!string.IsNullOrEmpty(opts.DevicePath))
drive = Drive.Create(null, opts.DevicePath!);

var env = new DumpEnvironment(options, filepath, drive, knownSystem, mediaType, internalProgram: null, parameters: null);
var env = new DumpEnvironment(opts.Options, filepath, drive, knownSystem, mediaType, internalProgram: null, parameters: null);

// Finally, attempt to do the output dance
#if NET40
Expand Down Expand Up @@ -123,10 +123,13 @@ private static void DisplayHelp(string? error = null)
/// <summary>
/// Load the current set of options from application arguments
/// </summary>
private static (Frontend.Options, SubmissionInfo?, string?, int) LoadFromArguments(string[] args, int startIndex = 0)
private static (CommandOptions, int) LoadFromArguments(string[] args, int startIndex = 0)
{
// Create return values
var opts = new CommandOptions();

// Create the output values with defaults
var options = new Frontend.Options()
opts.Options = new Frontend.Options()
{
RedumpUsername = null,
RedumpPassword = null,
Expand All @@ -137,20 +140,16 @@ private static (Frontend.Options, SubmissionInfo?, string?, int) LoadFromArgumen
DeleteUnnecessaryFiles = false,
};

// Create the submission info to return, if necessary
SubmissionInfo? info = null;
string? parsedPath = null;

// These values require multiple parts to be active
bool scan = false, hideDriveLetters = false;

// If we have no arguments, just return
if (args == null || args.Length == 0)
return (options, null, null, 0);
return (opts, 0);

// If we have an invalid start index, just return
if (startIndex < 0 || startIndex >= args.Length)
return (options, null, null, startIndex);
return (opts, startIndex);

// Loop through the arguments and parse out values
for (; startIndex < args.Length; startIndex++)
Expand All @@ -159,43 +158,43 @@ private static (Frontend.Options, SubmissionInfo?, string?, int) LoadFromArgumen
if (args[startIndex].StartsWith("-u=") || args[startIndex].StartsWith("--use="))
{
string internalProgram = args[startIndex].Split('=')[1];
options.InternalProgram = Frontend.Options.ToInternalProgram(internalProgram);
opts.Options.InternalProgram = Frontend.Options.ToInternalProgram(internalProgram);
}
else if (args[startIndex] == "-u" || args[startIndex] == "--use")
{
string internalProgram = args[startIndex + 1];
options.InternalProgram = Frontend.Options.ToInternalProgram(internalProgram);
opts.Options.InternalProgram = Frontend.Options.ToInternalProgram(internalProgram);
startIndex++;
}

// Redump login
else if (args[startIndex].StartsWith("-c=") || args[startIndex].StartsWith("--credentials="))
{
string[] credentials = args[startIndex].Split('=')[1].Split(';');
options.RedumpUsername = credentials[0];
options.RedumpPassword = credentials[1];
opts.Options.RedumpUsername = credentials[0];
opts.Options.RedumpPassword = credentials[1];
}
else if (args[startIndex] == "-c" || args[startIndex] == "--credentials")
{
options.RedumpUsername = args[startIndex + 1];
options.RedumpPassword = args[startIndex + 2];
opts.Options.RedumpUsername = args[startIndex + 1];
opts.Options.RedumpPassword = args[startIndex + 2];
startIndex += 2;
}

// Pull all information (requires Redump login)
else if (args[startIndex].Equals("-a") || args[startIndex].Equals("--pull-all"))
{
options.PullAllInformation = true;
opts.Options.PullAllInformation = true;
}

// Use a device path for physical checks
else if (args[startIndex].StartsWith("-p=") || args[startIndex].StartsWith("--path="))
{
parsedPath = args[startIndex].Split('=')[1];
opts.DevicePath = args[startIndex].Split('=')[1];
}
else if (args[startIndex] == "-p" || args[startIndex] == "--path")
{
parsedPath = args[startIndex + 1];
opts.DevicePath = args[startIndex + 1];
startIndex++;
}

Expand All @@ -215,37 +214,37 @@ private static (Frontend.Options, SubmissionInfo?, string?, int) LoadFromArgumen
else if (args[startIndex].StartsWith("-l=") || args[startIndex].StartsWith("--load-seed="))
{
string seedInfo = args[startIndex].Split('=')[1];
info = Builder.CreateFromFile(seedInfo);
opts.Seed = Builder.CreateFromFile(seedInfo);
}
else if (args[startIndex] == "-l" || args[startIndex] == "--load-seed")
{
string seedInfo = args[startIndex + 1];
info = Builder.CreateFromFile(seedInfo);
opts.Seed = Builder.CreateFromFile(seedInfo);
startIndex++;
}

// Add filename suffix
else if (args[startIndex].Equals("-x") || args[startIndex].Equals("--suffix"))
{
options.AddFilenameSuffix = true;
opts.Options.AddFilenameSuffix = true;
}

// Output submission JSON
else if (args[startIndex].Equals("-j") || args[startIndex].Equals("--json"))
{
options.OutputSubmissionJSON = true;
opts.Options.OutputSubmissionJSON = true;
}

// Compress log and extraneous files
else if (args[startIndex].Equals("-z") || args[startIndex].Equals("--zip"))
{
options.CompressLogFiles = true;
opts.Options.CompressLogFiles = true;
}

// Delete unnecessary files files
else if (args[startIndex].Equals("-d") || args[startIndex].Equals("--delete"))
{
options.DeleteUnnecessaryFiles = true;
opts.Options.DeleteUnnecessaryFiles = true;
}

// Default, we fall out
Expand All @@ -256,10 +255,19 @@ private static (Frontend.Options, SubmissionInfo?, string?, int) LoadFromArgumen
}

// Now deal with the complex options
options.ScanForProtection = scan && !string.IsNullOrEmpty(parsedPath);
options.HideDriveLetters = hideDriveLetters && scan && !string.IsNullOrEmpty(parsedPath);
opts.Options.ScanForProtection = scan && !string.IsNullOrEmpty(opts.DevicePath);
opts.Options.HideDriveLetters = hideDriveLetters && scan && !string.IsNullOrEmpty(opts.DevicePath);

return (opts, startIndex);
}

private class CommandOptions
{
public Frontend.Options Options { get; set; } = new Frontend.Options();

public SubmissionInfo? Seed { get; set; } = null;

return (options, info, parsedPath, startIndex);
public string? DevicePath { get; set; } = null;
}
}
}
5 changes: 3 additions & 2 deletions MPF.Frontend/DumpEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public int? Speed
/// <param name="internalProgram"></param>
/// <param name="parameters"></param>
public DumpEnvironment(Frontend.Options options,
string outputPath,
string ?outputPath,
Drive? drive,
RedumpSystem? system,
MediaType? type,
Expand Down Expand Up @@ -200,7 +200,8 @@ public bool SetExecutionContext(string? parameters)
_executionContext.System = _system;
_executionContext.Type = _type;

// Set the drive, if not already set
// Set some parameters, if not already set
OutputPath ??= _executionContext.OutputPath!;
_drive ??= Drive.Create(InternalDriveType.Optical, _executionContext.InputPath!);
}

Expand Down

0 comments on commit a92159b

Please sign in to comment.