programBase.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Filename: programBase.h
  2. // Created by: drose (13Feb00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef PROGRAMBASE_H
  19. #define PROGRAMBASE_H
  20. #include "pandatoolbase.h"
  21. #include "distanceUnit.h"
  22. #include "pathReplace.h"
  23. #include "pathStore.h"
  24. #include "filename.h"
  25. #include "pointerTo.h"
  26. #include "vector_string.h"
  27. #include "pvector.h"
  28. #include "pdeque.h"
  29. #include "pmap.h"
  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();
  40. virtual ~ProgramBase();
  41. void show_description();
  42. void show_usage();
  43. void show_options();
  44. INLINE void show_text(const string &text);
  45. void show_text(const string &prefix, int indent_width, string text);
  46. virtual void parse_command_line(int argc, char *argv[]);
  47. string get_exec_command() const;
  48. typedef pdeque<string> Args;
  49. Filename _program_name;
  50. Args _program_args;
  51. protected:
  52. typedef bool (*OptionDispatchFunction)(const string &opt, const string &parm, void *data);
  53. typedef bool (*OptionDispatchMethod)(ProgramBase *self, const string &opt, const string &parm, void *data);
  54. virtual bool handle_args(Args &args);
  55. virtual bool post_command_line();
  56. void set_program_description(const string &description);
  57. void clear_runlines();
  58. void add_runline(const string &runline);
  59. void clear_options();
  60. void add_option(const string &option, const string &parm_name,
  61. int index_group, const string &description,
  62. OptionDispatchFunction option_function,
  63. bool *bool_var = (bool *)NULL,
  64. void *option_data = (void *)NULL);
  65. void add_option(const string &option, const string &parm_name,
  66. int index_group, const string &description,
  67. OptionDispatchMethod option_method,
  68. bool *bool_var = (bool *)NULL,
  69. void *option_data = (void *)NULL);
  70. bool redescribe_option(const string &option, const string &description);
  71. bool remove_option(const string &option);
  72. void add_path_replace_options();
  73. void add_path_store_options();
  74. static bool dispatch_none(const string &opt, const string &arg, void *);
  75. static bool dispatch_true(const string &opt, const string &arg, void *var);
  76. static bool dispatch_false(const string &opt, const string &arg, void *var);
  77. static bool dispatch_count(const string &opt, const string &arg, void *var);
  78. static bool dispatch_int(const string &opt, const string &arg, void *var);
  79. static bool dispatch_int_pair(const string &opt, const string &arg, void *var);
  80. static bool dispatch_double(const string &opt, const string &arg, void *var);
  81. static bool dispatch_double_pair(const string &opt, const string &arg, void *var);
  82. static bool dispatch_double_triple(const string &opt, const string &arg, void *var);
  83. static bool dispatch_double_quad(const string &opt, const string &arg, void *var);
  84. static bool dispatch_color(const string &opt, const string &arg, void *var);
  85. static bool dispatch_string(const string &opt, const string &arg, void *var);
  86. static bool dispatch_vector_string(const string &opt, const string &arg, void *var);
  87. static bool dispatch_filename(const string &opt, const string &arg, void *var);
  88. static bool dispatch_search_path(const string &opt, const string &arg, void *var);
  89. static bool dispatch_coordinate_system(const string &opt, const string &arg, void *var);
  90. static bool dispatch_units(const string &opt, const string &arg, void *var);
  91. static bool dispatch_image_type(const string &opt, const string &arg, void *var);
  92. static bool dispatch_path_replace(const string &opt, const string &arg, void *var);
  93. static bool dispatch_path_store(const string &opt, const string &arg, void *var);
  94. static bool handle_help_option(const string &opt, const string &arg, void *);
  95. static void format_text(ostream &out, bool &last_newline,
  96. const string &prefix, int indent_width,
  97. const string &text, int line_width);
  98. PT(PathReplace) _path_replace;
  99. bool _got_path_store;
  100. bool _got_path_directory;
  101. private:
  102. void sort_options();
  103. void get_terminal_width();
  104. class Option {
  105. public:
  106. string _option;
  107. string _parm_name;
  108. int _index_group;
  109. int _sequence;
  110. string _description;
  111. OptionDispatchFunction _option_function;
  112. OptionDispatchMethod _option_method;
  113. bool *_bool_var;
  114. void *_option_data;
  115. };
  116. class SortOptionsByIndex {
  117. public:
  118. bool operator () (const Option *a, const Option *b) const;
  119. };
  120. string _description;
  121. typedef vector_string Runlines;
  122. Runlines _runlines;
  123. typedef pmap<string, Option> OptionsByName;
  124. typedef pvector<const Option *> OptionsByIndex;
  125. OptionsByName _options_by_name;
  126. OptionsByIndex _options_by_index;
  127. int _next_sequence;
  128. bool _sorted_options;
  129. typedef pmap<string, string> GotOptions;
  130. GotOptions _got_options;
  131. bool _last_newline;
  132. int _terminal_width;
  133. bool _got_terminal_width;
  134. int _option_indent;
  135. bool _got_option_indent;
  136. };
  137. #include "programBase.I"
  138. #endif