ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
cliutil.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_app of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace app {
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 #"%CommandLine" object. They have been
15/// gathered in this class to keep #"%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//==================================================================================================
26class CLIUtil {
27 public:
28 /// Searches and if found, retrieves the declaration of the option identified by
29 /// \p{identString} which, if it contains a single character is compared to the
30 /// #"OptionDecl::IdentifierChar;*". Otherwise, matching is done case-insensitive and
31 /// with respecting #"OptionDecl::MinimumRecognitionLength;*".
32 ///
33 /// This method is useful to implement a help command or option, with an optional "topic"
34 /// parameter.
35 ///
36 /// \note
37 /// If parsing of arguments should be (or has to be) customized, of course this method
38 /// can also be used for implementing such custom code. Otherwise, use method
39 /// #"CommandLine::ReadOptions;*", which parses and collects options in filed
40 /// #"CommandLine::Options;*".
41 ///
42 /// \see Methods #"GetCommandDecl", #".GetParameterDecl".
43 ///
44 /// @param cmdLine The friend object we work on.
45 /// @param identString The identifier string of the option to search. If this is a single
46 /// character, the short identifier is searched.
47 /// @return The object of type #"OptionDecl". \c nullptr if not found.
48 static ALIB_DLL
49 OptionDecl* GetOptionDecl( CommandLine& cmdLine, const String& identString );
50
51 /// Searches and if found, retrieves the declaration of the command identified by
52 /// \p{identString}. Matching is done case-insensitive and with respecting
53 /// #"CommandDecl::MinimumRecognitionLength;*".
54 ///
55 /// This method is useful to implement a help command or option, with an optional "topic"
56 /// parameter.
57 ///
58 /// \note
59 /// If parsing of arguments should be (or has to be) customized, of course this method
60 /// can also be used for implementing such custom code. Otherwise, use
61 /// #"CommandLine::NextCommand;*" to retrieve command objects (instead of command
62 /// declarations).
63 ///
64 /// \see Methods #"GetOptionDecl", #".GetParameterDecl".
65 ///
66 /// @param cmdLine The friend object we work on.
67 /// @param identString The identifier of the command to search.
68 /// @return The object of type #"CommandDecl". \c nullptr if not found.
69 static ALIB_DLL
70 CommandDecl* GetCommandDecl( CommandLine& cmdLine, const String& identString );
71
72 /// Searches and if found, retrieves the declaration of the parameter identified by
73 /// \p{identString}. Matching is done case-insensitive and with respecting
74 /// #"CommandDecl::MinimumRecognitionLength;*".
75 ///
76 /// This method is useful to implement a help command (or option), with an optional "topic"
77 /// parameter.
78 ///
79 /// \see Methods #"GetOptionDecl", #"GetCommandDecl".
80 ///
81 /// @param cmdLine The friend object we work on.
82 /// @param identString The identifier of the command to search.
83 /// @return The object of type #"CommandDecl". \c nullptr if not found.
84 static ALIB_DLL
85 ParameterDecl* GetParameterDecl( CommandLine& cmdLine, const String& identString );
86
87
88 /// Returns an AString providing a formatted help text on the defined command.
89 /// @param cmdLine The command-line instance.
90 /// @param commandDecl The declaration of the command to get help on.
91 /// @return The help text.
92 static ALIB_DLL
93 AString GetCommandUsageFormat( CommandLine& cmdLine, CommandDecl& commandDecl );
94
95 /// Creates a help text from the resource strings.
96 ///
97 /// @param cmdLine The command-line instance.
98 /// @param topics A comma-separated list of topics. If empty or nulled, general help is
99 /// created.
100 /// @param text The target text.
101 /// @return \c true on success. \c false if an argument was given that is not recognized or
102 /// if a topic list was found in the next argument where only some of the topics
103 /// could be identified.
104 static ALIB_DLL
105 bool GetHelp( CommandLine& cmdLine, const String& topics, Paragraphs& text );
106
107 /// Creates a help text from the given help command.
108 ///
109 /// This method accepts a command-object that the command-line instance uses to process
110 /// help requests.
111 ///
112 /// If no argument is set in \p{helpCmd}, the next argument is peeked and is checked to be a
113 /// command, option, parameter, or a special help topic found in resource string
114 /// "HlpAddnlTopics".
115 ///
116 /// If found, the argument is consumed and stored in \p{helpCmd}.
117 /// If not, the general help text is generated.
118 ///
119 /// @param cmdLine The command-line instance.
120 /// @param helpCmd The command to write the help text for.
121 /// @param text The target text.
122 /// @return \c true on success. \c false if an argument was given that is not recognized or
123 /// if a topic list was found in the next argument where only some of the topics
124 /// could be identified.
125 static ALIB_DLL
126 bool GetHelp( CommandLine& cmdLine, Command* helpCmd, Paragraphs& text );
127
128 /// Creates a help text from the given help option.
129 ///
130 /// This method accepts an option-object that the command-line instance uses to process
131 /// help requests.
132 ///
133 /// If no argument is set in \p{helpOpt}, the next argument is peeked and is checked to be a
134 /// command, option, parameter or special help topic found in resource string "HlpAddnlTopics".
135 ///
136 /// If found, the argument is consumed and stored in \p{helpOpt}.
137 /// If not, the general help text is generated.
138 ///
139 /// @param cmdLine The command-line instance.
140 /// @param helpOpt The option to write the help text for.
141 /// @param text The target text.
142 /// @return \c true on success. \c false if an argument was given that is not recognized or
143 /// if a topic list was found in the next argument where only some of the topics
144 /// could be identified.
145 static ALIB_DLL
146 bool GetHelp( CommandLine& cmdLine, Option* helpOpt, Paragraphs& text );
147
148 /// Dumps the configuration.
149 /// Shows which commands, options, parameters and errors are set with enums and their
150 /// meta info.
151 /// Useful during development.
152 ///
153 /// @param cmdLine The friend object we work on.
154 /// @param text The target text.
155 /// @return An internal \c AString object containing the dump text. (Beware of concurrent
156 /// debugging threads :-)
157 static ALIB_DLL
159
160 /// Write in human-readable form, which commands and options have been read from the
161 /// command-line.
162 ///
163 /// This is useful for debugging as well as to implement a "dry run" option of the
164 /// CLI application, that offers the user a list of what is parsed with a given set of
165 /// CLI arguments. In this case, method read #"CommandLine::ReadNextCommands;*" should
166 /// be invoked after the provisions of the various definitions.
167 ///
168 /// Probably, depending on the command syntax, not all commands can be parsed prior
169 /// to executing them. However, options can.
170 ///
171 /// @param cmdLine The friend object we work on.
172 /// @param text The target text.
173 /// @returns Returns an internal \c AString object containing the dump text.
174 /// (Beware of concurrent debugging threads :-)
175 static ALIB_DLL
177};
178
179} // namespace alib[::app]
180
181/// Type alias in namespace #"%alib".
183
184
185} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
static AString & DumpParseResults(CommandLine &cmdLine, Paragraphs &text)
Definition cliutil.cpp:352
static OptionDecl * GetOptionDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:4
static AString & DumpDeclarations(CommandLine &cmdLine, Paragraphs &text)
Definition cliutil.cpp:237
static AString GetCommandUsageFormat(CommandLine &cmdLine, CommandDecl &commandDecl)
Definition cliutil.cpp:32
static ParameterDecl * GetParameterDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:23
static CommandDecl * GetCommandDecl(CommandLine &cmdLine, const String &identString)
Definition cliutil.cpp:15
static bool GetHelp(CommandLine &cmdLine, const String &topics, Paragraphs &text)
Definition cliutil.cpp:87
Definition alox.cpp:14
app::CLIUtil CLIUtil
Type alias in namespace #"%alib".
Definition cliutil.hpp:182
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
format::Paragraphs Paragraphs
Type alias in namespace #"%alib".
A command argument of the command-line.