Slice Tools
  • Home
  • SourceForge Page


  • libFoundation
  • Home
  • Class Hierarchy
  • Class List
  • Class Members
  • Examples


  • SourceForge.net Logo
     

    Options.hh

    Go to the documentation of this file.
    00001 // $Id: Options.hh,v 1.1 2005/07/29 02:55:15 mschatz Exp $
    00002 
    00005 
    00006 #ifndef OPTIONS_HH
    00007 #define OPTIONS_HH 1
    00008 
    00009 #include <list>
    00010 #include <map>
    00011 #include <set>
    00012 #include <string>
    00013 
    00014 #ifdef _HAS_GETOPT
    00015 #include <getopt.h>
    00016 #else
    00017 #include "getopt.hh"
    00018 #endif
    00019 
    00020 #include "Exceptions.hh"
    00021 #include "OptionResult.hh"
    00022 
    00024 typedef std::map<std::string, OptionResult *> OptionsMap;
    00025 
    00027 typedef OptionsMap::iterator OptionsMapIter;
    00028 
    00030 typedef OptionsMap::const_iterator OptionsMapCIter;
    00031 
    00032 
    00034 typedef std::map<std::string, int> LongOptionsMap;
    00035 
    00037 typedef LongOptionsMap::const_iterator LongOptionsMapCIter;
    00038 
    00039 
    00041 typedef std::list<std::string> StringList;
    00043 typedef StringList::const_iterator StringListCIter;
    00044 
    00046 typedef std::set<std::string> StringSet;
    00048 typedef StringSet::const_iterator StringSetCIter;
    00049 
    00051 class Options
    00052 {
    00053 public:
    00054   Options(int argc, char ** argv);
    00055   ~Options();
    00056 
    00057   // Simple "getters"
    00058   std::string getApplicationFilespec();
    00059   std::string getApplicationName();
    00060   std::string getInvocation();
    00061 
    00062   // Functions for adding options
    00063   void addOption(const char * optionName, int has_arg,
    00064                  const char * alias = NULL,
    00065                  const std::string & optionHelp = "");
    00066   
    00067   // For adding options with results
    00068   void addOptionResult(const std::string & optionArgument,
    00069                        int * resultPointer,
    00070                        const std::string & optionHelp = "");
    00071 
    00072   void addOptionResult(const std::string & optionArgument,
    00073                        float * resultPointer,
    00074                        const std::string & optionHelp = "");
    00075 
    00076   void addOptionResult(const std::string & optionArgument,
    00077                        std::string * resultPointer,
    00078                        const std::string & optionHelp = "");
    00079 
    00080   // For Option results
    00081   int         hasOption(const std::string & optionName);
    00082   std::string getOption(const std::string & optionName);
    00083   std::string getNextOption();
    00084 
    00085   // Command line arguments that are not options
    00086   std::string getNextOtherData();
    00087   StringList  getAllOtherData();
    00088 
    00089   // Utility Functions
    00090   void standardOptionsComplete();
    00091   void printHelp();
    00092   void parseOptions();
    00093 
    00094 private:
    00095   void verifyName(const std::string & alias);
    00096   void parseArgument(const std::string & optionArgument,
    00097                      OptionResult * optionResult);
    00098 
    00099   // Insert options into appropriate lists
    00100   void insertOptionResult(OptionResult * optionResult);
    00101 
    00102   void insertLongOption(const std::string & longOptionName, int has_arg);
    00103   void insertShortOption(const std::string & shortOptionName, int has_arg);
    00104 
    00105   // Long Option array Utility
    00106   struct option * getLongOptionsArr();
    00107   void deleteLongOptionsArr(struct option * longOptionsArr);
    00108 
    00109   // Member vars
    00110 
    00111   bool m_optionsParsed;
    00112   bool m_standardOptionsComplete;
    00113 
    00114   int m_argc;
    00115   char ** m_argv;
    00116 
    00117   std::string m_applicationFilespec;
    00118   std::string m_applicationName;
    00119   std::string m_invocation;
    00120 
    00121   std::string m_shortOptions;
    00122   LongOptionsMap m_longOptions;
    00123 
    00124   OptionsMap m_optionResults;
    00125   OptionsMapCIter m_optionIterator;
    00126 
    00127   StringSet m_optionNames;
    00128   StringSet m_standardOptionNames;
    00129 
    00130   StringList m_otherOptionData;
    00131   StringListCIter m_otherOptionDataIterator;
    00132 };
    00133 
    00134 
    00135 
    00136 #endif