ALib C++ Library
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 \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 /// 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 /// \alib{cli;OptionDecl::IdentifierChar}. Otherwise, matching is done case-insensitive and
32 /// with respecting \alib{cli;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 /// \alib{cli;CommandLine::ReadOptions}, which parses and collects options in filed
41 /// \alib{cli;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 \alib{cli;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 /// \alib{cli;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 /// \alib{cli;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 \alib{cli;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 /// \alib{cli;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 \alib{cli;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 /// \alib{cli;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 /// This method accepts either a command object or an option object that the command line
121 /// instance uses to process help requests. Only one of the objects has to be provided, the
122 /// other has to be \c nullptr.
123 ///
124 /// If no argument is set in \p{helpCmd} (respectively \p{helpOpt}), the next argument is peeked
125 /// and checked to be a command, option, parameter or special help topic found in resource
126 /// string "HlpAddnlTopics".
127 ///
128 /// If found, the argument is consumed and stored in \p{helpCmd} (respectively \p{helpOpt}).
129 /// If not, the general help text is generated.
130 ///
131 /// @param cmdLine The command line instance.
132 /// @param helpCmd The command to write the help text for.
133 /// @param helpOpt The option to write the help text for.
134 /// @param text The target text.
135 /// @return \c true on success. \c false if an argument was given that is not recognized or
136 /// if a topic list was found in the next argument where only some of the topics
137 /// could be identified.
138 static ALIB_DLL
139 bool GetHelp( CommandLine& cmdLine, Command* helpCmd, Option* helpOpt, Paragraphs& text );
140
141 /// Reads a dry-run options and stores the result in \alib{cli;CommandLine::DryRun}.
142 ///
143 /// Option arguments as defined with records of enumeration \alib{cli;DryRunModes} are
144 /// accepted. These records are resourced and default to:
145 /// \snippet "cli/clicamp.cpp" DOX_CLI_DRYRUN_RESOURCES
146 ///
147 /// If no argument is set in the given \p{dryOpt}, the next unread CLI-argument is checked
148 /// for being parsable as an element of enum \b DryRunModes. If yes, the CLI-argument is
149 /// consumed and duly stored in \p{dryOpt}.<br>
150 /// In case no argument was set (or successfully peeked), \alib{cli;DryRunModes::Application} is
151 /// chosen and stored.
152 ///
153 /// If one of the modes offered by enumeration \b DryRunModes should not be recognizable,
154 /// three ways of implementing this exist:
155 /// 1. Do not use this method but implement a custom version. In case that only
156 /// \alib{cli;DryRunModes::Application} should be accepted, instead calling this util method,
157 /// simply set field \alib{cli;CommandLine::DryRun} to this value.
158 /// 2. Check for the "forbidden" argument type after the invocation of this method and
159 /// exit your cli app
160 /// 3. Modify this module's resource string <b>"CLI/DRM"</b> to not contain the suppressed
161 /// argument's record. (With that, the defaulted argument names can also be modified).
162 ///
163 /// By modifying the resource string, it is also possible to add custom options. Remeber, that
164 /// it is allowed in C++ to have an enum element evaluate to any integral, regardless
165 /// whether it is defined in the C++ definition or not.
166 ///
167 /// @param cmdLine The command line instance.
168 /// @param dryOpt The option object parsed.
169 /// @return \c true on success. \c false if an argument was given that is not recognized.
170 static ALIB_DLL
171 bool GetDryOpt( CommandLine& cmdLine, Option& dryOpt );
172
173 /// Dumps the configuration.
174 /// Shows which commands, options, parameters and errors are set with enums and their
175 /// meta info.
176 /// Useful during development.
177 ///
178 /// @param cmdLine The friend object we work on.
179 /// @param text The target text.
180 /// @return An internal \c AString object containing the dump text. (Beware of concurrent
181 /// debugging threads :-)
182 static ALIB_DLL
184
185 /// Write in human-readable form, which commands and options have been read from the
186 /// command line.
187 ///
188 /// This is useful for debugging as well as to implement a "dry run" option of the
189 /// CLI application, that offers the user a list of what is parsed with a given set of
190 /// CLI arguments. In this case, method read \alib{cli;CommandLine::ReadNextCommands} should
191 /// be invoked after the provisions of the various definitions.
192 ///
193 /// Probably, depending on the command syntax, not all commands can be parsed prior
194 /// to executing them. However, options can.
195 ///
196 /// @param cmdLine The friend object we work on.
197 /// @param text The target text.
198 /// @returns Returns an internal \c AString object containing the dump text.
199 /// (Beware of concurrent debugging threads :-)
200 static ALIB_DLL
202};
203
204} // namespace alib[::cli]
205
206/// Type alias in namespace \b alib.
208
209
210} // namespace [alib]
static ALIB_DLL ParameterDecl * GetParameterDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:53
static ALIB_DLL bool GetDryOpt(CommandLine &cmdLine, Option &dryOpt)
Definition cliutil.cpp:264
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:411
static ALIB_DLL AString & DumpDeclarations(CommandLine &cmdLine, Paragraphs &text)
Definition cliutil.cpp:296
static ALIB_DLL CommandDecl * GetCommandDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:45
static ALIB_DLL bool GetHelp(CommandLine &cmdLine, Command *helpCmd, Option *helpOpt, Paragraphs &text)
Definition cliutil.cpp:83
static ALIB_DLL AString GetCommandUsageFormat(CommandLine &cmdLine, CommandDecl &commandDecl)
Definition cliutil.cpp:62
static ALIB_DLL integer GetExitCode(CommandLine &cmdLine, Exception &exception)
Definition cliutil.inl:108
HashMap< MonoAllocator, Enum, ExitCodeDecl * > ExitCodeDecls
Possible Errors.
ALIB_DLL const Enum & Type() const
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_ERROR(domain,...)
Definition alib.inl:1062
cli::CLIUtil CLIUtil
Type alias in namespace alib.
Definition cliutil.inl:207
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:2189
TEnum Get() const
Definition enum.inl:75
A command of a ALib CLI command line.