programBase.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Filename: programBase.h
  2. // Created by: drose (13Feb00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #ifndef PROGRAMBASE_H
  6. #define PROGRAMBASE_H
  7. #include <pandatoolbase.h>
  8. #include <distanceUnit.h>
  9. #include <filename.h>
  10. #include <vector_string.h>
  11. #include <string>
  12. #include <vector>
  13. #include <deque>
  14. #include <map>
  15. ////////////////////////////////////////////////////////////////////
  16. // Class : ProgramBase
  17. // Description : This is intended to be the base class for most
  18. // general-purpose utility programs in the PANDATOOL
  19. // tree. It automatically handles things like
  20. // command-line arguments in a portable way.
  21. ////////////////////////////////////////////////////////////////////
  22. class ProgramBase {
  23. public:
  24. ProgramBase();
  25. virtual ~ProgramBase();
  26. void show_description();
  27. void show_usage();
  28. void show_options();
  29. INLINE void show_text(const string &text);
  30. void show_text(const string &prefix, int indent_width, string text);
  31. virtual void parse_command_line(int argc, char *argv[]);
  32. typedef deque<string> Args;
  33. Filename _program_name;
  34. Args _program_args;
  35. protected:
  36. typedef bool (*OptionDispatchFunction)(const string &opt, const string &parm, void *data);
  37. typedef bool (*OptionDispatchMethod)(ProgramBase *self, const string &opt, const string &parm, void *data);
  38. virtual bool handle_args(Args &args);
  39. virtual bool post_command_line();
  40. void set_program_description(const string &description);
  41. void clear_runlines();
  42. void add_runline(const string &runline);
  43. void clear_options();
  44. void add_option(const string &option, const string &parm_name,
  45. int index_group, const string &description,
  46. OptionDispatchFunction option_function,
  47. bool *bool_var = (bool *)NULL,
  48. void *option_data = (void *)NULL);
  49. void add_option(const string &option, const string &parm_name,
  50. int index_group, const string &description,
  51. OptionDispatchMethod option_method,
  52. bool *bool_var = (bool *)NULL,
  53. void *option_data = (void *)NULL);
  54. bool redescribe_option(const string &option, const string &description);
  55. bool remove_option(const string &option);
  56. static bool dispatch_none(const string &opt, const string &arg, void *);
  57. static bool dispatch_true(const string &opt, const string &arg, void *var);
  58. static bool dispatch_false(const string &opt, const string &arg, void *var);
  59. static bool dispatch_count(const string &opt, const string &arg, void *var);
  60. static bool dispatch_int(const string &opt, const string &arg, void *var);
  61. static bool dispatch_int_pair(const string &opt, const string &arg, void *var);
  62. static bool dispatch_double(const string &opt, const string &arg, void *var);
  63. static bool dispatch_double_pair(const string &opt, const string &arg, void *var);
  64. static bool dispatch_double_triple(const string &opt, const string &arg, void *var);
  65. static bool dispatch_double_quad(const string &opt, const string &arg, void *var);
  66. static bool dispatch_color(const string &opt, const string &arg, void *var);
  67. static bool dispatch_string(const string &opt, const string &arg, void *var);
  68. static bool dispatch_filename(const string &opt, const string &arg, void *var);
  69. static bool dispatch_search_path(const string &opt, const string &arg, void *var);
  70. static bool dispatch_coordinate_system(const string &opt, const string &arg, void *var);
  71. static bool dispatch_units(const string &opt, const string &arg, void *var);
  72. static bool handle_help_option(const string &opt, const string &arg, void *);
  73. static void format_text(ostream &out, bool &last_newline,
  74. const string &prefix, int indent_width,
  75. const string &text, int line_width);
  76. private:
  77. void sort_options();
  78. void get_terminal_width();
  79. class Option {
  80. public:
  81. string _option;
  82. string _parm_name;
  83. int _index_group;
  84. int _sequence;
  85. string _description;
  86. OptionDispatchFunction _option_function;
  87. OptionDispatchMethod _option_method;
  88. bool *_bool_var;
  89. void *_option_data;
  90. };
  91. class SortOptionsByIndex {
  92. public:
  93. bool operator () (const Option *a, const Option *b) const;
  94. };
  95. string _description;
  96. typedef vector_string Runlines;
  97. Runlines _runlines;
  98. typedef map<string, Option> OptionsByName;
  99. typedef vector<const Option *> OptionsByIndex;
  100. OptionsByName _options_by_name;
  101. OptionsByIndex _options_by_index;
  102. int _next_sequence;
  103. bool _sorted_options;
  104. typedef map<string, string> GotOptions;
  105. GotOptions _got_options;
  106. bool _last_newline;
  107. int _terminal_width;
  108. bool _got_terminal_width;
  109. int _option_indent;
  110. bool _got_option_indent;
  111. };
  112. #include "programBase.I"
  113. #endif