Commit 94e7f534cf6c70e9122bbce6578f6a8768248bf5

Authored by Jürgen Knödlseder
1 parent 4fc024b7

Display help file if --help option is given (#572).

src/support/ctool.cpp
... ... @@ -49,6 +49,7 @@
49 49 /* __ Method name definitions ____________________________________________ */
50 50 #define G_GET_MEAN_POINTING "ctool::get_mean_pointing(GObservations&)"
51 51 #define G_CREATE_EBOUNDS "ctool::create_ebounds()"
  52 +#define G_PROVIDE_HELP "ctool::provide_help()"
52 53  
53 54 /* __ Debug definitions __________________________________________________ */
54 55  
... ... @@ -109,6 +110,12 @@ ctool::ctool(const std::string& name, const std::string& version,
109 110 int argc, char *argv[]) :
110 111 GApplication(name, version, argc, argv)
111 112 {
  113 + // Catch --help option before doing anything else
  114 + if (need_help()) {
  115 + provide_help();
  116 + exit(0);
  117 + }
  118 +
112 119 // Initialise members
113 120 init_members();
114 121  
... ... @@ -978,3 +985,43 @@ size_t ctool::get_current_rss(void)
978 985 // Return resident set size
979 986 return rss;
980 987 }
  988 +
  989 +
  990 +/***********************************************************************//**
  991 + * @brief Dumps help text in the console
  992 + ***************************************************************************/
  993 +void ctool::provide_help(void) const
  994 +{
  995 + // Allocate line buffer
  996 + const int n = 1000;
  997 + char line[n];
  998 +
  999 + // Build help file name
  1000 + std::string helpfile = name()+".txt";
  1001 +
  1002 + // Get ctools environment variable
  1003 + char* ptr = std::getenv("CTOOLS");
  1004 + if (ptr == NULL) {
  1005 + std::string msg = "CTOOLS environment variable not set, cannot "
  1006 + "display help file. Please define CTOOLS "
  1007 + "environment variable.";
  1008 + throw GException::invalid_value(G_PROVIDE_HELP, msg);
  1009 + }
  1010 +
  1011 + // If help file exists then display it, otherwise notify that no
  1012 + // help is available
  1013 + std::string fname = std::string(ptr) + "/share/help/" + helpfile;
  1014 + FILE* fptr = fopen(fname.c_str(), "r");
  1015 + if (fptr != NULL) {
  1016 + while (fgets(line, n, fptr) != NULL) {
  1017 + std::cout << std::string(line);
  1018 + }
  1019 + fclose(fptr);
  1020 + }
  1021 + else {
  1022 + std::cout << "No help available for "+name()+"." << std::endl;
  1023 + }
  1024 +
  1025 + // Return
  1026 + return;
  1027 +}
... ...
src/support/ctool.hpp
... ... @@ -89,6 +89,10 @@ protected:
89 89 GSkyDir get_mean_pointing(const GObservations& obs);
90 90 size_t get_current_rss(void);
91 91  
  92 +protected:
  93 + // Protected methods
  94 + void provide_help(void) const;
  95 +
92 96 // Protected members
93 97 bool m_read_ahead; //!< Read ahead parameters
94 98 bool m_use_xml; //!< Use XML file instead of FITS file for observations
... ...