Skip to content

Commit

Permalink
Add some custom CLI parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Jun 25, 2024
1 parent 51a1f0c commit 9451629
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- Fix minimum number of args checks
- Move GetDefaultSpeedForMediaType to common location
- Move some Check-specific methods
- Add some custom CLI parameters

### 3.2.0 (2024-06-20)

Expand Down
92 changes: 81 additions & 11 deletions MPF.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,21 @@ public static void Main(string[] args)
if (!string.IsNullOrEmpty(message))
Console.WriteLine(message);

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

// Get the explicit output options
string path = args[2].Trim('"');
string filepath = args[3].Trim('"');
string filepath = args[startIndex].Trim('"');

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

// Now populate an environment
var drive = Drive.Create(null, path);
// Populate an environment
var drive = Drive.Create(null, devicePath ?? string.Empty);
var env = new DumpEnvironment(options, filepath, drive, knownSystem, mediaType, options.InternalProgram, parameters: null);
string? paramStr = env.GetFullParameters(speed);

// Process custom parameters
if (args.Length > 4)
paramStr = string.Join(" ", args.Skip(4).ToArray());

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

Console.WriteLine("Usage:");
Console.WriteLine("MPF.CLI <mediatype> <system> <drivepath> </path/to/output.cue/iso> [custom-params]");
Console.WriteLine("MPF.CLI <mediatype> <system> [options] </path/to/output.cue/iso>");
Console.WriteLine();
Console.WriteLine("Standalone Options:");
Console.WriteLine("-h, -? Show this help text");
Expand All @@ -170,9 +169,80 @@ private static void DisplayHelp(string? error = null)
Console.WriteLine("-ls, --listsystems List supported system types");
Console.WriteLine("-lp, --listprograms List supported dumping program outputs");
Console.WriteLine();
Console.WriteLine("Custom parameters, if supplied, will fully replace the default parameters for the dumping");

Console.WriteLine("CLI Options:");
Console.WriteLine("-u, --use <program> Override default dumping program");
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");
Console.WriteLine("program defined in the configuration. All parameters need to be supplied if doing this.");
Console.WriteLine();
}

/// <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)
{
// Create return values
string? parsedPath = null, customParams = null;

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

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

// Loop through the arguments and parse out values
for (; startIndex < args.Length; startIndex++)
{
// Use specific program
if (args[startIndex].StartsWith("-u=") || args[startIndex].StartsWith("--use="))
{
string internalProgram = args[startIndex].Split('=')[1];
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);
startIndex++;
}

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

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

// Default, we fall out
else
{
break;
}
}

return (parsedPath, customParams, startIndex);
}
}
}
7 changes: 5 additions & 2 deletions MPF.Frontend/DumpEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class DumpEnvironment
/// <summary>
/// Drive object representing the current drive
/// </summary>
private readonly Drive? _drive;
private Drive? _drive;

/// <summary>
/// ExecutionContext object representing how to invoke the internal program
Expand Down Expand Up @@ -194,11 +194,14 @@ public bool SetExecutionContext(string? parameters)
_ => null,
};

// Set system and type
// Set system, type, and drive
if (_executionContext != null)
{
_executionContext.System = _system;
_executionContext.Type = _type;

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

return _executionContext != null;
Expand Down

0 comments on commit 9451629

Please sign in to comment.