Add support for command line arguments

This commit is contained in:
Stone_Red
2023-12-17 15:07:01 +01:00
parent a44d8ebe73
commit 67b905dffa
2 changed files with 23 additions and 16 deletions
+15 -11
View File
@@ -22,7 +22,7 @@ internal class InputHandler
ClientCommands clientCommands = new ClientCommands(); ClientCommands clientCommands = new ClientCommands();
LogCommands logCommands = new LogCommands(); LogCommands logCommands = new LogCommands();
_ = commander.Register(input => commander.PrintHelp(input), "help"); _ = commander.Register(input => commander.PrintHelp(input), (HelpText)"Lists all commands.", "help", "h", "?");
_ = commander.Register(_ => Console.Clear(), (HelpText)"Clears the console.", "clear", "cls"); _ = commander.Register(_ => Console.Clear(), (HelpText)"Clears the console.", "clear", "cls");
_ = commander.Register(_ => exit = true, (HelpText)"Exits the application.", "exit", "quit"); _ = commander.Register(_ => exit = true, (HelpText)"Exits the application.", "exit", "quit");
_ = commander.Register(clientCommands.ShowInfo, (HelpText)"Displays the private and public IP address.", "info", "inf"); _ = commander.Register(clientCommands.ShowInfo, (HelpText)"Displays the private and public IP address.", "info", "inf");
@@ -66,19 +66,23 @@ internal class InputHandler
Console.CursorVisible = true; Console.CursorVisible = true;
string input = Console.ReadLine()?.Trim() ?? string.Empty; string input = Console.ReadLine()?.Trim() ?? string.Empty;
ExecuteInput(input);
}
}
Console.CursorVisible = false; public void ExecuteInput(string input)
try {
Console.CursorVisible = false;
try
{
if (!commander.Execute(input, out _))
{ {
if (!commander.Execute(input, out _)) ConsoleExt.WriteLine("Unknown command! Use `help` to list all commands.", ConsoleColor.Red);
{
ConsoleExt.WriteLine("Unknown command!", ConsoleColor.Red);
}
}
catch (Exception ex)
{
ConsoleExt.WriteLine($"An unexpected error occurred! {ex}", ConsoleColor.Red);
} }
} }
catch (Exception ex)
{
ConsoleExt.WriteLine($"An unexpected error occurred! {ex}", ConsoleColor.Red);
}
} }
} }
+8 -5
View File
@@ -53,12 +53,15 @@ internal static class Program
{ {
HyperbolicDownloaderApi.Commands.DownloadCommands downloadCommands = new HyperbolicDownloaderApi.Commands.DownloadCommands(apiManager.HostsManager, apiManager.FilesManager); HyperbolicDownloaderApi.Commands.DownloadCommands downloadCommands = new HyperbolicDownloaderApi.Commands.DownloadCommands(apiManager.HostsManager, apiManager.FilesManager);
downloadCommands.GetFileFrom(args[0]); downloadCommands.GetFileFrom(args[0]);
Console.WriteLine("Do you want to continue using this instance? [y/N]"); _ = Console.ReadLine();
if (char.ToLower(Console.ReadKey().KeyChar) != 'y') return;
{ }
return; else if (args.Length > 0)
} {
ConsoleExt.WriteLine("Warning! You are using command line arguments, which can lead to unexpected behavior with some commands.", ConsoleColor.Yellow);
Console.WriteLine(); Console.WriteLine();
inputHandler.ExecuteInput(string.Join(' ', args));
return;
} }
await Initialize(); await Initialize();