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