moab
ProgOptions.hpp
Go to the documentation of this file.
00001 #ifndef MOAB_PROGRAM_OPTIONS_H
00002 #define MOAB_PROGRAM_OPTIONS_H
00003 
00004 #include <vector>
00005 #include <map>
00006 #include <string>
00007 #include <iostream>
00008 
00009 
00010 class ProgOpt;
00011 
00012 
00040 class ProgOptions{
00041 
00042 public:
00043 
00050 
00051 
00052   static const int help_flag = 1<<0;
00053 
00057   static const int add_cancel_opt = 1<<1;
00058 
00062   static const int store_false = 1<<2 ;
00063 
00067   static const int int_flag = 1<<3 ;
00068 
00074   static const int rank_subst = 1<<4;
00075 
00077   static const int version_flag = 1<<5;
00078   
00080   //const static int accept_multiple;
00081 
00082 
00087   ProgOptions( const std::string& helptext = "",
00088                const std::string& briefdesc = "" );
00089   ~ProgOptions();
00090 
00099   void setVersion( const std::string& version_string, bool addFlag = true );
00100 
00121   template <typename T>
00122   void addOpt( const std::string& namestring, const std::string& helpstring,
00123            T* value, int flags = 0 );
00124 
00130   template <typename T>
00131   void addOpt( const std::string& namestring, const std::string& helpstring, int flags = 0 ){
00132     addOpt<T>( namestring, helpstring, NULL, flags );
00133   }
00134 
00141   void addOptionHelpHeading( const std::string& );
00142 
00143 
00154   template <typename T>
00155   void addRequiredArg( const std::string& helpname, const std::string& helpstring, T* value = NULL, int flags = 0 );
00156 
00167   template <typename T>
00168   void addOptionalArgs( unsigned max_count, const std::string& helpname, const std::string& helpstring, int flags = 0 );
00169 
00173   void printHelp( std::ostream& str = std::cout );
00174 
00178   void printUsage( std::ostream& str = std::cout );
00179   
00183   void printVersion( std::ostream& str = std::cout );
00184 
00188   void parseCommandLine( int argc, char* argv[] );
00189 
00199   template <typename T>
00200   bool getOpt( const std::string& namestring, T* value );
00201 
00214   template <typename T>
00215   void getOptAllArgs( const std::string& namestring, std::vector<T>& values );
00216 
00217   
00222   int numOptSet( const std::string& namestring );
00223 
00229   template <typename T>
00230   T getReqArg( const std::string& namestring );
00231   
00237   template <typename T>
00238   void getArgs( const std::string& namestring, std::vector<T>& values );
00239 
00247   void error( const std::string& message );
00248 
00252   void write_man_page( std::ostream& to_this_stream );
00253   
00254 protected:
00255 
00256   std::string get_option_usage_prefix( const  ProgOpt& option );
00257 
00258   void get_namestrings( const std::string& input, std::string* l, std::string* s );
00259 
00260   ProgOpt* lookup( const std::map<std::string, ProgOpt* >&, const std::string& );
00261   ProgOpt* lookup_option( const std::string& );
00262 
00263   bool evaluate( const ProgOpt& opt, void* target, const std::string& option, unsigned* arg_idx = NULL);
00264   bool process_option( ProgOpt* opt, std::string arg, const char* value = 0 );
00265   
00266   std::map< std::string, ProgOpt* > long_names;
00267   std::map< std::string, ProgOpt* > short_names;
00268   std::map< std::string, ProgOpt* > required_args;
00269 
00270   typedef std::pair<ProgOpt*, std::string> help_line; 
00271   std::vector< help_line > option_help_strings;
00272   std::vector< help_line > arg_help_strings;
00273   std::vector< std::string > main_help;
00274   std::string brief_help;
00275   
00276   bool expect_optional_args;
00277   unsigned optional_args_position, max_optional_args;
00278   
00279   std::string progname;
00280   std::string progversion;
00281   
00282     // if an option was specified with the int_flag, this
00283     // will contain the long name of the option
00284   std::string number_option_name;
00285   
00286 };
00287 
00288 #endif /* MOAB_PROGRAM_OPTIONS_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines