COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_full/include/coopy/Options.h
Go to the documentation of this file.
00001 #ifndef COOPY_OPTIONS_H
00002 #define COOPY_OPTIONS_H
00003 
00004 #include <string>
00005 #include <vector>
00006 #include <map>
00007 
00008 #include <coopy/CompareFlags.h>
00009 #include <coopy/PolyBook.h>
00010 
00011 namespace coopy {
00015   namespace app {
00016     class Option;
00017     class Example;
00018     typedef Example Recipe;
00019     class Options;
00020   }
00021 }
00022 
00023 enum {
00024   OPTION_FOR_DIFF = 1,
00025   OPTION_FOR_PATCH = 2,
00026   OPTION_FOR_MERGE = 4,
00027   OPTION_FOR_FORMAT = 8,
00028   OPTION_FOR_REDIFF = 16,
00029   OPTION_FOR_RESOLVE = 32,
00030   OPTION_FOR_COOPY = 64,
00031   OPTION_FOR_COMPARE = OPTION_FOR_DIFF|OPTION_FOR_MERGE,
00032   OPTION_FOR_ALL = 127,
00033   OPTION_PATCH_FORMAT = 128,
00034 };
00035 
00036 class coopy::app::Option {
00037 public:
00038   std::string long_name;
00039   std::string arg;
00040   std::string desc;
00041   int coverage;
00042   bool is_default;
00043 
00044   Option() {
00045     coverage = 0;
00046     is_default = false;
00047   }
00048 };
00049 
00050 class coopy::app::Example {
00051 public:
00052   std::string code;
00053   std::string desc;
00054   std::vector<std::string> reqs;
00055 
00056   Example& require(std::string req) {
00057     reqs.push_back(req);
00058     return *this;
00059   }
00060 };
00061 
00067 class coopy::app::Options {
00068 public:
00069   Options();
00070 
00071   int apply(int argc, char *argv[]);
00072 
00073   const std::vector<std::string>& getCore() const { return core; }
00074   std::vector<std::string>& getCoreMod() { return core; }
00075   const coopy::cmp::CompareFlags& getCompareFlags() const { return flags; }
00076 
00077   bool checkBool(const char *name, bool fallback=false) const { 
00078     if (option_bool.find(name)==option_bool.end()) return fallback;
00079     return option_bool.find(name)->second; 
00080   }
00081 
00082   void setBool(const char *name, bool val) {
00083     option_bool[name] = val;
00084   }
00085 
00086   void addStringToList(const char *name, const char *val) {
00087     if (option_list.find(name)==option_list.end()) {
00088       option_list[name] = std::vector<std::string>();
00089     }
00090     option_list[name].push_back(val);
00091   }
00092 
00093   std::string checkString(const char *name, const char *fallback="") const { 
00094     if (option_string.find(name)==option_string.end()) return fallback;
00095     return option_string.find(name)->second; 
00096   }
00097 
00098   bool isStringList(const char *name) const {
00099     return option_list.find(name)!=option_list.end();
00100   }
00101 
00102   const std::vector<std::string>& getStringList(const char *name) const {
00103     return option_list.find(name)->second;
00104   }
00105 
00106   bool isVerbose() const { return checkBool("verbose"); }
00107 
00108   bool isHelp() const { return checkBool("help"); }
00109 
00110   Options(const char *name);
00111 
00112   bool isDiffLike() const {
00113     return (name=="ssdiff"||name=="ssrediff"||name=="sspatch"||name=="ssresolve"||(name=="ssformat"));
00114   }
00115 
00116   bool isPatchLike() const {
00117     return (name=="sspatch")||(name=="ssrediff")||(name=="ssformat");
00118   }
00119 
00120   bool isRediffLike() const {
00121     return (name=="ssrediff");
00122   }
00123 
00124   bool isResolveLike() const {
00125     return (name=="ssresolve");
00126   }
00127 
00128   bool isMergeLike() const {
00129     return (name=="ssmerge");
00130   }
00131 
00132   bool isFormatLike() const {
00133     return (name=="ssformat");
00134   }
00135 
00136   std::string getVersion() const;
00137 
00138   void add(int coverage, const char *name, const char *desc);
00139 
00140   void addAll(const char *name, const char *desc) {
00141     add(OPTION_FOR_ALL,name,desc);
00142   }
00143 
00144   void addTransform(const char *name, const char *desc) {
00145     add(OPTION_FOR_DIFF|OPTION_FOR_MERGE|OPTION_FOR_PATCH|OPTION_FOR_REDIFF,name,desc);
00146   }
00147 
00148   void addCompare(const char *name, const char *desc) {
00149     add(OPTION_FOR_COMPARE,name,desc);
00150   }
00151 
00152   void showOptions(int filter) {
00153     option_filter = filter;
00154   }
00155 
00156 
00157   void beginHelp();
00158   void addUsage(const char *usage);
00159   void addDescription(const char *desc);
00160   Example& addExample(const char *code, const char *desc) {
00161     Example eg;
00162     eg.code = code;
00163     eg.desc = desc;
00164     examples.push_back(eg);
00165     return examples.back();
00166   }
00167   Recipe& addRecipe(const char *name, const char *code) {
00168     Recipe eg;
00169     eg.desc = name;
00170     eg.code = code;
00171     recipes[name] = eg;
00172     return recipes[name];
00173   }
00174 
00175   void endHelp();
00176 
00177   const std::vector<Option>& getOptionList() const { return opts; }
00178   const std::string& getName() const { return name; }
00179   const std::vector<std::string>& getUsages() const { return usages; }
00180   const std::vector<Example>& getExamples() const { return examples; }
00181   const std::vector<std::string> getExampleReqs() const;
00182   const std::vector<std::string> getExampleRecipes(const std::vector<std::string>& reqs) const;
00183   const std::string& getDescription() const { return description; }
00184   int getOptionFilter() const { return option_filter; }
00185 
00186 public:
00187   coopy::cmp::CompareFlags flags;
00188   std::map<std::string,bool> option_bool;
00189   std::map<std::string,std::string> option_string;
00190   std::vector<std::string> core;
00191 private:
00192   std::string name;
00193   std::map<std::string,std::vector<std::string> > option_list;
00194   coopy::store::PolyBook mapping;
00195   std::vector<Option> opts;
00196   std::vector<Example> examples;
00197   std::map<std::string,Recipe> recipes;
00198   std::vector<std::string> usages;
00199   std::string description;
00200   int option_filter;
00201 };
00202 
00203 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines