args-parser 6.3.6
Loading...
Searching...
No Matches
help.hpp
Go to the documentation of this file.
1
2/*
3 SPDX-FileCopyrightText: 2026 Igor Mironchik <igor.mironchik@gmail.com>
4 SPDX-License-Identifier: MIT
5*/
6
7#ifndef ARGS__HELP_HPP__INCLUDED
8#define ARGS__HELP_HPP__INCLUDED
9
10// Args include.
11#include "arg.hpp"
12#include "command.hpp"
13#include "context.hpp"
14#include "exceptions.hpp"
16#include "types.hpp"
17#include "utils.hpp"
18
19// C++ include.
20#include <memory>
21
22#ifdef ARGS_TESTING
23#ifndef ARGS_QSTRING_BUILD
24// C++ include.
25#include <sstream>
26#endif
27#endif
28
29namespace Args
30{
31
32#ifdef ARGS_TESTING
33#ifdef ARGS_WSTRING_BUILD
34extern std::wstringstream g_argsOutStream;
35#elif defined(ARGS_QSTRING_BUILD)
36extern OutStreamType g_argsOutStream;
37#else
38extern std::stringstream g_argsOutStream;
39#endif
40#else
41static OutStreamType &g_argsOutStream = outStream();
42#endif // ARGS_TESTING
43
44//
45// Help
46//
47
49class Help : public Arg
50{
51public:
52 explicit Help(bool throwExceptionOnPrint = true);
53
55 Help &setExecutable(const String &exe);
56
58 Help &setAppDescription(const String &desc);
59
61 Help &setLineLength(String::size_type length);
62
64 Help &setPrinter(std::unique_ptr<HelpPrinterIface> p);
65
66protected:
72 void process(
74 Context &context) override;
75
77 void setCmdLine(CmdLine *cmdLine) override
78 {
80
81 m_printer->setCmdLine(cmdLine);
82 }
83
84private:
86 std::unique_ptr<HelpPrinterIface> m_printer;
88 bool m_throwExceptionOnPrint;
89}; // class Help
90
91//
92// Help
93//
94
95inline Help &Help::setExecutable(const String &exe)
96{
97 m_printer->setExecutable(exe);
98 return *this;
99}
100
102{
103 m_printer->setAppDescription(desc);
104 return *this;
105}
106
107inline Help &Help::setLineLength(String::size_type length)
108{
109 m_printer->setLineLength(length);
110 return *this;
111}
112
113inline Help &Help::setPrinter(std::unique_ptr<HelpPrinterIface> p)
114{
115 m_printer.reset(p.release());
116 return *this;
117}
118
119inline void Help::process(Context &context)
120{
121 if (!context.atEnd()) {
122 String arg = *context.next();
123
124 // Argument or flag.
125 if (details::isArgument(arg) || details::isFlag(arg)) {
126 m_printer->print(arg, g_argsOutStream);
127 // Command?
128 } else {
129 auto *tmp = m_printer->findArgument(arg);
130
131 // Command.
132 if (tmp && tmp->type() == ArgType::Command) {
133 bool printed = false;
134
135 auto *cmd = static_cast<Command *>(tmp);
136
137 while (!context.atEnd()) {
138 arg = *context.next();
139
140 if (tmp && tmp->type() == ArgType::Command) {
141 cmd = static_cast<Command *>(tmp);
142
143 // Argument or flag.
144 if (details::isArgument(arg) || details::isFlag(arg)) {
145 m_printer->print(arg, g_argsOutStream, cmd);
146
147 printed = true;
148
149 break;
150 }
151 // Command?
152 else {
153 tmp = cmd->findChild(arg);
154 }
155 } else {
156 break;
157 }
158 }
159
160 if (!printed) {
161 if (tmp) {
162 m_printer->print(tmp->name(), g_argsOutStream, (cmd != tmp ? cmd : nullptr));
163 } else {
164 m_printer->print(g_argsOutStream);
165 }
166 }
167 } else {
168 m_printer->print(g_argsOutStream);
169 }
170 }
171 } else {
172 m_printer->print(g_argsOutStream);
173 }
174
175 setDefined(true);
176
177 if (m_throwExceptionOnPrint) {
179 }
180}
181
182} /* namespace Args */
183
184#endif // ARGS__HELP_HPP__INCLUDED
Arg & setDefined(bool on=true)
Set defined.
Definition arg.hpp:395
Arg(Char flag, T &&name, bool isWithValue=false, bool isRequired=false)
Construct argument with flag and name.
Definition arg.hpp:245
virtual void setCmdLine(CmdLine *cmdLine)
Set command line parser.
CmdLine * cmdLine() const
Definition arg_iface.hpp:86
CmdLine is class that holds all rguments and parse command line arguments in the correspondence with ...
Definition cmd_line.hpp:98
Command in the command line interface.
Definition command.hpp:28
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:36
ContextInternal::iterator next()
Definition context.hpp:106
This exception notifies about that help has been printed.
Help & setAppDescription(const String &desc)
Set description for the application.
Definition help.hpp:101
void process(Context &context) override
Process argument's staff, for example take values from context.
Definition help.hpp:119
Help & setPrinter(std::unique_ptr< HelpPrinterIface > p)
Set printer.
Definition help.hpp:113
Help(bool throwExceptionOnPrint=true)
Definition cmd_line.hpp:764
Help & setExecutable(const String &exe)
Set executable name.
Definition help.hpp:95
void setCmdLine(CmdLine *cmdLine) override
Set command line parser.
Definition help.hpp:77
Help & setLineLength(String::size_type length)
Set line length for the help.
Definition help.hpp:107
Definition api.hpp:18
std::ostream OutStreamType
Out stream type.
Definition types.hpp:320
std::string String
String type.
Definition types.hpp:314
@ Command
Command.
Definition enums.hpp:34