This class factors some functionality useful for command-line terminal applications (may also be used by GUI apps):
More...
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:
- "–help", which lists the the supported command line options along with brief descriptions of each.
"–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:
- Inherit from this class.
- Override the
mainThread() method. Your entire application will be run from this thread.
- If you wish to support command line options and / or positional parameters:
- Declare a data member for each setting. The types of these variables may be:
- C++ intrinsic types.
- <stdint.h> / <cstdint> types.
- std::string
- std::vector of any of the above types. These are used for options (not positional parameters) that may be specified multiple times.
- In your constructor, declare the options / positional parameters as follows: Example:
App(int argc, char *argv[])
, m_stringOption("default value for this option")
, m_bool(true)
, m_multiInt()
{
using namespace std;
using namespace boost::program_options;
m_multiInt.push_back(1);
m_multiInt.push_back(2);
(
"string-option,s",
value<string>(&m_stringOption)->default_value(m_stringOption),
"Description of this option that is displayed when --help is specified.")
"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.")
;
.add("string-option", 1)
.add("bool-option", 2)
.add("multi-int-option", 2)
.add("postional", -1)
;
}
- Implement your main() function something like this: Example:
int main(int argc, char *argv[])
{
App app(argc, argv);
return app.run();
}
- 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