args-parser 6.3.3
Loading...
Searching...
No Matches
help.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__HELP_HPP__INCLUDED
32#define ARGS__HELP_HPP__INCLUDED
33
34// Args include.
35#include "arg.hpp"
37#include "context.hpp"
38#include "utils.hpp"
39#include "exceptions.hpp"
40#include "types.hpp"
41#include "command.hpp"
42
43// C++ include.
44#include <memory>
45
46#ifdef ARGS_TESTING
47 #ifndef ARGS_QSTRING_BUILD
48 // C++ include.
49 #include <sstream>
50 #endif
51#endif
52
53
54namespace Args {
55
56#ifdef ARGS_TESTING
57 #ifdef ARGS_WSTRING_BUILD
58 extern std::wstringstream g_argsOutStream;
59 #elif defined( ARGS_QSTRING_BUILD )
60 extern OutStreamType g_argsOutStream;
61 #else
62 extern std::stringstream g_argsOutStream;
63 #endif
64#else
65 static OutStreamType & g_argsOutStream = outStream();
66#endif // ARGS_TESTING
67
68
69//
70// Help
71//
72
74class Help
75 : public Arg
76{
77public:
78 explicit Help( bool throwExceptionOnPrint = true );
79
81 void setExecutable( const String & exe );
82
84 void setAppDescription( const String & desc );
85
87 void setLineLength( String::size_type length );
88
90 void setPrinter( std::unique_ptr< HelpPrinterIface > p );
91
92protected:
98 void process(
100 Context & context ) override;
101
103 void setCmdLine( CmdLine * cmdLine ) override
104 {
106
107 m_printer->setCmdLine( cmdLine );
108 }
109
110private:
112 std::unique_ptr< HelpPrinterIface > m_printer;
114 bool m_throwExceptionOnPrint;
115}; // class Help
116
117
118//
119// Help
120//
121
122inline void
124{
125 m_printer->setExecutable( exe );
126}
127
128inline void
130{
131 m_printer->setAppDescription( desc );
132}
133
134inline void
135Help::setLineLength( String::size_type length )
136{
137 m_printer->setLineLength( length );
138}
139
140inline void
141Help::setPrinter( std::unique_ptr< HelpPrinterIface > p )
142{
143 m_printer.reset( p.release() );
144}
145
146inline void
148{
149 if( !context.atEnd() )
150 {
151 String arg = *context.next();
152
153 // Argument or flag.
154 if( details::isArgument( arg ) || details::isFlag( arg ) )
155 m_printer->print( arg, g_argsOutStream );
156 // Command?
157 else
158 {
159 auto * tmp = m_printer->findArgument( arg );
160
161 // Command.
162 if( tmp && tmp->type() == ArgType::Command )
163 {
164 bool printed = false;
165
166 auto * cmd = static_cast< Command* > ( tmp );
167
168 while( !context.atEnd() )
169 {
170 arg = *context.next();
171
172 if( tmp && tmp->type() == ArgType::Command )
173 {
174 cmd = static_cast< Command* > ( tmp );
175
176 // Argument or flag.
177 if( details::isArgument( arg ) || details::isFlag( arg ) )
178 {
179 m_printer->print( arg, g_argsOutStream, cmd );
180
181 printed = true;
182
183 break;
184 }
185 // Command?
186 else
187 tmp = cmd->findChild( arg );
188 }
189 else
190 break;
191 }
192
193 if( !printed )
194 {
195 if( tmp )
196 m_printer->print( tmp->name(), g_argsOutStream,
197 ( cmd != tmp ? cmd : nullptr ) );
198 else
199 m_printer->print( g_argsOutStream );
200 }
201 }
202 else
203 m_printer->print( g_argsOutStream );
204 }
205 }
206 else
207 m_printer->print( g_argsOutStream );
208
209 setDefined( true );
210
211 if( m_throwExceptionOnPrint )
213}
214
215} /* namespace Args */
216
217#endif // ARGS__HELP_HPP__INCLUDED
Argument with one value that can be present only once in the command line.
Definition arg.hpp:58
void setDefined(bool on=true)
Set defined.
Definition arg.hpp:427
virtual void setCmdLine(CmdLine *cmdLine)
Set command line parser.
CmdLine * cmdLine() const
CmdLine is class that holds all rguments and parse command line arguments in the correspondence with ...
Definition cmd_line.hpp:123
Command in the command line interface.
Definition command.hpp:53
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:60
ContextInternal::iterator next()
Definition context.hpp:136
This exception notifies about that help has been printed.
Help argument.
Definition help.hpp:76
void process(Context &context) override
Process argument's staff, for example take values from context.
Definition help.hpp:147
void setLineLength(String::size_type length)
Set line length for the help.
Definition help.hpp:135
void setExecutable(const String &exe)
Set executable name.
Definition help.hpp:123
void setAppDescription(const String &desc)
Set description for the application.
Definition help.hpp:129
void setPrinter(std::unique_ptr< HelpPrinterIface > p)
Set printer.
Definition help.hpp:141
void setCmdLine(CmdLine *cmdLine) override
Set command line parser.
Definition help.hpp:103
Definition api.hpp:42
std::string String
String type.
Definition types.hpp:324
@ Command
Command.
std::ostream OutStreamType
Out stream type.
Definition types.hpp:330