VR-Vantage 3.1 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Private Attributes
jrm::TerminalApp Class Reference

This class factors some functionality useful for command-line terminal applications (may also be used by GUI apps): More...

Public Member Functions

 TerminalApp (int argc, char *argv[])
 Per above, only main() may instantiate!
virtual ~TerminalApp ()
virtual int run ()
 Runs the application inline. This method supervises (i.e. starts and stops) the main thread based upon the Quit flag.
boost::program_options::options_description & options ()
 Used to add program options.
bool optionSpecified (const std::string &optName) const
 Helper method that returns a bool indicating if the given variable was specified.

Static Public Member Functions

static TerminalAppsingleton ()
 Singleton interface.

Static Public Attributes

static bool Quit
 Set when it is time to shutdown or when the application is signalled to stop.

Protected Member Functions

virtual void mainThread ()=0
 Override to implement the app's main thread.
virtual void configure ()
 Parses the command line and config file (if one is specified).

Protected Attributes

int m_argc
char ** m_argv
const boost::filesystem::path m_invocation
boost::program_options::options_description m_options
 Program options.
boost::program_options::positional_options_description m_positionals
 Positional parameters.
boost::program_options::variables_map m_variables
 Map of variable names to values.
boost::thread * m_mainThread
 Main thread of execution, supervised by run().
uint8_t m_rval
 Value returned to the shell unless a critical failure occurs. May be set from m_mainThread.
bool m_help
 Flag indicating if app should display help and return immediately following configure().

Static Private Attributes

static TerminalAppm_singleton
 The singular instance of this class.

Detailed Description

This class factors some functionality useful for command-line terminal applications (may also be used by GUI apps):

       1.  Processes arbitrary command line arguments.
       2.  Signal handling (i.e. provide opportunity for graceful shutdown upon Ctrl+C).
       Intended usage is via inheritance.  It is cross-platform (i.e. will compile and run on Linux and Windows).

This class factors functionality that is often useful for command-line terminal / console applications, but may also be used by GUI applications as well. It processes arbitrary command line options and hooks signals commonly used for termination. Some command line options are handled by this class:

  1. "–help", which lists the the supported command line options along with brief descriptions of each.
  2. "–config", which names a file that contains command line options to be used as if specified on the command line. These settings are overridden by anything that is also specified directly on the command line. The default for this setting is the executable's invocation name with a ".cfg" extensions, so simply creating a file with this name causes it to be used. This is useful for capturing settings, especially for apps that have many.

    The configuration file has one assignment per line, where each line is of the form: <option without the leading hyphens>=arg For options that take no argument, omit the "=arg" part. Comment lines start with a "#" character. Example:

    # Comment line here.
    string-option=C:\full path\with no\surrounding quotes
    bool-option=false
    multi-int-option=1
    multi-int-option=2
    multi-int-option=3
    # Example specifying an option having no argument, but you wouldn't do this.
    help

Usage is as follows:

  1. Inherit from this class.
  2. Override the mainThread() method. Your entire application will be run from this thread.
  3. If you wish to support command line options and / or positional parameters:
    1. Declare a data member for each setting. The types of these variables may be:
      1. C++ intrinsic types.
      2. <stdint.h> / <cstdint> types.
      3. std::string
      4. std::vector of any of the above types. These are used for options (not positional parameters) that may be specified multiple times.
    2. In your constructor, declare the options / positional parameters as follows: Example:
      App(int argc, char *argv[])
      : TerminalApp(argc, argv)
      , m_stringOption("default value for this option")
      , m_bool(true)
      , m_multiInt()
      {
      using namespace std;
      using namespace boost::program_options;
      // Set default values for any options that you could not easily initialize in the initialization list.
      m_multiInt.push_back(1);
      m_multiInt.push_back(2);
      // Declare the allowed command line options. For class settings, could use naming convention like "<Class>.<settingName>".
      // Using the first declaration as an example:
      // * "string-option" is the long option name, specified on the command line as "--string-option arg".
      // * "s" is the short option name, specified on the command line as "-s arg". These are optional.
      // * "value<string>(&m_stringOption)" says the option argument type is "string" and to store it into "m_stringOption".
      // * "->default_value(m_stringOption)" says to use the current value of "m_stringOption" as the default if not specified.
      // For options that require no arguments, omit the "value<>()->default_value()" parameter.
      m_options.add_options()
      ("string-option,s", value<string>(&m_stringOption)->default_value(m_stringOption),
      "Description of this option that is displayed when --help is specified.")
      ("bool-option,b", value<bool>(&m_bool),
      "Description of this option that is displayed when --help is specified.")
      ("multi-int-option,i", value<vector<unsigned int> >(&m_multiInt),
      "Description of this option that is displayed when --help is specified.")
      ;
      // This is optional! This maps positional parameters to options as defined above.
      // Mappings from argument position to option name. Option name must still be registered above.
      .add("string-option", 1) // First non-option parameter, e.g. $1 or %1.
      .add("bool-option", 2) // Second non-option parameter, e.g. $2 or %2.
      .add("multi-int-option", 2) // Second non-option parameter, e.g. $3 or %3.
      .add("postional", -1) // Group any additional positional parameters into "positional".
      ;
      }
  4. Implement your main() function something like this: Example:
    int main(int argc, char *argv[])
    {
    App app(argc, argv);
    return app.run();
    }
  5. To gracefully quit upon (or otherwise handle) termination signals (e.g. when Ctrl+C is pressed), mainThread() must periodically check the Quit flag and handle when it becomes set.
See Also
For a full example, see <JRM_REPOS>/CommonLibraries/JrmLicenseManager/cryptor/cryptor.cpp

Constructor & Destructor Documentation

jrm::TerminalApp::TerminalApp ( int  argc,
char *  argv[] 
)

Per above, only main() may instantiate!

virtual jrm::TerminalApp::~TerminalApp ( )
virtual

Member Function Documentation

static TerminalApp* jrm::TerminalApp::singleton ( )
static

Singleton interface.

main() creates a single (derived) app object and then all other code uses this method to get it. It is an error for more than one app object to be instantiated!

virtual int jrm::TerminalApp::run ( )
virtual

Runs the application inline. This method supervises (i.e. starts and stops) the main thread based upon the Quit flag.

boost::program_options::options_description& jrm::TerminalApp::options ( )
inline

Used to add program options.

bool jrm::TerminalApp::optionSpecified ( const std::string optName) const
inline

Helper method that returns a bool indicating if the given variable was specified.

virtual void jrm::TerminalApp::mainThread ( )
protectedpure virtual

Override to implement the app's main thread.

It is started and stopped from run(). Implementations MUST periodically check boost::this_thread::interruption_requested() and / or Quit and return when either are true.

virtual void jrm::TerminalApp::configure ( )
protectedvirtual

Parses the command line and config file (if one is specified).

Sets related data members accordingly. Other options may be added in the constructor (see usage example in class documentation).

Member Data Documentation

bool jrm::TerminalApp::Quit
static

Set when it is time to shutdown or when the application is signalled to stop.

int jrm::TerminalApp::m_argc
protected
char** jrm::TerminalApp::m_argv
protected
const boost::filesystem::path jrm::TerminalApp::m_invocation
protected
boost::program_options::options_description jrm::TerminalApp::m_options
protected

Program options.

boost::program_options::positional_options_description jrm::TerminalApp::m_positionals
protected

Positional parameters.

boost::program_options::variables_map jrm::TerminalApp::m_variables
protected

Map of variable names to values.

boost::thread* jrm::TerminalApp::m_mainThread
protected

Main thread of execution, supervised by run().

uint8_t jrm::TerminalApp::m_rval
protected

Value returned to the shell unless a critical failure occurs. May be set from m_mainThread.

bool jrm::TerminalApp::m_help
protected

Flag indicating if app should display help and return immediately following configure().

TerminalApp* jrm::TerminalApp::m_singleton
staticprivate

The singular instance of this class.


The documentation for this class was generated from the following file:


Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)