[bot] Implement command line parser

This commit is contained in:
RD42 2023-10-22 01:43:00 +08:00
parent 7a0c525559
commit af4346b14b
5 changed files with 118 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View File

@ -114,6 +114,9 @@
<File
RelativePath=".\main.cpp">
</File>
<File
RelativePath=".\main.h">
</File>
<File
RelativePath=".\resource.h">
</File>

View File

@ -1,7 +1,98 @@
int main()
#include "main.h"
GAME_SETTINGS tSettings;
void InitSettingsFromCommandLine(char * szCmdLine);
//----------------------------------------------------
/*
Argument format:
"-h %s -p %d -n %s -m %s"
-h -> either server's bind address or 127.0.0.1
-p -> server's port number
-n -> NPC's name
-m -> script name
*/
int main (int argc, char** argv)
{
char szCmdLine[1024];
memset(szCmdLine,0,1024);
int cmdcnt=1;
if(argc > 1) {
while(cmdcnt != argc) {
strcat(szCmdLine, argv[cmdcnt]);
strcat(szCmdLine, " ");
cmdcnt++;
}
}
InitSettingsFromCommandLine(szCmdLine);
// TODO: main
// Absolutely no indication it is named npc or bot...
return 0;
}
}
//----------------------------------------------------
void SetStringFromCommandLine(char* szCmdLine, char* szString);
void InitSettingsFromCommandLine(char * szCmdLine)
{
logprintf(szCmdLine);
memset(&tSettings,0,sizeof(GAME_SETTINGS));
while(*szCmdLine) {
if(*szCmdLine == '-' || *szCmdLine == '/') {
szCmdLine++;
switch(*szCmdLine) {
case 'z':
szCmdLine++;
SetStringFromCommandLine(szCmdLine,tSettings.szConnectPass);
break;
case 'h':
szCmdLine++;
SetStringFromCommandLine(szCmdLine,tSettings.szConnectHost);
break;
case 'p':
szCmdLine++;
SetStringFromCommandLine(szCmdLine,tSettings.szConnectPort);
break;
case 'n':
szCmdLine++;
SetStringFromCommandLine(szCmdLine,tSettings.szNickName);
break;
case 'm':
szCmdLine++;
SetStringFromCommandLine(szCmdLine,tSettings.szModeName);
break;
}
}
szCmdLine++;
}
}
//----------------------------------------------------
void SetStringFromCommandLine(char* szCmdLine, char* szString)
{
while(*szCmdLine == ' ') szCmdLine++;
while(*szCmdLine &&
*szCmdLine != ' ' &&
*szCmdLine != '-' &&
*szCmdLine != '/')
{
*szString = *szCmdLine;
szString++; szCmdLine++;
}
*szString = '\0';
}
//----------------------------------------------------

22
bot/main.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#endif
#define MAX_SETTINGS_STRING 256
typedef struct _GAME_SETTINGS {
CHAR szConnectPass[MAX_SETTINGS_STRING+1];
CHAR szConnectHost[MAX_SETTINGS_STRING+1];
CHAR szConnectPort[MAX_SETTINGS_STRING+1];
CHAR szNickName[MAX_SETTINGS_STRING+1];
CHAR szModeName[MAX_SETTINGS_STRING+1];
} GAME_SETTINGS;
#include <stdio.h>
#include <stdlib.h>