bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CmdLineOptions


From: David Sugar
Subject: Re: CmdLineOptions
Date: Tue, 11 Mar 2003 18:08:13 -0500
User-agent: KMail/1.4.3

Yes, the command line parser we have is a very complicated piece of code; 
perhaps overly so.  In fact, I have to admit I don't use it in Bayonne 
because it was overly complex :), and Bayonne is a BIG project which uses 
complicated command line option parsing!

I would like to think about this and see if there is anyone using the current 
parser.  If not, maybe this is something we want to adopt for the 1.1 
release.

On Thursday 06 March 2003 05:01 pm, Orlov, Yuriy wrote:
> Hello guys!
>
>       You are doing great job. Common C++ library is very usefull tool for
> developers.
> I think many programmers around the world spent many time to develop their
> own wrapers realization
> for system dependent calls, unfortunately including me.
>
> But one comment about your command line option mechanizm. I think it to
> complicated for most task that
> programmers sovling each day, and may be usefull just for big projects.
> Please look to my solution below.
>
> Regards,
> Yuri Orlov
>
> #include <map>
> #include <vector>
> #include <stdio.h>
>
> #define _opt_sign '-'
>
> /* Command line option class */
> class clopt
> {
> public:
>     clopt() :
>       name(NULL), value(NULL), format(NULL) {};
>     clopt(char *_name, void *_value,char *_format = NULL) :
>       name(_name), value(_value), format(_format) {};
>
>     bool operator () (const char* c1, const char* c2) const
>         { return (strcmp(c1,c2)<0); };
> protected:
>     char *name;
>     void *value;
>     char *format;
>
>     friend class cmdline;
> };
>
> /* Command line class */
> class cmdline
> {
> public:
>     cmdline& operator+(clopt &_o) {
>         olist[_o.name] = &_o;
>         return *this;
>     };
>     void extract(int argc, char* argv[]);
>     std::vector<char*> param;
>
> private:
>     std::map<char*,clopt*,clopt> olist;
> };
>
> inline void cmdline::extract(int argc, char* argv[])
> {
>     for (int i=0; i<argc; i++) {
>         if (argv[i][0] == _opt_sign) {
>             clopt *_o = olist[argv[i]+1];
>             if (_o)
>                 if (_o->format) {
>                     i++; if (i >= argc) return;
>                     sscanf(argv[i],_o->format,_o->value);
>                 } else *((bool *)_o->value) = true;
>         } else {
>             param.push_back(argv[i]);
>         }
>     }
> }
>
> // --------------------------------------------------------
> // Sample usage
> // --------------------------------------------------------
>
> #include <iostream>
> using namespace std;
> main(int argc, char* argv[])
> {
>     int  intval  = 1;     // Default value
>     bool boolval = false; // Default value
>     float floatval = -1.0;
>     cmdline cl;
>
>     cl +clopt("intval",&intval,"%d")
>        +clopt("boolval",&boolval)
>        +clopt("floatval",&floatval,"%f");
>     // .............
>
>     cl.extract(argc,argv);
>
>     cout << "intval=" << intval << endl
>          << "boolval=" << boolval << endl
>          << "floatval=" << floatval << endl;
>     return 0;
> }
>
>
>
>
> _______________________________________________
> Bug-commoncpp mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/bug-commoncpp





reply via email to

[Prev in Thread] Current Thread [Next in Thread]