cgma
ProgOptions.hpp
Go to the documentation of this file.
00001 #ifndef MOAB_PROGRAM_OPTIONS_H
00002 #define MOAB_PROGRAM_OPTIONS_H
00003 
00004 /* THIS FILE WAS COPIED FROM THE MOAB LIBRARY */
00005 
00006 #include <vector>
00007 #include <map>
00008 #include <string>
00009 #include <iostream>
00010 
00011 
00012 class ProgOpt;
00013 
00014 
00042 class ProgOptions{
00043 
00044 public:
00045 
00052 
00053 
00054   static const int help_flag = 1<<0;
00055 
00059   static const int add_cancel_opt = 1<<1;
00060 
00064   static const int store_false = 1<<2 ;
00065 
00069   static const int int_flag = 1<<3 ;
00070 
00076   static const int rank_subst = 1<<4;
00077 
00079   static const int version_flag = 1<<5;
00080 
00083   static const int store_true = 1<<6 ;
00084   
00086   //const static int accept_multiple;
00087 
00088 
00093   ProgOptions( const std::string& helptext = "",
00094                const std::string& briefdesc = "" );
00095   ~ProgOptions();
00096 
00105   void setVersion( const std::string& version_string, bool addFlag = true );
00106 
00127   template <typename T>
00128   void addOpt( const std::string& namestring, const std::string& helpstring,
00129            T* value, int flags = 0 );
00130 
00136   template <typename T>
00137   void addOpt( const std::string& namestring, const std::string& helpstring, int flags = 0 ){
00138     addOpt<T>( namestring, helpstring, NULL, flags );
00139   }
00140 
00147   void addOptionHelpHeading( const std::string& );
00148 
00149 
00160   template <typename T>
00161   void addRequiredArg( const std::string& helpname, const std::string& helpstring, T* value = NULL, int flags = 0 );
00162 
00173   template <typename T>
00174   void addOptionalArgs( unsigned max_count, const std::string& helpname, const std::string& helpstring, int flags = 0 );
00175 
00179   void printHelp( std::ostream& str = std::cout );
00180 
00184   void printUsage( std::ostream& str = std::cout );
00185   
00189   void printVersion( std::ostream& str = std::cout );
00190 
00194   void parseCommandLine( int argc, char* argv[] );
00195 
00205   template <typename T>
00206   bool getOpt( const std::string& namestring, T* value );
00207 
00220   template <typename T>
00221   void getOptAllArgs( const std::string& namestring, std::vector<T>& values );
00222 
00223   
00228   int numOptSet( const std::string& namestring );
00229 
00235   template <typename T>
00236   T getReqArg( const std::string& namestring );
00237   
00243   template <typename T>
00244   void getArgs( const std::string& namestring, std::vector<T>& values );
00245 
00253   void error( const std::string& message );
00254 
00258   void write_man_page( std::ostream& to_this_stream );
00259   
00260 protected:
00261 
00262   std::string get_option_usage_prefix( const  ProgOpt& option );
00263 
00264   void get_namestrings( const std::string& input, std::string* l, std::string* s );
00265 
00266   ProgOpt* lookup( const std::map<std::string, ProgOpt* >&, const std::string& );
00267   ProgOpt* lookup_option( const std::string& );
00268 
00269   bool evaluate( const ProgOpt& opt, void* target, const std::string& option, unsigned* arg_idx = NULL);
00270   bool process_option( ProgOpt* opt, std::string arg, const char* value = 0 );
00271   
00272   std::map< std::string, ProgOpt* > long_names;
00273   std::map< std::string, ProgOpt* > short_names;
00274   std::map< std::string, ProgOpt* > required_args;
00275 
00276   typedef std::pair<ProgOpt*, std::string> help_line; 
00277   std::vector< help_line > option_help_strings;
00278   std::vector< help_line > arg_help_strings;
00279   std::vector< std::string > main_help;
00280   std::string brief_help;
00281   
00282   bool expect_optional_args;
00283   unsigned optional_args_position, max_optional_args;
00284   
00285   std::string progname;
00286   std::string progversion;
00287   
00288     // if an option was specified with the int_flag, this
00289     // will contain the long name of the option
00290   std::string number_option_name;
00291   
00292 };
00293 
00294 #endif /* MOAB_PROGRAM_OPTIONS_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines