ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
cliutil.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_cli of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8namespace alib { namespace cli {
9
10//==================================================================================================
11/// This is a friend class of \alib{cli;CommandLine} that exposes a collection of utility methods
12/// useful for CLI applications.
13///
14/// The methods found here are static and receive the friend \b %CommandLine object. They have been
15/// gathered in this class to keep \b %CommandLine tidy and more easily understandable as only
16/// needed methods are found there.
17///
18/// \note
19/// The nature of this class is to provide a basic, standard functionality. It was created while
20/// building a CLI application based on this library and provided for convenience.
21///
22/// Methods found here, might be used as a jump start for own implementations.
23/// Their documentation is considered only fundamental.
24/// For details, please consult the source code.
25//==================================================================================================
27{
28 public:
29 //==============================================================================================
30 /// Searches and if found, retrieves the declaration of the option identified by
31 /// \p{identString} which, if it contains a single character is compared to the
32 /// \alib{cli;OptionDecl::IdentifierChar}. Otherwise, matching is done case-insensitive and
33 /// with respecting \alib{cli;OptionDecl::MinimumRecognitionLength}.
34 ///
35 /// This method is useful to implement a help command or option, with an optional "topic"
36 /// parameter.
37 ///
38 /// \note
39 /// If parsing of arguments should be (or has to be) customized, of course this method
40 /// can also be used for implementing such custom code. Otherwise, use method
41 /// \alib{cli;CommandLine::ReadOptions}, which parses and collects options in filed
42 /// \alib{cli;CommandLine::Options}.
43 ///
44 /// \see Methods #GetCommandDecl, #GetParameterDecl.
45 ///
46 /// @param cmdLine The friend object we work on.
47 /// @param identString The identifier string of the option to search. If this is a single
48 /// character, the short identifier is searched.
49 /// @return The object of type \alib{cli;OptionDecl}. \c nullptr if not found.
50 //==============================================================================================
51 static ALIB_DLL
52 OptionDecl* GetOptionDecl( CommandLine& cmdLine, const String& identString );
53
54 //==============================================================================================
55 /// Searches and if found, retrieves the declaration of the command identified by
56 /// \p{identString}. Matching is done case-insensitive and with respecting
57 /// \alib{cli;CommandDecl::MinimumRecognitionLength}.
58 ///
59 /// This method is useful to implement a help command or option, with an optional "topic"
60 /// parameter.
61 ///
62 /// \note
63 /// If parsing of arguments should be (or has to be) customized, of course this method
64 /// can also be used for implementing such custom code. Otherwise, use
65 /// \alib{cli;CommandLine::NextCommand} to retrieve command objects (instead of command
66 /// declarations).
67 ///
68 /// \see Methods #GetOptionDecl, #GetParameterDecl.
69 ///
70 /// @param cmdLine The friend object we work on.
71 /// @param identString The identifier of the command to search.
72 /// @return The object of type \alib{cli;CommandDecl}. \c nullptr if not found.
73 //==============================================================================================
74 static ALIB_DLL
75 CommandDecl* GetCommandDecl( CommandLine& cmdLine, const String& identString );
76
77 //==============================================================================================
78 /// Searches and if found, retrieves the declaration of the parameter identified by
79 /// \p{identString}. Matching is done case-insensitive and with respecting
80 /// \alib{cli;CommandDecl::MinimumRecognitionLength}.
81 ///
82 /// This method is useful to implement a help command (or option), with an optional "topic"
83 /// parameter.
84 ///
85 /// \see Methods #GetOptionDecl, #GetCommandDecl.
86 ///
87 /// @param cmdLine The friend object we work on.
88 /// @param identString The identifier of the command to search.
89 /// @return The object of type \alib{cli;CommandDecl}. \c nullptr if not found.
90 //==============================================================================================
91 static ALIB_DLL
92 ParameterDecl* GetParameterDecl( CommandLine& cmdLine, const String& identString );
93
94
95 //==============================================================================================
96 /// Returns an AString providing a formatted help text on the defined command.
97 /// @param cmdLine The command line instance.
98 /// @param commandDecl The declaration of the command to get help on.
99 /// @return The help text.
100 //==============================================================================================
101 static ALIB_DLL
102 AString GetCommandUsageFormat( CommandLine& cmdLine, CommandDecl& commandDecl );
103
105 //==============================================================================================
106 /// Translates exceptions thrown by the \alib_cli_nl library to exit codes defined with
107 /// \alib{cli;CommandLine::DefineExitCodes}.
108 ///
109 /// If the code is not found, this indicates an error in the resource data, as an exit
110 /// code corresponding to the \alib_cli_nl exceptions is obviously missing.
111 /// In this case, \c -1 is returned. With debug-builds an \alib assertion is raised.
112 ///
113 /// @param cmdLine The friend object we work on.
114 /// @param exception The cli exception caught.
115 /// @return The exit code to return to the caller. \c -1, if not found.
116 //==============================================================================================
117 static ALIB_DLL
118 integer GetExitCode( CommandLine& cmdLine, Exception& exception )
119 {
120 auto element= exception.Type().Get<cli::Exceptions>();
121 for( auto& exitCodeDecl : cmdLine.ExitCodeDecls )
122 if( exitCodeDecl.second->AssociatedCLIException() == element )
123 return exitCodeDecl.first.Integral();
124 ALIB_ERROR( "CLI", "No exit code associated with {}.", element )
125 return -1;
126 }
127#include "ALib.Lang.CIMethods.H"
128
129 //==============================================================================================
130 /// Creates a help text from the resource strings.
131 ///
132 /// This method accepts either a command object or an option object that the command line
133 /// instance uses to process help requests. Only one of the objects has to be provided, the
134 /// other has to be \c nullptr.
135 ///
136 /// If no argument is set in \p{helpCmd} (respectively \p{helpOpt}), the next argument is peeked
137 /// and checked to be a command, option, parameter or special help topic found in resource
138 /// string "HlpAddnlTopics".
139 ///
140 /// If found, the argument is consumed and stored in \p{helpCmd} (respectively \p{helpOpt}).
141 /// If not, the general help text is generated.
142 ///
143 /// @param cmdLine The command line instance.
144 /// @param helpCmd The command to write the help text for.
145 /// @param helpOpt The option to write the help text for.
146 /// @param text The target text.
147 /// @return \c true on success. \c false if an argument was given that is not recognized or
148 /// if a topic list was found in the next argument where only some of the topics
149 /// could be identified.
150 //==============================================================================================
151 static ALIB_DLL
152 bool GetHelp( CommandLine& cmdLine, Command* helpCmd, Option* helpOpt, Paragraphs& text );
153
154 //==============================================================================================
155 /// Reads a dry-run options and stores the result in \alib{cli;CommandLine::DryRun}.
156 ///
157 /// Option arguments as defined with records of enumeration \alib{cli;DryRunModes} are
158 /// accepted. These records are resourced and default to:
159 /// \snippet "cli/clicamp.cpp" DOX_CLI_DRYRUN_RESOURCES
160 ///
161 /// If no argument is set in the given \p{dryOpt}, the next unread CLI-argument is checked
162 /// for being parsable as an element of enum \b DryRunModes. If yes, the CLI-argument is
163 /// consumed and duly stored in \p{dryOpt}.<br>
164 /// In case no argument was set (or successfully peeked), \alib{cli;DryRunModes::Application} is
165 /// chosen and stored.
166 ///
167 /// If one of the modes offered by enumeration \b DryRunModes should not be recognizable,
168 /// three ways of implementing this exist:
169 /// 1. Do not use this method but implement a custom version. In case that only
170 /// \alib{cli;DryRunModes::Application} should be accepted, instead calling this util method,
171 /// simply set field \alib{cli;CommandLine::DryRun} to this value.
172 /// 2. Check for the "forbidden" argument type after the invocation of this method and
173 /// exit your cli app
174 /// 3. Modify this module's resource string <b>"CLI/DRM"</b> to not contain the suppressed
175 /// argument's record. (With that, the defaulted argument names can also be modified).
176 ///
177 /// By modifying the resource string, it is also possible to add custom options. Remeber, that
178 /// it is allowed in C++ to have an enum element evaluate to any integral, regardless
179 /// whether it is defined in the C++ definition or not.
180 ///
181 /// @param cmdLine The command line instance.
182 /// @param dryOpt The option object parsed.
183 /// @return \c true on success. \c false if an argument was given that is not recognized.
184 //==============================================================================================
185 static ALIB_DLL
186 bool GetDryOpt( CommandLine& cmdLine, Option& dryOpt );
187
188 //==============================================================================================
189 /// Dumps the configuration.
190 /// Shows which commands, options, parameters and errors are set with enums and their
191 /// meta info.
192 /// Useful during development.
193 ///
194 /// @param cmdLine The friend object we work on.
195 /// @param text The target text.
196 /// @return An internal \c AString object containing the dump text. (Beware of concurrent
197 /// debugging threads :-)
198 //==============================================================================================
199 static ALIB_DLL
201
202 //==============================================================================================
203 /// Write in human-readable form, which commands and options have been read from the
204 /// command line.
205 ///
206 /// This is useful for debugging as well as to implement a "dry run" option of the
207 /// CLI application, that offers the user a list of what is parsed with a given set of
208 /// CLI arguments. In this case, method read \alib{cli;CommandLine::ReadNextCommands} should
209 /// be invoked after the provisions of the various definitions.
210 ///
211 /// Probably, depending on the command syntax, not all commands can be parsed prior
212 /// to executing them. However, options can.
213 ///
214 /// @param cmdLine The friend object we work on.
215 /// @param text The target text.
216 /// @returns Returns an internal \c AString object containing the dump text.
217 /// (Beware of concurrent debugging threads :-)
218 //==============================================================================================
219 static ALIB_DLL
221};
222
223} // namespace alib[::cli]
224
225/// Type alias in namespace \b alib.
227
228
229} // namespace [alib]
230
static ALIB_DLL ParameterDecl * GetParameterDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:55
static ALIB_DLL bool GetDryOpt(CommandLine &cmdLine, Option &dryOpt)
Definition cliutil.cpp:307
static ALIB_DLL OptionDecl * GetOptionDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:34
static ALIB_DLL AString & DumpParseResults(CommandLine &cmdLine, Paragraphs &text)
Definition cliutil.cpp:468
static ALIB_DLL AString & DumpDeclarations(CommandLine &cmdLine, Paragraphs &text)
Definition cliutil.cpp:345
static ALIB_DLL CommandDecl * GetCommandDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:46
static ALIB_DLL bool GetHelp(CommandLine &cmdLine, Command *helpCmd, Option *helpOpt, Paragraphs &text)
Definition cliutil.cpp:89
static ALIB_DLL AString GetCommandUsageFormat(CommandLine &cmdLine, CommandDecl &commandDecl)
Definition cliutil.cpp:65
static ALIB_DLL integer GetExitCode(CommandLine &cmdLine, Exception &exception)
Definition cliutil.inl:118
HashMap< MonoAllocator, Enum, ExitCodeDecl * > ExitCodeDecls
Possible Errors.
ALIB_DLL const Enum & Type() const
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_ERROR(domain,...)
Definition alib.inl:1045
cli::CLIUtil CLIUtil
Type alias in namespace alib.
Definition cliutil.inl:226
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
format::Paragraphs Paragraphs
Type alias in namespace alib.
exceptions::Exception Exception
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
TEnum Get() const
Definition enum.inl:76
A command of a ALib CLI command line.