programBase.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef PROGRAMBASE_H
  14. #define PROGRAMBASE_H
  15. #include "pandatoolbase.h"
  16. #include "distanceUnit.h"
  17. #include "pathReplace.h"
  18. #include "pathStore.h"
  19. #include "filename.h"
  20. #include "pointerTo.h"
  21. #include "vector_string.h"
  22. #include "pvector.h"
  23. #include "pdeque.h"
  24. #include "pmap.h"
  25. /**
  26. * This is intended to be the base class for most general-purpose utility
  27. * programs in the PANDATOOL tree. It automatically handles things like
  28. * command-line arguments in a portable way.
  29. */
  30. class ProgramBase {
  31. public:
  32. enum ExitCode {
  33. EC_not_exited = -1,
  34. EC_clean_exit = 0,
  35. EC_failure = 1
  36. };
  37. ProgramBase(const std::string &name = std::string());
  38. virtual ~ProgramBase();
  39. void show_description();
  40. void show_usage();
  41. void show_options();
  42. INLINE void show_text(const std::string &text);
  43. void show_text(const std::string &prefix, int indent_width, std::string text);
  44. void write_man_page(std::ostream &out);
  45. virtual ExitCode parse_command_line(int argc, char **argv, bool exit_on_complete = true);
  46. std::string get_exec_command() const;
  47. typedef pdeque<std::string> Args;
  48. Filename _program_name;
  49. Args _program_args;
  50. protected:
  51. typedef bool (*OptionDispatchFunction)(const std::string &opt, const std::string &parm, void *data);
  52. typedef bool (*OptionDispatchMethod)(ProgramBase *self, const std::string &opt, const std::string &parm, void *data);
  53. virtual bool handle_args(Args &args);
  54. virtual bool post_command_line();
  55. void set_program_brief(const std::string &brief);
  56. void set_program_description(const std::string &description);
  57. void clear_runlines();
  58. void add_runline(const std::string &runline);
  59. void clear_options();
  60. void add_option(const std::string &option, const std::string &parm_name,
  61. int index_group, const std::string &description,
  62. OptionDispatchFunction option_function,
  63. bool *bool_var = nullptr,
  64. void *option_data = nullptr);
  65. void add_option(const std::string &option, const std::string &parm_name,
  66. int index_group, const std::string &description,
  67. OptionDispatchMethod option_method,
  68. bool *bool_var = nullptr,
  69. void *option_data = nullptr);
  70. bool redescribe_option(const std::string &option, const std::string &description);
  71. bool remove_option(const std::string &option);
  72. void add_path_replace_options();
  73. void add_path_store_options();
  74. static bool dispatch_none(const std::string &opt, const std::string &arg, void *);
  75. static bool dispatch_true(const std::string &opt, const std::string &arg, void *var);
  76. static bool dispatch_false(const std::string &opt, const std::string &arg, void *var);
  77. static bool dispatch_count(const std::string &opt, const std::string &arg, void *var);
  78. static bool dispatch_int(const std::string &opt, const std::string &arg, void *var);
  79. static bool dispatch_int_pair(const std::string &opt, const std::string &arg, void *var);
  80. static bool dispatch_int_quad(const std::string &opt, const std::string &arg, void *var);
  81. static bool dispatch_double(const std::string &opt, const std::string &arg, void *var);
  82. static bool dispatch_double_pair(const std::string &opt, const std::string &arg, void *var);
  83. static bool dispatch_double_triple(const std::string &opt, const std::string &arg, void *var);
  84. static bool dispatch_double_quad(const std::string &opt, const std::string &arg, void *var);
  85. static bool dispatch_color(const std::string &opt, const std::string &arg, void *var);
  86. static bool dispatch_string(const std::string &opt, const std::string &arg, void *var);
  87. static bool dispatch_vector_string(const std::string &opt, const std::string &arg, void *var);
  88. static bool dispatch_vector_string_comma(const std::string &opt, const std::string &arg, void *var);
  89. static bool dispatch_filename(const std::string &opt, const std::string &arg, void *var);
  90. static bool dispatch_search_path(const std::string &opt, const std::string &arg, void *var);
  91. static bool dispatch_coordinate_system(const std::string &opt, const std::string &arg, void *var);
  92. static bool dispatch_units(const std::string &opt, const std::string &arg, void *var);
  93. static bool dispatch_image_type(const std::string &opt, const std::string &arg, void *var);
  94. static bool dispatch_path_replace(const std::string &opt, const std::string &arg, void *var);
  95. static bool dispatch_path_store(const std::string &opt, const std::string &arg, void *var);
  96. static bool handle_help_option(const std::string &opt, const std::string &arg, void *);
  97. static void format_text(std::ostream &out, bool &last_newline,
  98. const std::string &prefix, int indent_width,
  99. const std::string &text, int line_width);
  100. PT(PathReplace) _path_replace;
  101. bool _got_path_store;
  102. bool _got_path_directory;
  103. private:
  104. void sort_options();
  105. void get_terminal_width();
  106. class Option {
  107. public:
  108. std::string _option;
  109. std::string _parm_name;
  110. int _index_group;
  111. int _sequence;
  112. std::string _description;
  113. OptionDispatchFunction _option_function;
  114. OptionDispatchMethod _option_method;
  115. bool *_bool_var;
  116. void *_option_data;
  117. };
  118. class SortOptionsByIndex {
  119. public:
  120. bool operator () (const Option *a, const Option *b) const;
  121. };
  122. std::string _name;
  123. std::string _brief;
  124. std::string _description;
  125. typedef vector_string Runlines;
  126. Runlines _runlines;
  127. typedef pmap<std::string, Option> OptionsByName;
  128. typedef pvector<const Option *> OptionsByIndex;
  129. OptionsByName _options_by_name;
  130. OptionsByIndex _options_by_index;
  131. int _next_sequence;
  132. bool _sorted_options;
  133. typedef pmap<std::string, std::string> GotOptions;
  134. GotOptions _got_options;
  135. bool _last_newline;
  136. int _terminal_width;
  137. bool _got_terminal_width;
  138. int _option_indent;
  139. bool _got_option_indent;
  140. };
  141. #include "programBase.I"
  142. #endif