From 94e7f534cf6c70e9122bbce6578f6a8768248bf5 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 28 Oct 2015 11:54:15 +0100 Subject: [PATCH] Display help file if --help option is given (#572). --- src/support/ctool.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/support/ctool.hpp | 4 ++++ 2 files changed, 51 insertions(+), 0 deletions(-) diff --git a/src/support/ctool.cpp b/src/support/ctool.cpp index a254a0a..a053309 100644 --- a/src/support/ctool.cpp +++ b/src/support/ctool.cpp @@ -49,6 +49,7 @@ /* __ Method name definitions ____________________________________________ */ #define G_GET_MEAN_POINTING "ctool::get_mean_pointing(GObservations&)" #define G_CREATE_EBOUNDS "ctool::create_ebounds()" +#define G_PROVIDE_HELP "ctool::provide_help()" /* __ Debug definitions __________________________________________________ */ @@ -109,6 +110,12 @@ ctool::ctool(const std::string& name, const std::string& version, int argc, char *argv[]) : GApplication(name, version, argc, argv) { + // Catch --help option before doing anything else + if (need_help()) { + provide_help(); + exit(0); + } + // Initialise members init_members(); @@ -978,3 +985,43 @@ size_t ctool::get_current_rss(void) // Return resident set size return rss; } + + +/***********************************************************************//** + * @brief Dumps help text in the console + ***************************************************************************/ +void ctool::provide_help(void) const +{ + // Allocate line buffer + const int n = 1000; + char line[n]; + + // Build help file name + std::string helpfile = name()+".txt"; + + // Get ctools environment variable + char* ptr = std::getenv("CTOOLS"); + if (ptr == NULL) { + std::string msg = "CTOOLS environment variable not set, cannot " + "display help file. Please define CTOOLS " + "environment variable."; + throw GException::invalid_value(G_PROVIDE_HELP, msg); + } + + // If help file exists then display it, otherwise notify that no + // help is available + std::string fname = std::string(ptr) + "/share/help/" + helpfile; + FILE* fptr = fopen(fname.c_str(), "r"); + if (fptr != NULL) { + while (fgets(line, n, fptr) != NULL) { + std::cout << std::string(line); + } + fclose(fptr); + } + else { + std::cout << "No help available for "+name()+"." << std::endl; + } + + // Return + return; +} diff --git a/src/support/ctool.hpp b/src/support/ctool.hpp index 43cb3e0..13672d5 100644 --- a/src/support/ctool.hpp +++ b/src/support/ctool.hpp @@ -89,6 +89,10 @@ protected: GSkyDir get_mean_pointing(const GObservations& obs); size_t get_current_rss(void); +protected: + // Protected methods + void provide_help(void) const; + // Protected members bool m_read_ahead; //!< Read ahead parameters bool m_use_xml; //!< Use XML file instead of FITS file for observations -- libgit2 0.21.4