Skip to content

Commit

Permalink
Use consistent method for checking validity of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Feb 7, 2022
1 parent a0b6dec commit 940e3cc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 65 deletions.
138 changes: 80 additions & 58 deletions EOBot/ArgumentsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public enum ArgsError
InvalidSimultaneousNumberOfBots,
InvalidWaitFlag,
InvalidInitDelay,
InvalidPath
InvalidPath,
InvalidScriptArgs,
AutoConnectRequired
}

public class ArgumentsParser
Expand All @@ -42,80 +44,100 @@ public class ArgumentsParser

public List<string> UserArgs { get; internal set; }

public bool ExtendedHelp { get; private set; }


public ArgumentsParser(string[] args)
{
InitDelay = 1100;

Error = ArgsError.NoError;

if (args.Length < 5)
if (args.Select(x => x.ToLower()).Any(x => x == "help"))
{
Error = ArgsError.WrongNumberOfArgs;
return;
ExtendedHelp = true;
}

for (int i = 0; i < args.Length; i++)
else
{
var arg = args[i];

if (arg == "--")
for (int i = 0; i < args.Length; i++)
{
UserArgs = new List<string>();
for (i = i + 1; i < args.Length; i++)
var arg = args[i];

if (arg == "--")
{
UserArgs.Add(args[i]);
UserArgs = new List<string>();
for (i = i + 1; i < args.Length; i++)
{
UserArgs.Add(args[i]);
}
break;
}
break;
}

var pair = arg.ToLower().Split('=');
var pair = arg.ToLower().Split('=');

if (pair.Length != 2)
{
Error = ArgsError.BadFormat;
return;
if (pair.Length != 2)
{
Error = ArgsError.BadFormat;
return;
}

switch (pair[0])
{
case "script":
if (!File.Exists(pair[1]))
{
Error = ArgsError.InvalidPath;
return;
}
ScriptFile = pair[1];
break;
case "autoconnect":
AutoConnect = bool.Parse(pair[1]);
break;
case "host":
ParseHost(pair[1]);
break;
case "port":
if (!ParsePort(pair[1]))
return;
break;
case "bots":
if (!ParseNumBots(pair))
return;
break;
case "initdelay":
if (!ParseInitDelay(pair[1]))
return;
break;
case "account":
Account = pair[1];
break;
case "password":
Password = pair[1];
break;
case "character":
Character = pair[1];
break;
default:
Error = ArgsError.BadFormat;
return;
}
}

switch (pair[0])
if (ScriptFile == null)
{
case "script":
if (!File.Exists(pair[1]))
{
Error = ArgsError.InvalidPath;
return;
}
ScriptFile = pair[1];
break;
case "autoconnect":
AutoConnect = bool.Parse(pair[1]);
break;
case "host":
ParseHost(pair[1]);
break;
case "port":
if (!ParsePort(pair[1]))
return;
break;
case "bots":
if (!ParseNumBots(pair))
return;
break;
case "initdelay":
if (!ParseInitDelay(pair[1]))
return;
break;
case "account":
Account = pair[1];
break;
case "password":
Password = pair[1];
break;
case "character":
Character = pair[1];
break;
default:
Error = ArgsError.BadFormat;
return;
if (Host == null || Port == 0 || NumBots == 0 || Account == null || Password == null || Character == null)
{
Error = ArgsError.WrongNumberOfArgs;
}
else if (UserArgs != null || !AutoConnect)
{
Error = ArgsError.InvalidScriptArgs;
}
}
else if (NumBots > 1 && ScriptFile != null && !AutoConnect)
{
Error = ArgsError.AutoConnectRequired;
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions EOBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static async Task<int> Main(string[] args)

ArgumentsParser parsedArgs = new ArgumentsParser(args);

if (parsedArgs.Error != ArgsError.NoError)
if (parsedArgs.Error != ArgsError.NoError || parsedArgs.ExtendedHelp)
{
ShowError(parsedArgs);
return 1;
Expand All @@ -200,12 +200,6 @@ static async Task<int> Main(string[] args)
botFactory = new TrainerBotFactory(parsedArgs);
}

if (parsedArgs.NumBots > 1 && parsedArgs.ScriptFile != null && !parsedArgs.AutoConnect )
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, "AutoConnect is required when using a script with more than 1 bot due to eoserv connection throttling");
return 1;
}

ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, "Starting bots...");

try
Expand Down Expand Up @@ -280,6 +274,12 @@ static void ShowError(ArgumentsParser args)
case ArgsError.InvalidPath:
Console.WriteLine("Invalid: Script file does not exist or is not a valid path.");
break;
case ArgsError.InvalidScriptArgs:
Console.WriteLine("Invalid: User-defined arguments and disabling autoconnect require a script.");
break;
case ArgsError.AutoConnectRequired:
Console.WriteLine("Invalid: AutoConnect is required when using a script with more than 1 bot due to eoserv connection throttling.");
break;
}

Console.WriteLine("\n\nUsage: (enter arguments in any order) (angle brackets is entry) (square brackets is optional)");
Expand All @@ -303,6 +303,10 @@ static void ShowError(ArgumentsParser args)
Console.WriteLine("\t script: script file to execute\n\t if script is not specified, default trainer bot will be used");
Console.WriteLine("\t autoconnect: (default true) true to automatically connect/disconnect to server with initDelay timeout between connection attempts for bots, false otherwise");
Console.WriteLine("\t --: Any arguments passed after '--' will be available in a script under the '$args' array");

if (!args.ExtendedHelp)
return;

Console.WriteLine(@"
===============================================================
Bot Script Info
Expand Down

0 comments on commit 940e3cc

Please sign in to comment.