"Fossies" - the Fresh Open Source Software Archive

Member "htmlrecode-1.3.1/argh/argh.hh" (21 Jul 2009, 5482 Bytes) of package /linux/www/old/htmlrecode-1.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #ifndef optrar_argh_hh
    2 #define optrar_argh_hh
    3 
    4 /* Copyright (C) 1992,2004 Bisqwit (http://iki.fi/bisqwit/) */
    5 
    6 /*
    7  * C++ USAGE EXAMPLE
    8  * 
    9 
   10 #include <argh.hh>
   11 
   12 int main(int argc, const char *const *argv)
   13 {
   14     vector<string> files;
   15     
   16     string buildfn;
   17     bool worst = false;
   18     
   19     ParamHandler Argh;
   20     
   21     Argh.AddLong("worst",   'w').SetBool().SetDesc("Least optimal.");
   22     Argh.AddLong("help",    'h').SetBool().SetDesc("This help.");
   23     Argh.AddLong("version", 'V').SetBool().SetDesc("Displays version information.");
   24     Argh.AddLong("build",   'b').SetString().SetDesc("Builds the archive.", "<file>");
   25     Argh.AddLong("make",    'b');
   26     
   27     Argh.AddBool('k');
   28     Argh.AddDesc('k', "This option does nothing.");
   29     Argh.AddLong("idle", 500).SetDesc("This option does nothing.");
   30     
   31     Argh.StartParse(argc, argv);
   32     for(;;)
   33     {
   34         int c = Argh.GetParam();
   35         if(c == -1)break;
   36         switch(c)
   37         {
   38             case 'w': worst = Argh.GetBool(); break;
   39             case 'V': printf("%s\n", VERSION); return 0;
   40             case 'b': buildfn = Argh.GetString(); break;
   41             case 'k': break; // -k
   42             case 500: break; // --idle
   43             case 'h':
   44                 printf(
   45                     "This is software\n"
   46                     "\nUsage: software [<option> [<...>]] <file> [<...>]\n"
   47                     "\nThis software does something for the files.\n"
   48                     "\nOptions:\n");
   49                 Argh.ListOptions();
   50                 printf("\nNo warranty whatsoever.\n");
   51                 return 0;
   52             default:
   53                 files.push_back(Argh.GetString());
   54         }
   55     }
   56     if(!Argh.ok())return -1;
   57     if(!files.size())
   58     {
   59         fprintf(stderr, "Error: At least one file must be specified.\n");
   60         return -1;
   61     }
   62     
   63     ...
   64 }
   65 
   66 */
   67 
   68 /* The header begins here. */
   69 
   70 #include <string>
   71 class ParamHandler
   72 {
   73 private:
   74     /* No copying */
   75     void operator=(const ParamHandler &);
   76     ParamHandler (const ParamHandler &);
   77     class Reference;
   78 public:
   79     typedef long keytype;
   80     
   81     ParamHandler();
   82     virtual ~ParamHandler();
   83     virtual void PrintOpt(unsigned space, const std::string &opts, const std::string &desc);
   84     
   85     Reference AddLong(const std::string &longname, keytype alias);
   86     Reference AddBool(keytype c);
   87     Reference AddInt(keytype c, int min, int max);
   88     Reference AddFloat(keytype c, double min, double max);
   89     Reference AddString(keytype c, unsigned min=1, unsigned max=std::string::npos);
   90     Reference AddDesc(keytype c, const std::string &s, const std::string &param="");
   91 
   92     // Support for many conventions
   93     void StartParse(int ac, const char *const *av, int firstarg=1);
   94     void StartParse(int ac, const char **av, int firstarg=1);
   95     void StartParse(int ac, char **av, int firstarg=1);
   96     void StartParse(int ac, char *const*av, int firstarg=1);
   97     
   98     const std::string ProgName() const { return A0; }
   99 
  100     /* --not-x, --no-x, --without-x and --with-x are recognized.
  101      * Therefore you should use GetBool().
  102      */
  103     inline bool GetBool() const      { return polarity; }
  104     inline int GetInt() const        { return intparm; }
  105     inline double GetFloat() const   { return doubleparm; }
  106     inline const std::string &GetString() const { return param; }
  107     inline bool ok() const           { return !error; }
  108     
  109     keytype GetParam();
  110     void ListOptions();
  111 
  112     /* - disabled, not supported.
  113 public:
  114      // Keys for 2,3,4 character options with single hyphen
  115      static inline keytype char2opt(char c1, char c2) { return (unsigned char)c1 + (unsigned char)c2 * 256; }
  116      static inline keytype char3opt(char c1, char c2, char c3) { return (unsigned char)c1 + 256 * char2opt(c2, c3); }
  117      static inline keytype char4opt(char c1, char c2, char c3, char c4) { return char2opt(c1, c2) + 65536 * char2opt(c3, c4); }
  118     */
  119 private:    
  120     void ErrorIllegalOption(keytype key);
  121     void ErrorNeedsArg(keytype key);
  122     void ErrorNeedsArg(const std::string &longo);
  123     void ErrorNeedNoArg(const std::string &longo);
  124     void InternalError(keytype key, const char *s);
  125     void ErrorOutOfRange(const std::string &param);
  126     void ErrorUnknownOption(const char *s, bool negafail);
  127         
  128 public:
  129     /* argh_atypemap needs this and I don't know how to make it friend. */
  130     class ArgInfo { public: char type; int min,max;unsigned maxl;double fmin,fmax; };
  131 private:
  132     class argh_aliasmap *aliases;
  133     class argh_descsmap *descs;
  134     class argh_atypemap *argtypes;
  135     class Reference
  136     {
  137         ParamHandler& par;
  138         keytype key;
  139     public:
  140         Reference(ParamHandler& p, keytype k) : par(p), key(k) { }
  141         Reference &SetBool();
  142         Reference &SetInt(int min, int max);
  143         Reference &SetFloat(double min, double max);
  144         Reference &SetString(unsigned min=1, unsigned max=std::string::npos);
  145         Reference &SetDesc(const std::string &s, const std::string &param="");
  146     /*- disabled, not needed.
  147     private:
  148         Reference(const Reference&);
  149         const Reference& operator= (const Reference& );
  150     */
  151     };
  152     Reference MakeRef(keytype key);
  153     int ParseError();
  154     int ShortOpt(keytype key, const char *s);
  155 private:
  156     int argc; const char *const *argv;
  157     const char *A0;
  158     /* parser volatile */
  159     int argpos; bool opts; std::string longo, longobuf; std::string param;
  160     const char *shortpointer; bool error, polarity; int intparm; double doubleparm;
  161 };
  162 
  163 #endif