Files
MPF/MPF.CLI/Program.cs

168 lines
5.8 KiB
C#
Raw Permalink Normal View History

2024-06-24 10:37:05 -04:00
using System;
2025-10-06 11:00:59 -04:00
using System.Collections.Generic;
2024-07-24 11:45:12 -04:00
#if NET40
using System.Threading.Tasks;
#endif
2025-10-06 10:34:11 -04:00
using MPF.CLI.Features;
2025-10-06 11:00:59 -04:00
using MPF.Frontend.Features;
2024-06-24 10:37:05 -04:00
using MPF.Frontend.Tools;
2025-10-06 11:00:59 -04:00
using SabreTools.CommandLine;
using SabreTools.CommandLine.Features;
2024-06-24 10:37:05 -04:00
namespace MPF.CLI
{
public class Program
{
public static void Main(string[] args)
{
2024-06-24 11:38:13 -04:00
// Load options from the config file
var options = OptionsLoader.LoadFromConfig();
if (options.FirstRun)
{
// Reset first run
options.FirstRun = false;
OptionsLoader.SaveToConfig(options);
2025-04-30 09:02:36 -04:00
// Display non-error message
2025-10-21 09:01:33 -04:00
Console.WriteLine("First-run detected! Please verify the generated config.json and run again.");
2025-04-30 09:02:36 -04:00
return;
2024-06-24 11:38:13 -04:00
}
2025-10-06 15:25:11 -04:00
// Create the command set
2025-10-06 16:34:19 -04:00
var mainFeature = new MainFeature();
var commandSet = CreateCommands(mainFeature);
2025-10-06 15:25:11 -04:00
// If we have no args, show the help and quit
2026-01-25 18:09:00 -05:00
if (args is null || args.Length == 0)
2024-06-24 10:37:05 -04:00
{
BaseFeature.DisplayHelp();
2024-06-24 10:37:05 -04:00
return;
}
2025-10-06 15:25:11 -04:00
// Get the first argument as a feature flag
string featureName = args[0];
// Try processing the standalone arguments
var topLevel = commandSet.GetTopLevel(featureName);
switch (topLevel)
2024-06-24 10:37:05 -04:00
{
2025-10-06 15:25:11 -04:00
// Standalone Options
case Help: BaseFeature.DisplayHelp(); return;
2025-10-06 15:25:11 -04:00
case VersionFeature version: version.Execute(); return;
case ListCodesFeature lc: lc.Execute(); return;
case ListConfigFeature lc: lc.Execute(); return;
2025-10-06 15:25:11 -04:00
case ListMediaTypesFeature lm: lm.Execute(); return;
case ListProgramsFeature lp: lp.Execute(); return;
case ListSystemsFeature ls: ls.Execute(); return;
// Interactive Mode
case InteractiveFeature interactive:
if (interactive.Options.CheckForUpdatesOnStartup)
CheckForUpdates();
2025-10-06 17:53:15 -04:00
if (!interactive.ProcessArgs(args, 0))
{
BaseFeature.DisplayHelp();
2025-10-06 17:53:15 -04:00
return;
}
2026-01-25 18:09:00 -05:00
if (!interactive.Execute())
{
BaseFeature.DisplayHelp();
return;
}
2025-10-06 15:25:11 -04:00
break;
2025-10-06 10:32:16 -04:00
2025-10-06 15:25:11 -04:00
// Default Behavior
default:
if (mainFeature.Options.CheckForUpdatesOnStartup)
CheckForUpdates();
2025-10-06 17:53:15 -04:00
if (!mainFeature.ProcessArgs(args, 0))
{
BaseFeature.DisplayHelp();
return;
}
2026-01-25 18:09:00 -05:00
if (!mainFeature.Execute())
{
BaseFeature.DisplayHelp();
2025-10-06 17:53:15 -04:00
return;
}
2025-10-06 15:25:11 -04:00
break;
}
2024-06-24 10:37:05 -04:00
}
/// <summary>
/// Check for available updates
/// </summary>
private static void CheckForUpdates()
{
FrontendTool.CheckForNewVersion(out bool different, out string message, out string? url);
if (url is null)
message = $"An exception occurred while checking for remote versions:{Environment.NewLine}{message}";
Console.WriteLine(message);
if (different && url is not null)
Console.WriteLine($"Update URL: {url}");
else if (!different && url is not null)
Console.WriteLine("You have the newest version!");
Console.WriteLine();
}
2025-10-06 11:00:59 -04:00
/// <summary>
/// Create the command set for the program
/// </summary>
2025-10-06 16:34:19 -04:00
private static CommandSet CreateCommands(MainFeature mainFeature)
2025-10-06 11:00:59 -04:00
{
List<string> header = [
"MPF.CLI [standalone|system] [options] <path> ...",
string.Empty,
];
List<string> footer = [
string.Empty,
"Dumping program paths and other settings can be found in the config.json file",
"generated next to the program by default. Ensure that all settings are to user",
"preference before running MPF.CLI.",
string.Empty,
"Custom dumping parameters, if used, will fully replace the default parameters.",
"All dumping parameters need to be supplied if doing this.",
"Otherwise, both a drive path and output file path are required.",
string.Empty,
"Mounted filesystem path is only recommended on OSes that require block",
"device dumping, usually Linux and macOS.",
string.Empty,
];
var commandSet = new CommandSet(header, footer);
// Standalone Options
commandSet.Add(new Help());
commandSet.Add(new VersionFeature());
commandSet.Add(new ListCodesFeature());
commandSet.Add(new ListConfigFeature());
2025-10-06 11:00:59 -04:00
commandSet.Add(new ListMediaTypesFeature());
commandSet.Add(new ListSystemsFeature());
commandSet.Add(new ListProgramsFeature());
commandSet.Add(new InteractiveFeature());
// CLI Options
2025-10-06 16:34:19 -04:00
commandSet.Add(mainFeature.UseInput);
commandSet.Add(mainFeature.MediaTypeInput);
commandSet.Add(mainFeature.DeviceInput);
commandSet.Add(mainFeature.MountedInput);
commandSet.Add(mainFeature.FileInput);
commandSet.Add(mainFeature.SpeedInput);
commandSet.Add(mainFeature.CustomInput);
2025-10-06 11:00:59 -04:00
return commandSet;
}
2024-06-24 10:37:05 -04:00
}
}