Easy way to handle comand line parameters ?

Started by
7 comments, last by CoMaNdore 20 years, 1 month ago
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.
- Me
Advertisement
Not surprisingly, Google returned quite a few hits.

*yawn*


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Link your program with a parser like flex or something...
Killing a fly with an axe? Yes, but it''s cool
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;<br>            i++;<br>        }<br>        else if(strcmp(argv, "-j"))<br>        {<br>            // Do your things<br>            char j[] = **argv;<br>            i++;<br>        }<br>        else if(strcmp(argv, "-o"))<br>        {<br>            // Do your things<br>            char o[] = **argv;<br>            i++;<br>        }<br>    }<br>    /* The rest of your program where you analize n, j and o if those are parameters. */<br>    return 0;<br>} </pre> <br><br>In my example I assumed "test", "300" and "dosomething" are variables you give to the arguments "-n", "-j" and "-o".<br><br>Hope that helps.  </i>  <br><br>Creator of: Science Fiction Career<br>Percent finnished: 0.1%<br><br>Site: http://www.nightsoftware.com/scificareer<br>Forum: comming soon   
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]
I found this method in the Quake2 source code.

#define NUM_CMDS 5char* GRcmds[]={"-scale","-angle","-gen4","-gen8","-gen16"};static bool  GRgenrot = false;static int  GRnumrots = -1;static float GRangle  = 0; //defaultstatic float GRscale  = 1; //default//Commandline processing inspired from quake2 sourcevoid 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]
0xa0000000
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.
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]
- Me
I would also suggest getopt(), very easy to use and works great


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

This topic is closed to new replies.

Advertisement