Commit 5506fcad1f26fca7401ebd5b88990d1c6a9ae298

Authored by Jürgen Knödlseder
1 parent add5f336

Add GApplication::need_help() method that notifies the client application that t…

…he --help option has been specified on the command line (#572).
include/GApplication.hpp
1 1 /***************************************************************************
2 2 * GApplication.hpp - GammaLib application base class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2014 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -83,6 +83,7 @@ public:
83 83 void log_header(void);
84 84 void log_trailer(void);
85 85 void log_parameters(void);
  86 + const bool& need_help(void) const;
86 87 std::string print(const GChatter& chatter = NORMAL) const;
87 88  
88 89 // Public members
... ... @@ -105,6 +106,7 @@ protected:
105 106 std::time_t m_tstart; //!< Calendar start time of execution
106 107 std::clock_t m_cstart; //!< Clock start time of execution
107 108 GApplicationPars m_pars; //!< Application parameters
  109 + bool m_need_help; //!< --help specified
108 110 };
109 111  
110 112  
... ... @@ -209,4 +211,17 @@ const std::string&amp; GApplication::log_filename(void) const
209 211 return (m_logfile);
210 212 }
211 213  
  214 +
  215 +/***********************************************************************//**
  216 + * @brief Signals if --help option has been specified
  217 + *
  218 + * @return True if --help option has been specified.
  219 + ***************************************************************************/
  220 +inline
  221 +const bool& GApplication::need_help(void) const
  222 +{
  223 + // Return
  224 + return (m_need_help);
  225 +}
  226 +
212 227 #endif /* GAPPLICATION_HPP */
... ...
pyext/GApplication.i
... ... @@ -70,6 +70,7 @@ public:
70 70 void log_header(void);
71 71 void log_trailer(void);
72 72 void log_parameters(void);
  73 + const bool& need_help(void) const;
73 74  
74 75 // Public members
75 76 GLog log; //!< Application logger
... ...
src/app/GApplication.cpp
1 1 /***************************************************************************
2 2 * GApplication.cpp - GammaLib application base class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2013 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -131,15 +131,32 @@ GApplication::GApplication(const std::string&amp; name, const std::string&amp; version,
131 131 m_args.push_back(gammalib::strip_whitespace(argv[i]));
132 132 }
133 133  
134   - // Initialise application parameters
135   - m_pars.load(par_filename(), m_args);
  134 + // Catch --help option before doing anything else. The actual action
  135 + // needs to be done by the client, but we skip the loading of the
  136 + // parameter file and the opening of the logger if the help option
  137 + // was specified.
  138 + m_need_help = false;
  139 + if (m_args.size() >= 2) {
  140 + if (m_args[1] == "--help") {
  141 + m_need_help = true;
  142 + }
  143 + }
136 144  
137   - // Set log filename and chattiness
138   - set_log_filename();
139   - set_log_chatter();
  145 + // If no --help option has been specified then proceed with loading
  146 + // the parameter file and opening the logger
  147 + if (!m_need_help) {
  148 +
  149 + // Initialise application parameters
  150 + m_pars.load(par_filename(), m_args);
  151 +
  152 + // Set log filename and chattiness
  153 + set_log_filename();
  154 + set_log_chatter();
  155 +
  156 + // Initialise the application logger
  157 + logFileOpen();
140 158  
141   - // Initialise the application logger
142   - logFileOpen();
  159 + } // endif: no --help option specified
143 160  
144 161 // Return
145 162 return;
... ... @@ -579,6 +596,7 @@ void GApplication::init_members(void)
579 596 m_logfile.clear();
580 597 m_args.clear();
581 598 m_pars.clear();
  599 + m_need_help = false;
582 600  
583 601 // Save the execution calendar start time
584 602 std::time(&m_tstart);
... ... @@ -602,14 +620,15 @@ void GApplication::copy_members(const GApplication&amp; app)
602 620 log = app.log;
603 621  
604 622 // Copy protected attributes
605   - m_name = app.m_name;
606   - m_version = app.m_version;
607   - m_parfile = app.m_parfile;
608   - m_logfile = app.m_logfile;
609   - m_args = app.m_args;
610   - m_tstart = app.m_tstart;
611   - m_cstart = app.m_cstart;
612   - m_pars = app.m_pars;
  623 + m_name = app.m_name;
  624 + m_version = app.m_version;
  625 + m_parfile = app.m_parfile;
  626 + m_logfile = app.m_logfile;
  627 + m_args = app.m_args;
  628 + m_tstart = app.m_tstart;
  629 + m_cstart = app.m_cstart;
  630 + m_pars = app.m_pars;
  631 + m_need_help = app.m_need_help;
613 632  
614 633 // Return
615 634 return;
... ...