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