Panda3D

programBase.h

00001 // Filename: programBase.h
00002 // Created by:  drose (13Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef PROGRAMBASE_H
00016 #define PROGRAMBASE_H
00017 
00018 #include "pandatoolbase.h"
00019 
00020 #include "distanceUnit.h"
00021 #include "pathReplace.h"
00022 #include "pathStore.h"
00023 #include "filename.h"
00024 #include "pointerTo.h"
00025 #include "vector_string.h"
00026 #include "pvector.h"
00027 #include "pdeque.h"
00028 #include "pmap.h"
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //       Class : ProgramBase
00032 // Description : This is intended to be the base class for most
00033 //               general-purpose utility programs in the PANDATOOL
00034 //               tree.  It automatically handles things like
00035 //               command-line arguments in a portable way.
00036 ////////////////////////////////////////////////////////////////////
00037 class ProgramBase {
00038 public:
00039   ProgramBase();
00040   virtual ~ProgramBase();
00041 
00042   void show_description();
00043   void show_usage();
00044   void show_options();
00045 
00046   INLINE void show_text(const string &text);
00047   void show_text(const string &prefix, int indent_width, string text);
00048 
00049   virtual void parse_command_line(int argc, char **argv);
00050 
00051   string get_exec_command() const;
00052 
00053   typedef pdeque<string> Args;
00054   Filename _program_name;
00055   Args _program_args;
00056 
00057 protected:
00058   typedef bool (*OptionDispatchFunction)(const string &opt, const string &parm, void *data);
00059   typedef bool (*OptionDispatchMethod)(ProgramBase *self, const string &opt, const string &parm, void *data);
00060 
00061   virtual bool handle_args(Args &args);
00062   virtual bool post_command_line();
00063 
00064   void set_program_description(const string &description);
00065   void clear_runlines();
00066   void add_runline(const string &runline);
00067   void clear_options();
00068   void add_option(const string &option, const string &parm_name,
00069                   int index_group, const string &description,
00070                   OptionDispatchFunction option_function,
00071                   bool *bool_var = (bool *)NULL,
00072                   void *option_data = (void *)NULL);
00073   void add_option(const string &option, const string &parm_name,
00074                   int index_group, const string &description,
00075                   OptionDispatchMethod option_method,
00076                   bool *bool_var = (bool *)NULL,
00077                   void *option_data = (void *)NULL);
00078   bool redescribe_option(const string &option, const string &description);
00079   bool remove_option(const string &option);
00080 
00081   void add_path_replace_options();
00082   void add_path_store_options();
00083 
00084   static bool dispatch_none(const string &opt, const string &arg, void *);
00085   static bool dispatch_true(const string &opt, const string &arg, void *var);
00086   static bool dispatch_false(const string &opt, const string &arg, void *var);
00087   static bool dispatch_count(const string &opt, const string &arg, void *var);
00088   static bool dispatch_int(const string &opt, const string &arg, void *var);
00089   static bool dispatch_int_pair(const string &opt, const string &arg, void *var);
00090   static bool dispatch_double(const string &opt, const string &arg, void *var);
00091   static bool dispatch_double_pair(const string &opt, const string &arg, void *var);
00092   static bool dispatch_double_triple(const string &opt, const string &arg, void *var);
00093   static bool dispatch_double_quad(const string &opt, const string &arg, void *var);
00094   static bool dispatch_color(const string &opt, const string &arg, void *var);
00095   static bool dispatch_string(const string &opt, const string &arg, void *var);
00096   static bool dispatch_vector_string(const string &opt, const string &arg, void *var);
00097   static bool dispatch_vector_string_comma(const string &opt, const string &arg, void *var);
00098   static bool dispatch_filename(const string &opt, const string &arg, void *var);
00099   static bool dispatch_search_path(const string &opt, const string &arg, void *var);
00100   static bool dispatch_coordinate_system(const string &opt, const string &arg, void *var);
00101   static bool dispatch_units(const string &opt, const string &arg, void *var);
00102   static bool dispatch_image_type(const string &opt, const string &arg, void *var);
00103   static bool dispatch_path_replace(const string &opt, const string &arg, void *var);
00104   static bool dispatch_path_store(const string &opt, const string &arg, void *var);
00105 
00106   static bool handle_help_option(const string &opt, const string &arg, void *);
00107 
00108   static void format_text(ostream &out, bool &last_newline,
00109                           const string &prefix, int indent_width,
00110                           const string &text, int line_width);
00111 
00112   PT(PathReplace) _path_replace;
00113   bool _got_path_store;
00114   bool _got_path_directory;
00115 
00116 
00117 private:
00118   void sort_options();
00119   void get_terminal_width();
00120 
00121   class Option {
00122   public:
00123     string _option;
00124     string _parm_name;
00125     int _index_group;
00126     int _sequence;
00127     string _description;
00128     OptionDispatchFunction _option_function;
00129     OptionDispatchMethod _option_method;
00130     bool *_bool_var;
00131     void *_option_data;
00132   };
00133 
00134   class SortOptionsByIndex {
00135   public:
00136     bool operator () (const Option *a, const Option *b) const;
00137   };
00138 
00139   string _description;
00140   typedef vector_string Runlines;
00141   Runlines _runlines;
00142 
00143   typedef pmap<string, Option> OptionsByName;
00144   typedef pvector<const Option *> OptionsByIndex;
00145   OptionsByName _options_by_name;
00146   OptionsByIndex _options_by_index;
00147   int _next_sequence;
00148   bool _sorted_options;
00149 
00150   typedef pmap<string, string> GotOptions;
00151   GotOptions _got_options;
00152 
00153   bool _last_newline;
00154   int _terminal_width;
00155   bool _got_terminal_width;
00156   int _option_indent;
00157   bool _got_option_indent;
00158 };
00159 
00160 #include "programBase.I"
00161 
00162 #endif
00163 
00164 
 All Classes Functions Variables Enumerations