options.h

Go to the documentation of this file.
00001 #ifndef options_h_included
00002 #define options_h_included
00003 
00004 
00005 //
00006 // Copyright 2002, Lowell Boggs Jr.
00007 //
00008 // This file or directory, containing source code for a computer program,
00009 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00010 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00011 // source file without charge or obligation so long as you agree to
00012 // the following:
00013 //
00014 //  1.  You must indemnify Lowell Boggs against any and all financial
00015 //      obligations caused by its use, misuse, function, or malfunction.
00016 //      Further, you acknowledge that there is no warranty of any kind,
00017 //      whatsoever.
00018 //
00019 //  2.  You agree not to attempt to patent any portion of this original
00020 //      work -- though you may attempt to patent your own extensions to
00021 //      it if you so choose.
00022 //
00023 //  3.  You keep this copyright notice with the file and all copies
00024 //      of the file and do not change it anyway except language translation.
00025 //
00026 // You are responsible for enforcing your own compliance with these
00027 // conditions and may not use this source file if you cannot agree to the
00028 // above terms and conditions.
00029 
00030 
00031 
00032 
00037 
00038 #include <string>
00039 #include <vector>
00040 #include <map>
00041 
00042 namespace cxxtls
00043 {
00044 
00045 class ProgramOptions
00247         
00248 {
00249 
00250 public:
00251 
00252  // types
00253 
00254     class                                  Descriptor;
00255     class                                  Descriptors;
00256     class                                  Option;
00257     class                                  EnvVar;
00258 
00259     typedef std::string                    string;
00260 
00261     typedef std::vector<string>            option_values;  
00262     typedef option_values::const_iterator  value_iterator; 
00263     typedef std::map<string,string>        Environment;    
00264 
00265     typedef std::map<string,   Option*>    Options;
00266         //
00271 
00272 
00273     static ProgramOptions*                 global_options_; 
00274 
00275     enum allowed_parms_
00278     {
00279       no_values,   
00280       one_value,   
00281       many_values  
00282 
00283     };
00284 
00285  // Public Variables
00286 
00287     option_values   argv;           
00288 
00289 
00290  // Methods
00291 
00292     ProgramOptions(Descriptors const &  desc,
00293                    int                  argc,
00294                    char const * const * argvp,
00295                    char const * const * environ,
00296                    bool                 exit_on_error=true
00297                   )
00298     : exit_on_error_(exit_on_error),
00299       error_(false)
00300     {
00301       constructor(desc, argc, argvp, environ, exit_on_error);
00302        //
00308        //
00309     }
00310 
00311 
00312     ProgramOptions(std::string          option_descriptor_string,
00313                    int                  argc,
00314                    char const * const * argvp,
00315                    char const * const * environ,
00316                    bool                 exit_on_error=true
00317                   );
00322        //
00323 
00324     ProgramOptions()
00325     : exit_on_error_(true),
00326       error_(false)
00327     {
00328        //
00330        //
00331     }
00332 
00333     ~ProgramOptions();       
00334                                 
00335     Option &add_option(string  const &name);
00341        //
00342     void remove_option(string const &name);
00343 
00345                                 
00346     Option const &option(string const &name) const;
00350        //
00351 
00352     bool error() const { return error_; }
00357         
00358     string const& error_text() const { return errmsg_; }
00364         
00365     EnvVar  getenv(string const &name) const;
00371         
00372     void setenv(string const &name, string const & value);
00377 
00378     Environment const& get_environment() const { return environ_; }
00383         
00384     Options const& get_options() const { return options_; }
00388         
00389     std::string expand(std::string const &) const;
00415         
00416     class Option
00417        //
00420     {
00421 
00422     public:
00423 
00424         Option(string const &name);             
00425         
00426         void append_value(string const &value); 
00427         
00428         value_iterator begin() const { return values_.begin(); }
00430         
00431         value_iterator end()   const { return values_.end(); }
00433         
00434         operator bool() const { return set_; }
00436 
00437         string value() const;
00439         
00440         operator string() const { return value(); } 
00442         
00443         string const& name() const { return name_; }  
00444         
00445     private:
00446 
00447         friend class ProgramOptions;
00448         
00449         Option(); 
00450 
00451 
00452         option_values values_;  
00453         
00454         string        name_;    
00455 
00456                         
00457         bool          set_;     
00458         
00459     };
00460 
00461 
00462     class Descriptor
00463         //
00466         //
00467     {
00468       public:
00469 
00470         string  name_;   
00471         int     parms_;  
00472 
00473 
00474         Descriptor(string const &name, int parms=no_values)
00479             //
00480         : name_(name),
00481           parms_(parms)
00482         {
00483         }
00484 
00485     };
00486 
00487 
00488     class EnvVar
00489         //
00504     {
00505       public:
00506 
00507         bool          set_;    
00508         string        name_;   
00509         string        value_;  
00510         
00511         EnvVar(string const &name)
00512         : set_(false),
00513           name_(name)
00514         {
00517         }
00518         
00519         EnvVar(string const& name, string const &value)
00520         :  set_(true),
00521            name_(name),
00522            value_(value)
00523         {
00526         };
00527         
00528         operator bool() const { return set_; }
00532         
00533         operator string const &() const { return value_; }
00537         
00538         string const &name() const { return name_; }
00540 
00541         char const* c_str() const { return value_.c_str(); }
00542     };
00543 
00544 
00545     class Descriptors
00546 
00552     {
00553     public:
00554 
00555         std::vector<Descriptor> options_; 
00556         
00557         void add_option(string const & name, int parms = no_values);
00558 
00559     };
00560 
00561 private:
00562 
00563     static Option  null_option_;  
00564 
00565     Options        options_;      
00566 
00567 
00568 
00569 
00570     bool   exit_on_error_;        
00571     string errmsg_;               
00572     bool   error_;                
00573 
00574     Environment environ_;         
00575 
00576     Option* get_option(string const&); 
00577 
00578     void constructor(Descriptors const &  desc,
00579                      int                  argc,
00580                      char const * const * argv,
00581                      char const * const * environ,
00582                      bool                 exit_on_error=true
00583                     );
00589 
00590 
00591 };
00592 
00593 } // namespace cxxtls
00594 
00595 #endif
00596 
Generated on Wed Feb 29 22:50:04 2012 for CXXUtilities by  doxygen 1.6.3