/// <summary> /// Parses command line or batch file arguments and configures the /// <paramref name="executor" /> according to them. /// </summary> /// <param name="executor"> /// The <see cref="CommandExecutor" /> to be configured /// </param> /// <returns></returns> private static Args GetAppArguments(CommandExecutor executor) { var options = new Args(); var parser = new CommandLineParser(options); parser.Parse(); if (options.Help) { Exit(executor, parser, 0); } else if (parser.HasErrors) { Exit(executor, parser, 1); } #if DEBUG options.BatchFile = "debug.bnet"; options.AsService = 30; #endif // batch file or command-line? if (options.BatchFile != null) { if (!ParseBatchFile(options.BatchFile, executor)) { Exit(executor, parser, 1); } } else { executor.Servers = ParseServerUris(options.Servers); if (executor.Servers == null || !executor.Servers.Any()) { Console.WriteLine("No servers specified."); Exit(executor, parser, 1); } executor.Commands = options.Commands; if (executor.Commands == null || options.Commands.Count == 0) { Console.WriteLine("No commands specified."); Exit(executor, parser, 1); } } return options; }
private static void Exit(CommandExecutor executor, CommandLineParser parser, int errorLevel) { var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine(); sb.AppendLine(parser.UsageInfo.ToString(78, true)); sb.AppendLine(GetCommandsHelp(executor)); sb.AppendLine(); sb.AppendLine("Examples: bnet -b SampleBatch.bnet -svc 60"); sb.AppendLine( " bnet -u [email protected]:2302 -u [email protected]:3302 -svc 60 -c getplayers -c update_dbplayers"); sb.AppendLine(); sb.AppendLine("Press any key to exit..."); Console.Write(sb); Console.ReadKey(); Environment.Exit(errorLevel); }
private static bool ParseBatchFile(string batchFile, CommandExecutor executor) { var parser = new FileIniDataParser { CommentDelimiter = (char)0 }; IniData data; try { data = parser.LoadFile(batchFile); } catch (ParsingException pex) { Console.WriteLine(pex.Message); return false; } if (!ValidateIniSettingExists(data, "Servers") || !ValidateIniSettingExists(data, "Commands")) { return false; } // Get db settings even if we don't need to get the servers from it executor.DbConnectionString = GetIniDbConnectionString(data); if (IsIniTrue(data["Servers"]["UseBNetDb"])) { if (executor.DbConnectionString == null) { Console.WriteLine("Invalid or not specified connection string."); return false; } executor.Servers = executor.GetDbServers(); } else { var serverUris = from keyData in data["Servers"] where keyData.KeyName != "UseBNetDb" select keyData.Value; executor.Servers = ParseServerUris(serverUris); } if (executor.Servers == null || !executor.Servers.Any()) { Console.WriteLine("No servers specified."); return false; } executor.Commands = from keyData in data["Commands"] select keyData.Value; if (!executor.Commands.Any()) { Console.WriteLine("No commands specified."); return false; } return true; }
private static string GetCommandsHelp(CommandExecutor executor) { var sb = new StringBuilder(); sb.AppendLine("Available extra commands:"); foreach (var command in executor.BNetCommandsMetadata) { sb.AppendFormat("{0} - {1}", command.Key, command.Value.Description); sb.AppendLine(); } return sb.ToString(); }