Jump to content

[CODE] Parsing Command Line in C++/Windows/Visual Studio


Qai
 Share

Recommended Posts

The following is example native C++/Windows code how to parse the command line in Windows. It is meant to be used with the Standard version of LW if you are re-compiling the exe and would like this feature. The code is from my own project not connected with LW, so just treat the references to DX/GL selection as an example of how to use the method.

 

Having a command line parser is very useful for adding options to show a debugging window or write to a log, etc. Literally anything you want to pass via the command line to your main program. This makes it easier to create several shortcuts to your exe that invokes the application in different ways.

 

#include <Windows.h>
#include <stdio.h>

bool parselpCmdLine(bool &DXSelected, bool &GLSelected, bool &engineDebugActive, int &debugWindow_WIDTH, int &debugWindow_HEIGHT, bool &engineLogActive)
{
LPWSTR *szArgList;
int nArgs;

LPWSTR argCmdLineDX = L"-dx";
LPWSTR argCmdLineGL = L"-gl";

LPWSTR argCmdLineDebug = L"-debug";

LPWSTR argCmdLineLog = L"-log";

szArgList = CommandLineToArgvW(GetCommandLineW(), &nArgs);

// program requires at least one argument for either DirectX (-dx) or OpenGL (-gl)
if (nArgs > 1)
{
// parse for DirectX (-dx) or OpenGL (-gl)
// required: yes
// number of arguments: 1
for (int i = 1; i < nArgs; i++)
{
if (lstrcmpi(szArgList[i], argCmdLineDX) == 0)
{
if (DXSelected == false)
{
DXSelected = true;
}
}
else if (lstrcmpi(szArgList[i], argCmdLineGL) == 0)
{
if (GLSelected == false)
{
GLSelected = true;
}
}
}

if ((DXSelected == false) && (GLSelected == false))
{
MessageBox(NULL, L"No selection for graphics type DirectX (-dx) or OpenGL (-gl) indicated.", L"Command Line Error", MB_OK | MB_ICONERROR);
LocalFree(szArgList);
return false;
}

if ((DXSelected == true) && (GLSelected == true))
{
MessageBox(NULL, L"You cannot select both DirectX (-dx) and OpenGL (-gl).", L"Command Line Error", MB_OK | MB_ICONERROR);
LocalFree(szArgList);
return false;
}

// parse for debugging information
// argument syntax: -debug window_WIDTH window_HEIGHT
// argument types: window_WIDTH = integer, window_HEIGHT = integer
// required: optional
// number of arguments: 3
for (int i = 1; i < nArgs; i++)
{
if (lstrcmpi(szArgList[i], argCmdLineDebug) == 0)
{
engineDebugActive = true;
swscanf_s(szArgList[i + 1], (L"%d"), &debugWindow_WIDTH);
swscanf_s(szArgList[i + 2], (L"%d"), &debugWindow_HEIGHT);
break;
}
}

// parse for logging information
// required: optional
// number of arguments: 1
for (int i = 1; i < nArgs; i++)
{
if (lstrcmpi(szArgList[i], argCmdLineLog) == 0)
{
engineLogActive = true;
break;
}
}

}
else
{
MessageBox(NULL, L"No selection for graphics type DirectX (-dx) or OpenGL (-gl) indicated.", L"Command Line Error", MB_OK | MB_ICONERROR);
LocalFree(szArgList);
return false;
}

LocalFree(szArgList);

return true;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...