libcyberradio 22.01.24
App.h
1/***************************************************************************
2 * \file App.h
3 *
4 * \brief Defines basic application-level constructs.
5 *
6 * \author DA
7 * \copyright Copyright (c) 2015-2021 CyberRadio Solutions, Inc.
8 *
9 */
10
11#ifndef INCLUDED_LIBCYBERRADIO_APP_H
12#define INCLUDED_LIBCYBERRADIO_APP_H
13
14#include <string>
15#include <map>
16#include <getopt.h>
17#include "LibCyberRadio/Common/BasicList.h"
18
19
24{
29 {
30 public:
34 AppOption();
58 AppOption(const std::string& shortName, const std::string& longName,
59 int valueType, void *valuePtr,
60 const std::string& helpArgName, const std::string& helpText,
61 bool showDefault);
68 AppOption(const AppOption& opt);
75 virtual AppOption& operator=(const AppOption& opt);
79 virtual ~AppOption();
80
81 public:
86 std::string shortName;
91 std::string longName;
100 void *valuePtr;
104 std::string helpArgName;
108 std::string helpText;
114
115 public:
119 static int TYPE_NONE;
123 static int TYPE_INTEGER;
127 static int TYPE_FLOAT;
131 static int TYPE_DOUBLE;
135 static int TYPE_STRING;
139 static int TYPE_BOOLEAN;
150 };
151
152
156 typedef BASIC_LIST_CONTAINER<AppOption> AppOptionList;
157
158
163 {
164 public:
171 AppHelpTextFormatter(int displayWidth = 75);
175 virtual ~AppHelpTextFormatter();
188 virtual void addPreOptionText(const std::string& text);
203 virtual void addOptionText(const std::string& option, const std::string& text);
216 virtual void addPostOptionText(const std::string& text);
222 virtual std::string getFormattedText();
223
224 protected:
225 // Wraps the given text to the specified width (in characters), applying
226 // the given hanging indent (in characters) to each line after the first.
227 virtual std::string wordWrappedText(const std::string& text, int width,
228 int hangingIndent);
229
230 protected:
231 BasicStringList _preOptionText;
232 BasicStringList _options;
233 BasicStringList _optionText;
234 BasicStringList _postOptionText;
235 int _maxOptionWidth;
236 int _displayWidth;
237 };
238
239
249 {
250 public:
258 virtual ~AppOptionParser();
268 void allowUnknownOption(bool allow = true);
276 void setDescription(const std::string& description);
283 void setDisplayWidth(int displayWidth = 75);
294 void setEpilogText(const std::string& epilogText);
304 void setExecutable(const std::string& executable);
312 void setUnparsedArgText(const std::string& unparsedArgText);
320 void setVersion(const std::string& version);
327 virtual void addOption(const AppOption& opt);
351 virtual void addOption(const std::string& shortName, const std::string& longName,
352 int valueType, void *valuePtr,
353 const std::string& helpArgName, const std::string& helpText,
354 bool showDefault);
373 virtual int parse(int argc, char **argv);
374
375 protected:
376 // Prints usage information.
377 virtual void printUsage();
378 // Determines whether an option should have an argument based on its type.
379 virtual int optionValueArg(int valueType);
380 // Gets the default value for a given option, as a string.
381 virtual std::string getDefault(const AppOption& opt);
382 // Dispatch function for handling configured options.
383 virtual int handleOptionReturn(int opt, const std::string& optarg);
384 // Option type handler functions.
385 // -- AppOptionValueType::NONE
386 virtual int handleOptionNone(int optindex, const std::string& optarg);
387 // -- AppOptionValueType::INTEGER
388 virtual int handleOptionInt(int optindex, const std::string& optarg);
389 // -- AppOptionValueType::FLOAT
390 virtual int handleOptionFloat(int optindex, const std::string& optarg);
391 // -- AppOptionValueType::DOUBLE
392 virtual int handleOptionDouble(int optindex, const std::string& optarg);
393 // -- AppOptionValueType::STRING
394 virtual int handleOptionString(int optindex, const std::string& optarg);
395 // -- AppOptionValueType::BOOLEAN
396 virtual int handleOptionBool(int optindex, const std::string& optarg);
397 // -- AppOptionValueType::BOOLEAN_SET_TRUE
398 virtual int handleOptionBooleanSetTrue(int optindex, const std::string& optarg);
399 // -- AppOptionValueType::BOOLEAN_SET_FALSE
400 virtual int handleOptionBooleanSetFalse(int optindex, const std::string& optarg);
401
402 public:
408
409 protected:
410 AppOptionList _optionList;
411 std::string _description;
412 std::string _version;
413 std::string _executable;
414 std::string _unparsedArgText;
415 std::string _epilogText;
416 bool _allowUnknownOption;
417 int _displayWidth;
418 typedef int (AppOptionParser::*OptionHelper)(int optindex, const std::string& optarg);
419 std::map<int, OptionHelper> _optionHelpers;
420 };
421
422
434 class App
435 {
436 public:
440 App();
444 virtual ~App() { };
461 virtual int run(int argc, char *argv[]);
462
463 protected:
474 virtual void defineOptions(const char *argv0);
475 // . The return value will
476 // be passed up to the host operating system as a return code.
484 virtual int mainLoop();
485 // Parses command-line options.
486 virtual int parseCommandLine(int argc, char *argv[]);
487
488 public:
492 std::string description;
496 std::string version;
497
498 protected:
499 AppOptionParser _optParser;
500 };
501
502} /* namespace LibCyberRadio */
503
504#endif /* INCLUDED_LIBCYBERRADIO_APP_H */
505
virtual void addPostOptionText(const std::string &text)
Adds text that gets displayed after the list of options.
Definition App.cpp:122
AppHelpTextFormatter(int displayWidth=75)
Constructs an AppHelpTextFormatter object.
Definition App.cpp:94
virtual ~AppHelpTextFormatter()
Destroys an AppHelpTextFormatter object.
Definition App.cpp:100
virtual void addPreOptionText(const std::string &text)
Adds text that gets displayed before the list of options.
Definition App.cpp:104
virtual void addOptionText(const std::string &option, const std::string &text)
Adds an option and its help text to the list of options.
Definition App.cpp:110
virtual std::string getFormattedText()
Gets the formatted help text.
Definition App.cpp:128
Parses command-line options supported by the application.
Definition App.h:249
void setEpilogText(const std::string &epilogText)
Sets the epilog portion of the usage information; that is, the part that comes after the list of supp...
Definition App.cpp:277
void allowUnknownOption(bool allow=true)
Determines whether or not the parser will allow unknown options to pass through the parser without ge...
Definition App.cpp:262
void setExecutable(const std::string &executable)
Sets the portion of the usage information where the executable name is displayed.
Definition App.cpp:282
void setDescription(const std::string &description)
Sets the portion of the usage information where the application description is displayed.
Definition App.cpp:267
virtual void addOption(const AppOption &opt)
Adds an allowed option to the parser.
Definition App.cpp:247
AppOptionParser()
Constructs an AppOptionParser object.
Definition App.cpp:214
virtual ~AppOptionParser()
Destroys an AppOptionParser object.
Definition App.cpp:243
BasicStringList unparsedArgs
The collection of arguments that were not dealt with by the parser during option parsing.
Definition App.h:407
void setDisplayWidth(int displayWidth=75)
Sets the display width of the usage information.
Definition App.cpp:272
void setUnparsedArgText(const std::string &unparsedArgText)
Sets the portion of the usage information that represents the collection of unparsed arguments.
Definition App.cpp:287
void setVersion(const std::string &version)
Sets the portion of the usage information that represents the application version.
Definition App.cpp:292
virtual int parse(int argc, char **argv)
Parses the command-line options.
Definition App.cpp:297
Defines a command-line option that is supported by an application.
Definition App.h:29
std::string shortName
Short option name, without leading hyphen.
Definition App.h:86
bool showDefault
Show the default value in the option help text.
Definition App.h:113
static int TYPE_NONE
Option does not set a value.
Definition App.h:119
static int TYPE_DOUBLE
Option sets a double-precision floating-point value.
Definition App.h:131
std::string longName
Long option name, without leading hyphens.
Definition App.h:91
virtual ~AppOption()
Destroys an AppOption object.
Definition App.cpp:84
static int TYPE_INTEGER
Option sets an integer value.
Definition App.h:123
static int TYPE_BOOLEAN_SET_TRUE
Option sets a boolean value that is False by default but becomes True if this option is specified.
Definition App.h:144
void * valuePtr
Pointer to a variable that holds the option value.
Definition App.h:100
int valueType
Type of value that this option sets.
Definition App.h:96
virtual AppOption & operator=(const AppOption &opt)
Makes one AppOption object equivalent to another.
Definition App.cpp:69
static int TYPE_BOOLEAN_SET_FALSE
Option sets a boolean value that is True by default but becomes False if this option is specified.
Definition App.h:149
static int TYPE_BOOLEAN
Option explicitly sets a boolean value.
Definition App.h:139
AppOption()
Constructs an empty AppOption object.
Definition App.cpp:37
static int TYPE_FLOAT
Option sets a floating-point value.
Definition App.h:127
static int TYPE_STRING
Option sets a string value.
Definition App.h:135
std::string helpArgName
Option argument name to display in the help text.
Definition App.h:104
std::string helpText
Option help text.
Definition App.h:108
virtual ~App()
Destroys an App object.
Definition App.h:444
std::string description
The application description.
Definition App.h:492
virtual int mainLoop()
Defines the application's main processing loop.
Definition App.cpp:673
virtual int run(int argc, char *argv[])
Runs the application.
Definition App.cpp:652
std::string version
The application version.
Definition App.h:496
virtual void defineOptions(const char *argv0)
Defines which command-line options are supported by the application, as well as any help text that is...
Definition App.cpp:661
App()
Constructs an App object.
Definition App.cpp:646
Defines functionality for LibCyberRadio applications.
Definition App.h:24
BASIC_LIST_CONTAINER< AppOption > AppOptionList
Defines a list of AppOption objects.
Definition App.h:156
BASIC_LIST_CONTAINER< std::string > BasicStringList
Type representing a list of strings.
Definition BasicList.h:25