programBase.h 3.4 KB

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