ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
appcli.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header is part of module \alib_app of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib::app {
9
10///TODOX: Check ollama doxing ;-)
11/// Application base class with integrated command-line processing.
12///
13/// This class extends #"App" with a ready-to-use command-line interface (CLI)
14/// powered by module \alib_cli. It introduces a small set of additional bootstrap
15/// states (methods) to define CLI options/commands, to import configuration influenced by CLI
16/// arguments, and to optionally perform an early "dry-run" check. Subclasses typically extend and
17/// customize the CLI in #"bsCLIDefine" and #"bsConfigureCLI" and may override the processing
18/// loop via #"cliProcessCmd".
19class AppCli : public App {
20 protected:
21
22
23 //--------------------------------------------- Types --------------------------------------------
24 /// Additional bootstrap states executed by this class.
25 /// Extends #"App::States;*" with custom phases. The states and their corresponding
26 /// virtual methods are registered by the constructor.
27 enum class States {
28 CLIDefine , ///< Inserted before #"States::ImportConfig": define CLI options and commands.
29 CLIReadOptions , ///< Inserted behind #"CLIDefine".
30 ReadDryRunOption , ///< Inserted before #"States::SetupALox": evaluate early flags (e.g., dry-run).
31 ConfigureCLI , ///< Inserted after #"States::ImportConfig": finalize CLI setup based on config.
32 };
33
34 //--------------------------------------------- Fields -------------------------------------------
35 /// The command-line parser provided by module \alib_cli.
37
38 /// \brief Flag to stop the CLI processing loop.
39 ///
40 /// When set to \c true, no further commands from the command line are processed.
41 /// Typical usage is the built-in or custom \c help command: once help text has been
42 /// generated and printed, further command processing is not desired.
43 bool cliStop =false;
44
45 //---------------------------------------- Overriding Built --------------------------------------
46 /// Maps an #"excep Exception" to an application exit code.
47 /// @param exception The exception that terminated the run.
48 /// \return An enumeration value convertible to an integer exit code.
49 Enum exceptionToExitCode( Exception& exception ) override;
50
51 /// Contributes configuration file locations.
52 /// CLI arguments may affect which configuration files are considered. Subclasses can
53 /// override #"getConfigFilePathsFromCLIParam" to implement CLI-based selection.
54 /// @param files Output vector to append discovered file descriptors to.
56
57 /// Called after the application's basic name, version and info have been determined.
58 /// Allows adjusting CLI-visible metadata accordingly.
59 void onBsSetNameVersionAndInfo() override;
60
61 /// Preloads configuration variables, potentially seeding defaults based on early CLI
62 /// evaluation performed in #"bsReadDryRunOption".
63 void onBsPreloadVariables() override;
64
65 /// Signals the start of the application's run phase. May initialize CLI-dependent
66 /// resources just before command processing begins.
67 void onRunStart() override;
68
69 /// Main run method. The default implementation processes CLI commands until
70 /// #"cliStop" is set or all commands are consumed.
71 void onRun() override;
72
73 /// Signals the end of the application's run phase. Cleanup that depends on CLI state
74 /// can be performed here.
75 void onRunEnd() override;
76
77 //-------------------------------------- New virtual methods -------------------------------------
78 /// Implements #"States::CLIDefine": define CLI options, flags and commands.
79 virtual void bsCLIDefine();
80
81 /// Implements #"States::CLIReadOptions": calls #"CommandLine::ReadOptions;*" on
82 /// member #".cli". This is done quite early in the boot sequence to allow option '--config'
83 /// to change the configuration files used.
84 virtual void bsCLIReadOptions();
85
86 /// Implements #"States::ConfigureCLI": finalize CLI after configuration import.
87 virtual void bsConfigureCLI();
88
89 /// Implements #"States::ReadDryRunOption": evaluate early flags (e.g., --dry-run).
90 virtual void bsReadDryRunOption();
91
92 /// Used when errors are detected. May be overwritten, for example to suppress help output
93 /// and/or otherwise change the standard behavior.
94 /// @param exitCode The exit code to that in field #"App::machine;*". Also used to
95 /// write an error message by retrieving
96 /// #"ExitCodeDecl::FormatString;*".
97 /// @param helpTopic An optional parameter that defines a topic for the help text written
98 /// to #"App::cOut;*".
99 /// @param formatParam1 An optional parameter to be used with the format string.
100 /// @param formatParam2 An optional second parameter to be used with the format string.
101 virtual void exitWithHelpOutput( Enum exitCode, const String& helpTopic= NULL_STRING,
102 Box formatParam1 = EMPTY_STRING,
103 Box formatParam2 = EMPTY_STRING );
104
105 /// Adds configuration file paths influenced by a dedicated CLI parameter.
106 /// Subclasses may parse a CLI option (for example, `--config <file>` or a search path)
107 /// and append corresponding #"ConfigFileDescriptor" entries to \p files.
108 /// @param files Output vector to append file descriptors to.
110
111 /// Processes a single parsed CLI command.
112 /// @param cmd The command to execute.
113 /// \return \c true if the command was handled successfully; \c false to indicate that
114 /// the command was not recognized or could not be executed. Implementations
115 /// may set #"cliStop" to terminate further processing.
116 virtual bool cliProcessCmd(cli::Command* cmd );
117
118
119 public:
120 /// \brief Constructs an instance and registers additional bootstrap states.
121 AppCli();
122};
123
124} // namespace [app]
#define ALIB_EXPORT
Definition alib.inl:562
Enum exceptionToExitCode(Exception &exception) override
Definition appcli.cpp:54
void onRunEnd() override
Definition appcli.cpp:234
void onRunStart() override
Definition appcli.cpp:163
bool cliStop
Flag to stop the CLI processing loop.
Definition appcli.inl:43
virtual bool cliProcessCmd(cli::Command *cmd)
Definition appcli.cpp:246
virtual void getConfigFilePathsFromCLIParam(StdVectorMA< ConfigFileDescriptor > &files)
Definition appcli.cpp:138
virtual void bsReadDryRunOption()
Implements #"States::ReadDryRunOption": evaluate early flags (e.g., –dry-run).
Definition appcli.cpp:109
virtual void bsCLIReadOptions()
Definition appcli.cpp:95
cli::CommandLine cli
The command-line parser provided by module ALib CLI.
Definition appcli.inl:36
void getConfigFilePaths(StdVectorMA< ConfigFileDescriptor > &files) override
Definition appcli.cpp:133
AppCli()
Constructs an instance and registers additional bootstrap states.
Definition appcli.cpp:38
void onBsPreloadVariables() override
Definition appcli.cpp:128
void onRun() override
Definition appcli.cpp:200
virtual void exitWithHelpOutput(Enum exitCode, const String &helpTopic=NULL_STRING, Box formatParam1=EMPTY_STRING, Box formatParam2=EMPTY_STRING)
Definition appcli.cpp:191
virtual void bsCLIDefine()
Implements #"States::CLIDefine": define CLI options, flags and commands.
Definition appcli.cpp:87
virtual void bsConfigureCLI()
Implements #"States::ConfigureCLI": finalize CLI after configuration import.
Definition appcli.cpp:97
@ CLIReadOptions
Inserted behind #"CLIDefine".
Definition appcli.inl:29
@ ConfigureCLI
Inserted after #"States::ImportConfig": finalize CLI setup based on config.
Definition appcli.inl:31
@ CLIDefine
Inserted before #"States::ImportConfig": define CLI options and commands.
Definition appcli.inl:28
@ ReadDryRunOption
Inserted before #"States::SetupALox": evaluate early flags (e.g., dry-run).
Definition appcli.inl:30
void onBsSetNameVersionAndInfo() override
Definition appcli.cpp:77
App(camp::Camp *appCamp)
Definition app.cpp:43
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2254
constexpr const String EMPTY_STRING
An empty string of the default character type.
Definition string.inl:2234
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1135
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
exceptions::Exception Exception
Type alias in namespace alib.
std::vector< T, StdMA< T > > StdVectorMA
Type alias in namespace alib.
boxing::Enum Enum
Type alias in namespace alib.
Definition enum.inl:211
A command of a ALib CLI command-line.