improve message clarity and add validator for path command

This commit is contained in:
Syping 2025-11-20 23:48:46 +01:00
parent 530c84d979
commit 9a70c45c7a

View file

@ -13,7 +13,7 @@ internal static class Commands {
photo.Format = format.ToLowerInvariant() switch { photo.Format = format.ToLowerInvariant() switch {
"gta5" => PhotoFormat.GTA5, "gta5" => PhotoFormat.GTA5,
"rdr2" => PhotoFormat.RDR2, "rdr2" => PhotoFormat.RDR2,
_ => throw new ArgumentException("Invalid format", nameof(format)) _ => throw new ArgumentException("Invalid Photo Format", nameof(format))
}; };
if (photo.Format == PhotoFormat.GTA5) { if (photo.Format == PhotoFormat.GTA5) {
@ -178,7 +178,7 @@ internal static class Commands {
photo.Format = format.ToLowerInvariant() switch { photo.Format = format.ToLowerInvariant() switch {
"gta5" => PhotoFormat.GTA5, "gta5" => PhotoFormat.GTA5,
"rdr2" => PhotoFormat.RDR2, "rdr2" => PhotoFormat.RDR2,
_ => throw new ArgumentException("Invalid format", nameof(format)) _ => throw new ArgumentException("Invalid Photo Format", nameof(format))
}; };
} }
@ -238,14 +238,14 @@ internal static class Commands {
try { try {
if (command == "register" || command == "unregister") { if (command == "register" || command == "unregister") {
String appPath = Path.GetDirectoryName(Environment.ProcessPath) ?? String appPath = Path.GetDirectoryName(Environment.ProcessPath) ??
throw new Exception("Application path can not be found"); throw new Exception("Application Path can not be found");
String fullAppPath = Path.TrimEndingDirectorySeparator(Path.GetFullPath(appPath)); String fullAppPath = Path.TrimEndingDirectorySeparator(Path.GetFullPath(appPath));
using RegistryKey environmentKey = Registry.LocalMachine.OpenSubKey( using RegistryKey environmentKey = Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true) ?? @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true) ??
throw new Exception("Environment registry key can not be opened"); throw new Exception("Environment Registry Key can not be opened");
String? path = environmentKey.GetValue( String? path = environmentKey.GetValue(
"Path", null, RegistryValueOptions.DoNotExpandEnvironmentNames) as String ?? "Path", null, RegistryValueOptions.DoNotExpandEnvironmentNames) as String ??
throw new Exception("Path registry value is invalid"); throw new Exception("Path Registry Value is invalid");
List<String> paths = [.. path.Split(';', StringSplitOptions.RemoveEmptyEntries)]; List<String> paths = [.. path.Split(';', StringSplitOptions.RemoveEmptyEntries)];
for (Int32 i = 0; i < paths.Count; i++) { for (Int32 i = 0; i < paths.Count; i++) {
if (!String.Equals( if (!String.Equals(
@ -265,10 +265,8 @@ internal static class Commands {
environmentKey.SetValue("Path", String.Join(";", paths), RegistryValueKind.ExpandString); environmentKey.SetValue("Path", String.Join(";", paths), RegistryValueKind.ExpandString);
return 0; return 0;
} }
else { Console.Error.WriteLine("Invalid Path Command");
Console.Error.WriteLine("Invalid path command supplied"); return 1;
return 0;
}
} }
catch (Exception exception) { catch (Exception exception) {
Console.Error.WriteLine(exception.Message); Console.Error.WriteLine(exception.Message);
@ -306,7 +304,7 @@ internal static class Commands {
Description = "Output File", Description = "Output File",
DefaultValueFactory = _ => "-" DefaultValueFactory = _ => "-"
}; };
Command createCommand = new("create", "Create Photo") { Command createCommand = new("create", "Create a new Photo") {
formatArgument, jpegOption, descriptionOption, jsonOption, titleOption, outputOption formatArgument, jpegOption, descriptionOption, jsonOption, titleOption, outputOption
}; };
createCommand.SetAction(result => Environment.ExitCode = CreateFunction( createCommand.SetAction(result => Environment.ExitCode = CreateFunction(
@ -352,7 +350,7 @@ internal static class Commands {
Description = "Output File", Description = "Output File",
DefaultValueFactory = _ => "-" DefaultValueFactory = _ => "-"
}; };
Command getCommand = new("get", "Get Photo Data") { Command getCommand = new("get", "Get Data from a Photo") {
inputArgument, dataTypeArgument, outputOption inputArgument, dataTypeArgument, outputOption
}; };
getCommand.SetAction(result => Environment.ExitCode = GetFunction( getCommand.SetAction(result => Environment.ExitCode = GetFunction(
@ -389,7 +387,7 @@ internal static class Commands {
Option<String?> outputOption = new("--output", "-o") { Option<String?> outputOption = new("--output", "-o") {
Description = "Output File" Description = "Output File"
}; };
Command setCommand = new("set", "Set Photo Data") { Command setCommand = new("set", "Set Data from a Photo") {
inputArgument, formatOption, jpegOption, descriptionOption, jsonOption, titleOption, updateSignOption, outputOption inputArgument, formatOption, jpegOption, descriptionOption, jsonOption, titleOption, updateSignOption, outputOption
}; };
setCommand.SetAction(result => Environment.ExitCode = SetFunction( setCommand.SetAction(result => Environment.ExitCode = SetFunction(
@ -413,6 +411,12 @@ internal static class Commands {
commandArgument.CompletionSources.Add(_ => [ commandArgument.CompletionSources.Add(_ => [
new ("register"), new ("register"),
new ("unregister")]); new ("unregister")]);
commandArgument.Validators.Add(result => {
String[] commands = ["register", "unregister"];
String command = result.GetValueOrDefault<String>();
if (!commands.Contains(command, StringComparer.InvariantCultureIgnoreCase))
result.AddError("Invalid Path Command.");
});
Command pathCommand = new("path", "Register/Unregister Path") { Command pathCommand = new("path", "Register/Unregister Path") {
commandArgument commandArgument
}; };