From 296e195fae7204598c010a2efdcbb3a710825fcd Mon Sep 17 00:00:00 2001 From: Egezenn Date: Thu, 15 Oct 2026 12:09:56 +0300 Subject: [PATCH] Add --account CLI argument and improve help display - Add -a/--account option to select autologin account from command line - Store SelectedAccount in ProfileManager and use in AutoLogin - Clean up DisplayHelp to avoid duplicate --help and --version lines - Respect profile -> account -> character hierarchy in command line arguments --- Application/OasisBot/Program.cs | 61 +++++++++++++------ .../RSBot.Core/Components/ProfileManager.cs | 5 ++ Plugins/RSBot.General/Components/AutoLogin.cs | 10 +-- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/Application/OasisBot/Program.cs b/Application/OasisBot/Program.cs index d1654beb..25b99f58 100644 --- a/Application/OasisBot/Program.cs +++ b/Application/OasisBot/Program.cs @@ -42,6 +42,9 @@ public class CommandLineOptions [Option('p', "profile", Required = false, HelpText = "Set the profile name to use.")] public string Profile { get; set; } + [Option('a', "account", Required = false, HelpText = "Set the account name to use.")] + public string Account { get; set; } + [Option("launch-client", Required = false, HelpText = "Start with client")] public bool LaunchClient { get; set; } @@ -50,20 +53,30 @@ public class CommandLineOptions [Option("headless", Required = false, HelpText = "Start the bot without graphical user interface")] public bool Headless { get; set; } + + [Option('h', "help", Required = false, HelpText = "Show help message.")] + public bool Help { get; set; } } private static void DisplayHelp(ParserResult result) { - var helpText = HelpText.AutoBuild( - result, - h => - { - h.AdditionalNewLineAfterOption = false; - h.AddDashesToOption = true; - return HelpText.DefaultParsingErrorsHandler(result, h); - } + var helpText = new HelpText + { + Heading = new HeadingInfo(AssemblyTitle, AssemblyVersion), + AdditionalNewLineAfterOption = false, + AddDashesToOption = true, + }; + helpText.AddPreOptionsLine( + "Copyright (C) 2017-2021 ngoedde; 2021-2026 RSBot Team; 2026 Silkroad Developer Community" + ); + helpText.AddOptions(result); + + var lines = helpText.ToString().Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); + var filteredLines = lines.Where(line => + !line.TrimStart().StartsWith("--help") && !line.TrimStart().StartsWith("--version") ); - Console.WriteLine(helpText); + + Console.WriteLine(string.Join(Environment.NewLine, filteredLines)); } [STAThread] @@ -77,16 +90,19 @@ private static void Main(string[] args) parserResult .WithParsed(options => { + if (options.Help) + { + DisplayHelp(parserResult); + Environment.Exit(0); + } + RunOptions(options); isHeadless = options.Headless; }) .WithNotParsed(errs => { DisplayHelp(parserResult); - var isHelp = errs.Any(e => - e.Tag == ErrorType.HelpRequestedError || e.Tag == ErrorType.VersionRequestedError - ); - Environment.Exit(isHelp ? 0 : 1); + Environment.Exit(1); }); //CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; @@ -174,13 +190,20 @@ private static void RunOptions(CommandLineOptions options) ProfileManager.IsProfileLoadedByArgs = true; Log.Debug($"Selected profile by args: {profile}"); - } - if (!string.IsNullOrEmpty(options.Character)) - { - var character = options.Character; - ProfileManager.SelectedCharacter = character; - Log.Debug($"Selected character by args: {character}"); + if (!string.IsNullOrEmpty(options.Account)) + { + var account = options.Account; + ProfileManager.SelectedAccount = account; + Log.Debug($"Selected account by args: {account}"); + + if (!string.IsNullOrEmpty(options.Character)) + { + var character = options.Character; + ProfileManager.SelectedCharacter = character; + Log.Debug($"Selected character by args: {character}"); + } + } } } } diff --git a/Library/RSBot.Core/Components/ProfileManager.cs b/Library/RSBot.Core/Components/ProfileManager.cs index c9939997..6ca9e6d8 100644 --- a/Library/RSBot.Core/Components/ProfileManager.cs +++ b/Library/RSBot.Core/Components/ProfileManager.cs @@ -47,6 +47,11 @@ static ProfileManager() /// public static string SelectedCharacter { get; set; } + /// + /// The selected account + /// + public static string SelectedAccount { get; set; } + /// /// The selected profile /// diff --git a/Plugins/RSBot.General/Components/AutoLogin.cs b/Plugins/RSBot.General/Components/AutoLogin.cs index 5c0f567f..c44650d2 100644 --- a/Plugins/RSBot.General/Components/AutoLogin.cs +++ b/Plugins/RSBot.General/Components/AutoLogin.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Text; using System.Threading; @@ -51,9 +51,11 @@ public static async void Handle() return; } - var selectedAccount = Accounts.SavedAccounts?.Find(p => - p.Username == GlobalConfig.Get("RSBot.General.AutoLoginAccountUsername") - ); + var autoLoginUsername = !string.IsNullOrEmpty(ProfileManager.SelectedAccount) + ? ProfileManager.SelectedAccount + : GlobalConfig.Get("RSBot.General.AutoLoginAccountUsername"); + + var selectedAccount = Accounts.SavedAccounts?.Find(p => p.Username == autoLoginUsername); if (selectedAccount == null) { _busy = false;