Panda3D
 All Classes Functions Variables Enumerations
programBase.h
1 // Filename: programBase.h
2 // Created by: drose (13Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef PROGRAMBASE_H
16 #define PROGRAMBASE_H
17 
18 #include "pandatoolbase.h"
19 
20 #include "distanceUnit.h"
21 #include "pathReplace.h"
22 #include "pathStore.h"
23 #include "filename.h"
24 #include "pointerTo.h"
25 #include "vector_string.h"
26 #include "pvector.h"
27 #include "pdeque.h"
28 #include "pmap.h"
29 
30 ////////////////////////////////////////////////////////////////////
31 // Class : ProgramBase
32 // Description : This is intended to be the base class for most
33 // general-purpose utility programs in the PANDATOOL
34 // tree. It automatically handles things like
35 // command-line arguments in a portable way.
36 ////////////////////////////////////////////////////////////////////
37 class ProgramBase {
38 public:
39  ProgramBase(const string &name = string());
40  virtual ~ProgramBase();
41 
42  void show_description();
43  void show_usage();
44  void show_options();
45 
46  INLINE void show_text(const string &text);
47  void show_text(const string &prefix, int indent_width, string text);
48 
49  void write_man_page(ostream &out);
50 
51  virtual void parse_command_line(int argc, char **argv);
52 
53  string get_exec_command() const;
54 
55  typedef pdeque<string> Args;
56  Filename _program_name;
57  Args _program_args;
58 
59 protected:
60  typedef bool (*OptionDispatchFunction)(const string &opt, const string &parm, void *data);
61  typedef bool (*OptionDispatchMethod)(ProgramBase *self, const string &opt, const string &parm, void *data);
62 
63  virtual bool handle_args(Args &args);
64  virtual bool post_command_line();
65 
66  void set_program_brief(const string &brief);
67  void set_program_description(const string &description);
68  void clear_runlines();
69  void add_runline(const string &runline);
70  void clear_options();
71  void add_option(const string &option, const string &parm_name,
72  int index_group, const string &description,
73  OptionDispatchFunction option_function,
74  bool *bool_var = (bool *)NULL,
75  void *option_data = (void *)NULL);
76  void add_option(const string &option, const string &parm_name,
77  int index_group, const string &description,
78  OptionDispatchMethod option_method,
79  bool *bool_var = (bool *)NULL,
80  void *option_data = (void *)NULL);
81  bool redescribe_option(const string &option, const string &description);
82  bool remove_option(const string &option);
83 
84  void add_path_replace_options();
85  void add_path_store_options();
86 
87  static bool dispatch_none(const string &opt, const string &arg, void *);
88  static bool dispatch_true(const string &opt, const string &arg, void *var);
89  static bool dispatch_false(const string &opt, const string &arg, void *var);
90  static bool dispatch_count(const string &opt, const string &arg, void *var);
91  static bool dispatch_int(const string &opt, const string &arg, void *var);
92  static bool dispatch_int_pair(const string &opt, const string &arg, void *var);
93  static bool dispatch_int_quad(const string &opt, const string &arg, void *var);
94  static bool dispatch_double(const string &opt, const string &arg, void *var);
95  static bool dispatch_double_pair(const string &opt, const string &arg, void *var);
96  static bool dispatch_double_triple(const string &opt, const string &arg, void *var);
97  static bool dispatch_double_quad(const string &opt, const string &arg, void *var);
98  static bool dispatch_color(const string &opt, const string &arg, void *var);
99  static bool dispatch_string(const string &opt, const string &arg, void *var);
100  static bool dispatch_vector_string(const string &opt, const string &arg, void *var);
101  static bool dispatch_vector_string_comma(const string &opt, const string &arg, void *var);
102  static bool dispatch_filename(const string &opt, const string &arg, void *var);
103  static bool dispatch_search_path(const string &opt, const string &arg, void *var);
104  static bool dispatch_coordinate_system(const string &opt, const string &arg, void *var);
105  static bool dispatch_units(const string &opt, const string &arg, void *var);
106  static bool dispatch_image_type(const string &opt, const string &arg, void *var);
107  static bool dispatch_path_replace(const string &opt, const string &arg, void *var);
108  static bool dispatch_path_store(const string &opt, const string &arg, void *var);
109 
110  static bool handle_help_option(const string &opt, const string &arg, void *);
111 
112  static void format_text(ostream &out, bool &last_newline,
113  const string &prefix, int indent_width,
114  const string &text, int line_width);
115 
116  PT(PathReplace) _path_replace;
117  bool _got_path_store;
118  bool _got_path_directory;
119 
120 
121 private:
122  void sort_options();
123  void get_terminal_width();
124 
125  class Option {
126  public:
127  string _option;
128  string _parm_name;
129  int _index_group;
130  int _sequence;
131  string _description;
132  OptionDispatchFunction _option_function;
133  OptionDispatchMethod _option_method;
134  bool *_bool_var;
135  void *_option_data;
136  };
137 
138  class SortOptionsByIndex {
139  public:
140  bool operator () (const Option *a, const Option *b) const;
141  };
142 
143  string _name;
144  string _brief;
145  string _description;
146  typedef vector_string Runlines;
147  Runlines _runlines;
148 
151  OptionsByName _options_by_name;
152  OptionsByIndex _options_by_index;
153  int _next_sequence;
154  bool _sorted_options;
155 
157  GotOptions _got_options;
158 
159  bool _last_newline;
160  int _terminal_width;
161  bool _got_terminal_width;
162  int _option_indent;
163  bool _got_option_indent;
164 };
165 
166 #include "programBase.I"
167 
168 #endif
169 
170 
This is intended to be the base class for most general-purpose utility programs in the PANDATOOL tree...
Definition: programBase.h:37
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
void show_usage()
Writes the usage line(s) to stderr.
void show_description()
Writes the program description to stderr.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
void show_text(const string &text)
Formats the indicated text to stderr with the known _terminal_width.
Definition: programBase.I:23
void show_options()
Describes each of the available options to stderr.
string get_exec_command() const
Returns the command that invoked this program, as a shell-friendly string, suitable for pasting into ...
This encapsulates the user&#39;s command-line request to replace existing, incorrect pathnames to models ...
Definition: pathReplace.h:40
void write_man_page(ostream &out)
Generates a man page in nroff syntax based on the description and options.