Vikram_B
asked on
parsing commandline arguments of WinMain
Hi all,
I wanted a simple, failsafe way to get command line arguments from:
int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nCmdShow );
The arguments are 2 absolute file-paths and spaces(blanks) can be part of the file-paths. So, I cann't just search for blank space to find out the next token :)
thanks,
Vikram
I wanted a simple, failsafe way to get command line arguments from:
int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nCmdShow );
The arguments are 2 absolute file-paths and spaces(blanks) can be part of the file-paths. So, I cann't just search for blank space to find out the next token :)
thanks,
Vikram
The norm is to enclose the file paths with "" - for example :
command "/path/to first/file 1" "/path/to second/file 2"
Those will be passed as :
argument 1 : /path/to first/file 1
argument 2 : /path/to second/file 2
command "/path/to first/file 1" "/path/to second/file 2"
Those will be passed as :
argument 1 : /path/to first/file 1
argument 2 : /path/to second/file 2
ASKER
thanks Infinity08,
But, how to parse argument1 and argument2? can I use some string manipulation function?
But, how to parse argument1 and argument2? can I use some string manipulation function?
You can use whatever you want, but this is the basic idea :
char *cmdLine = (char*) lpCmdLine;
char args[128][128] = { 0 };
int i = 0;
while (*cmdLine) {
if (*cmdLine == ' ') {
cmdLine++;
}
else {
char *end = 0;
if (*cmdLine == '\"') {
cmdLine++;
end = strchr(cmdLine, '\"');
}
else {
end = strchr(cmdLine, ' ');
}
if (end) {
strncpy(args[i], cmdLine, end - cmdLine);
args[i][end - cmdLine] = '\0';
cmdLine = end + 1;
i++;
}
else {
/* ERROR - treat it !! */
}
}
}
Note that this is just for you to get an idea of how you COULD do it. This is ny no means complete code !!!
If more than 128 args are passed or if one arg is more than 127 characters long, this code will create problems !!
The above code is not validated, and is just written off the top of my head, so there might still be other problems with it !
char *cmdLine = (char*) lpCmdLine;
char args[128][128] = { 0 };
int i = 0;
while (*cmdLine) {
if (*cmdLine == ' ') {
cmdLine++;
}
else {
char *end = 0;
if (*cmdLine == '\"') {
cmdLine++;
end = strchr(cmdLine, '\"');
}
else {
end = strchr(cmdLine, ' ');
}
if (end) {
strncpy(args[i], cmdLine, end - cmdLine);
args[i][end - cmdLine] = '\0';
cmdLine = end + 1;
i++;
}
else {
/* ERROR - treat it !! */
}
}
}
Note that this is just for you to get an idea of how you COULD do it. This is ny no means complete code !!!
If more than 128 args are passed or if one arg is more than 127 characters long, this code will create problems !!
The above code is not validated, and is just written off the top of my head, so there might still be other problems with it !
References for the strcpy() and strchr() functions I used :
http://www.cplusplus.com/ref/cstring/strcpy.html
http://www.cplusplus.com/ref/cstring/strchr.html
http://www.cplusplus.com/ref/cstring/strcpy.html
http://www.cplusplus.com/ref/cstring/strchr.html
Maybe you want that (it is tested):
#include <string>
#include <vector>
using namespace std;
int parseCmdLine(const char* szcmdline, vector<string> arguments)
{
string cmdline = szcmdline;
cmdline += ' '; // add a final space for easier parsing
bool bQuotes = false;
int lpos = 0;
int i = 0;
for (i = 0; i < cmdline.length(); ++i)
{
char c = cmdline[i];
switch(c)
{
case '"': // note it is ' " '
bQuotes = !bQuotes; break;
case ' ':
if (bQuotes)
break; // ignore space if within quotes
if (i > lpos)
arguments.push_back(cmdlin e.substr(l pos, i - lpos));
lpos = i+1;
}
}
return arguments.size();
}
You can use it in your WinMain like
vector<string> arguments;
int nArgs = parseCmdLine( lpCmdLine, arguments );
for (int i = 0; i < nArgs; ++i)
cout << arguments[i] << endl;
Regards, Alex
#include <string>
#include <vector>
using namespace std;
int parseCmdLine(const char* szcmdline, vector<string> arguments)
{
string cmdline = szcmdline;
cmdline += ' '; // add a final space for easier parsing
bool bQuotes = false;
int lpos = 0;
int i = 0;
for (i = 0; i < cmdline.length(); ++i)
{
char c = cmdline[i];
switch(c)
{
case '"': // note it is ' " '
bQuotes = !bQuotes; break;
case ' ':
if (bQuotes)
break; // ignore space if within quotes
if (i > lpos)
arguments.push_back(cmdlin
lpos = i+1;
}
}
return arguments.size();
}
You can use it in your WinMain like
vector<string> arguments;
int nArgs = parseCmdLine( lpCmdLine, arguments );
for (int i = 0; i < nArgs; ++i)
cout << arguments[i] << endl;
Regards, Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks everyone!
sorry, i cudnt reply in time. i donno why, but i am not receiving notifications from ee for the new comments posted!
sorry, i cudnt reply in time. i donno why, but i am not receiving notifications from ee for the new comments posted!
yourProgram "C:\Program Files\Dir\File1.txt" "C:\Program Files\Dir\File2.txt"
This is general rule for all command parsers. Without this there is no way to detect number of parameters.