Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CoMaNdore

Easy way to handle comand line parameters ?

This topic is 5228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a app, lets call it, foo.exe Now foo.exe can handle many diffrent things and these are spesified tru the cmdline parameters. Like: "foo -n test -j 300 -o dosomthing" Its leagal to call ''foo'' using any sequence of the parameters. Example: "foo -o dosomthing -n test -j 300" Its also leagal to give any number of legal parameters. Example: "foo -n test" "foo -o dosomthingelse -p 99" Now the methods I have been using to find these parameters have not been pretty. I wonder if there is any standard way of doing this? If not exatly standard any clean way will be helpfull. As im kinda tired of my "hack ''n slash" methods of doing this.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Link your program with a parser like flex or something...
Killing a fly with an axe? Yes, but it''s cool

Share this post


Link to post
Share on other sites
I''m not sure if you are using int WINAPI WinMain or simply int Main.

If you use the latter you can do this:

#include <string.h>
int Main(int argc, char** argv)
{
// strcmp(argv[0],"path + foo.exe") == true
for(int i = 1; i>argc;i++)
{
if(strcmp(argv, "-n"))
{
// Do your things you do with -n like:
char n[] = **argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-j"))
{
// Do your things
char j[] = **argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-o"))
{
// Do your things
char o[] = **argv[i + 1];
i++;
}
}
/* The rest of your program where you analize n, j and o if those are parameters. */
return 0;
}


In my example I assumed "test", "300" and "dosomething" are variables you give to the arguments "-n", "-j" and "-o".

Hope that helps.


Creator of: Science Fiction Career
Percent finnished: 0.1%

Site: http://www.nightsoftware.com/scificareer
Forum: comming soon

Share this post


Link to post
Share on other sites
quote:
Original post by CoMaNdore
I wonder if there is any standard way of doing this?
No.

quote:
If not exatly standard any clean way will be helpfull.
Loop through your parameters; for a given "flag" parameter (-<letter>) the next parameter must be of a given type. Validate.

[Edit: Blasted formatting.]

[edited by - Oluseyi on February 27, 2004 4:57:28 PM]

Share this post


Link to post
Share on other sites
I found this method in the Quake2 source code.


#define NUM_CMDS 5
char* GRcmds[]={"-scale","-angle","-gen4","-gen8","-gen16"};
static bool GRgenrot = false;
static int GRnumrots = -1;
static float GRangle = 0; //default

static float GRscale = 1; //default

//Commandline processing inspired from quake2 source

void processCL(int iArgc,char* pArgv[])
{
int res;

for(int x = 0;x < iArgc;x++)
{
for(int c=0;c < NUM_CMDS;c++)
{
res = stricmp(GRcmds[c],pArgv[x]);
if(0==res){
switch(c){
case 0:
GRscale = atof(pArgv[x+1]);
break;
case 1:
GRangle = atof(pArgv[x+1]);
break;
case 2:
GRgenrot = true;
GRnumrots = 4;
break;
case 3:
GRgenrot = true;
GRnumrots = 8;
break;
case 4:
GRgenrot = true;
GRnumrots = 16;
break;
default:
break;
} //end switch

}//end if

}//loop through all possible commands

}//loop though all command line arguments

}


[edited by - Jack Sotac on February 27, 2004 5:10:17 PM]

Share this post


Link to post
Share on other sites
The getopt family of functions are the standard way to do this on Unix platforms (and the option style you showed was like that). If you''re not targetting Unix, you can find the source code to an implementation and rebuild it for your target platform as, say, a library; that''s what I did.

Share this post


Link to post
Share on other sites
First, thanks for all the replies I got.

mldaalder: that was almost way I was doing it before.

I ended creating a class for it and placed in my utility lib.
So i can easy use it in small apps i create.
And it work great for me.
*note I got some more overloaded methods of Parse()*

edit: fixed some spelling

class CommandLineParser
{
public:
/*
Parse()
parses a string you can retrive the options
returns FALSE on error, TRUE on succes
*/

BOOL Parse( std::string strCmd );

/*
Check if the strCmd got the flag spesified by @strFlag
return FALSE if the flag is not set.
*/

BOOL HasFlag( const std::string& strFlag ) const;

/*
returns the value spesified by @strFlag.
if the flag in not present the methods return:
int - 0
uint - 0
float - 0.0f
string - ""
*/

int GetFlagValueAsInt ( std::string& strFlag ) const;
uint GetFlagValueAsUint ( std::string& strFlag ) const;
float GetFlagValueAsFloat ( strd::string& strFlag ) const;
string GetFlagValueAsString( strd::string& strFlag ) const;



[edited by - CoMaNdore on February 27, 2004 8:24:40 PM]

[edited by - CoMaNdore on February 27, 2004 9:32:58 PM]

Share this post


Link to post
Share on other sites
I would also suggest getopt(), very easy to use and works great


------------------------------------------------------------
// TODO: Insert clever comment here.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!