args-parser 6.3.3
Loading...
Searching...
No Matches
command.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__COMMAND_HPP__INCLUDED
32#define ARGS__COMMAND_HPP__INCLUDED
33
34// Args include.
35#include "api.hpp"
36#include "group_iface.hpp"
37#include "utils.hpp"
38#include "context.hpp"
39#include "value_utils.hpp"
40#include "enums.hpp"
41#include "types.hpp"
42
43
44namespace Args {
45
46//
47// Command
48//
49
52 : public GroupIface
53{
54 friend class CmdLine;
55 friend class HelpPrinter;
56 friend class Help;
57
58public:
59 template< typename T >
60 explicit Command( T && nm,
62 bool isSubCommandRequired = false )
63 : GroupIface( std::forward< T > ( nm ) )
64 , m_opt( opt )
65 , m_isDefined( false )
66 , m_isSubCommandRequired( isSubCommandRequired )
67 , m_subCommand( nullptr )
68 {
69 if( details::isArgument( name() ) || details::isFlag( name() ) )
70 throw BaseException( String( SL( "Command's name can't "
71 "start with \"-\" whereas you are trying to set name to \"" ) ) +
72 name() + SL( "\"." ) );
73
74 if( name().empty() )
75 throw BaseException(
76 String( SL( "Command can't be with empty name." ) ) );
77
78 switch( m_opt )
79 {
81 {
82 m_valueSpecifier = SL( "arg" );
83 }
84 break;
85
87 {
88 m_valueSpecifier = SL( "args" );
89 }
90 break;
91
92 default :
93 break;
94 }
95 }
96
97 virtual ~Command()
98 {
99 }
100
102 ArgType type() const override
103 {
104 return ArgType::Command;
105 }
106
108 bool isDefined() const override
109 {
110 return m_isDefined;
111 }
112
114 bool isWithValue() const override
115 {
116 return ( m_opt == ValueOptions::OneValue ||
117 m_opt == ValueOptions::ManyValues );
118 }
119
121 void setRequired( bool on = true ) override
122 {
123 UNUSED( on )
124 }
125
127 const String & valueSpecifier() const override
128 {
129 return m_valueSpecifier;
130 }
132 void setValueSpecifier( const String & vs )
133 {
134 m_valueSpecifier = vs;
135 }
136
138 const String & description() const override
139 {
140 return m_description;
141 }
143 void setDescription( const String & desc )
144 {
145 m_description = desc;
146 }
147
149 const String & longDescription() const override
150 {
151 if( !m_longDescription.empty() )
152 return m_longDescription;
153 else
154 return m_description;
155 }
157 void setLongDescription( const String & desc )
158 {
159 m_longDescription = desc;
160 }
161
163 const String & value() const
164 {
165 if( !m_values.empty() )
166 return m_values.front();
167 else if( !m_defaultValues.empty() )
168 return m_defaultValues.front();
169 else
171 }
172
174 const StringList & values() const
175 {
176 if( !m_values.empty() )
177 return m_values;
178 else
179 return m_defaultValues;
180 }
181
183 const String & defaultValue() const
184 {
185 if( !m_defaultValues.empty() )
186 return m_defaultValues.front();
187 else
189 }
190
193 void setDefaultValue( const String & v )
194 {
195 m_defaultValues.push_back( v );
196 }
197
199 const StringList & defaultValues() const
200 {
201 return m_defaultValues;
202 }
203
205 void setDefaultValues( const StringList & v )
206 {
207 m_defaultValues = v;
208 }
209
213 const String & nm,
215 StringList & possibleNames ) const override
216 {
217 bool ret = false;
218
219 if( details::isMisspelledName( nm, name() ) )
220 {
221 possibleNames.push_back( name() );
222
223 ret = true;
224 }
225
226 if( GroupIface::isMisspelledName( nm, possibleNames ) )
227 return true;
228 else
229 return ret;
230 }
231
235 const String & nm,
237 StringList & possibleNames ) const
238 {
239 if( details::isMisspelledName( nm, name() ) )
240 {
241 possibleNames.push_back( name() );
242
243 return true;
244 }
245 else
246 return false;
247 }
248
250 void clear() override
251 {
252 m_isDefined = false;
253
254 m_values.clear();
255
257 }
258
259 using GroupIface::addArg;
260
262 void addArg( ArgPtr arg ) override
263 {
264 if( arg->type() == ArgType::Command && m_opt != ValueOptions::NoValue )
265 throw BaseException( String( SL( "Addition of commands to command with "
266 "value is disallowed." ) ) );
267
268 if( std::find( m_children.cbegin(), m_children.cend(), arg ) ==
269 m_children.cend() )
270 {
271 if( cmdLine() )
272 arg->setCmdLine( cmdLine() );
273
274 m_children.push_back( std::move( arg ) );
275 }
276 }
277
278protected:
291 const String & n ) override
292 {
293 if( name() == n )
294 return this;
295 else
296 return nullptr;
297 }
298
314 const String & name )
315 {
316 auto * arg = GroupIface::findArgument( name );
317
318 if( !arg && m_subCommand )
319 return m_subCommand->findChild( name );
320
321 return arg;
322 }
323
331 Context & ctx ) override
332 {
333 m_isDefined = true;
334
335 switch( m_opt )
336 {
338 {
339 eatValues( ctx, m_values,
340 String( SL( "Command \"" ) ) +
341 name() + SL( "\" requires value that wasn't presented." ),
342 cmdLine() );
343 }
344 break;
345
347 {
348 m_values.push_back( eatOneValue( ctx,
349 String( SL( "Command \"" ) ) + name() +
350 SL( "\" requires value that wasn't presented." ),
351 cmdLine() ) );
352 }
353 break;
354
355 default :
356 break;
357 }
358 }
359
368 StringList & flags,
370 StringList & names ) const override
371 {
372 if( details::isCorrectName( name() ) )
373 {
374 auto it = std::find( names.begin(), names.end(), name() );
375
376 if( it != names.end() )
377 throw BaseException( String( SL( "Redefinition of command "
378 "with name \"" ) ) + name() + SL( "\"." ) );
379 else
380 names.push_back( name() );
381 }
382 else
383 throw BaseException( String( SL( "Disallowed name \"" ) ) +
384 name() + SL( "\" for the command." ) );
385
386 StringList ftmp = flags;
387 StringList ntmp = names;
388
390 }
391
393 void checkCorrectnessAfterParsing() const override
394 {
395 if( isDefined() )
397
398 if( isDefined() && m_isSubCommandRequired && !m_subCommand )
399 throw BaseException( String( SL( "Wasn't defined required sub-command of command \"" ) ) +
400 name() + SL( "\"." ) );
401 }
402
405 {
406 m_subCommand = sub;
407 }
408
409private:
411
412
413 ValueOptions m_opt;
415 String m_valueSpecifier;
417 String m_description;
419 String m_longDescription;
421 bool m_isDefined;
423 bool m_isSubCommandRequired;
425 StringList m_values;
427 StringList m_defaultValues;
429 Command * m_subCommand;
430}; // class Command
431
432} /* namespace Args */
433
434#endif // ARGS__COMMAND_HPP__INCLUDED
Interface for arguments.
Definition arg_iface.hpp:51
CmdLine * cmdLine() const
Base exception of the library.
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
ArgIface * findArgument(const String &n) override
Definition command.hpp:286
bool isMisspelledCommand(const String &nm, StringList &possibleNames) const
Definition command.hpp:233
void process(Context &ctx) override
Process argument's staff, for example take values from context.
Definition command.hpp:329
void setDefaultValues(const StringList &v)
Set default values.
Definition command.hpp:205
void setDefaultValue(const String &v)
Definition command.hpp:193
ArgIface * findChild(const String &name)
Definition command.hpp:309
bool isDefined() const override
Definition command.hpp:108
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition command.hpp:366
bool isWithValue() const override
Definition command.hpp:114
ArgType type() const override
Definition command.hpp:102
Command(T &&nm, ValueOptions opt=ValueOptions::NoValue, bool isSubCommandRequired=false)
Definition command.hpp:60
const String & value() const
Definition command.hpp:163
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Definition command.hpp:393
void clear() override
Clear state of the argument.
Definition command.hpp:250
void setDescription(const String &desc)
Set description.
Definition command.hpp:143
const String & longDescription() const override
Definition command.hpp:149
void addArg(ArgPtr arg) override
Add argument.
Definition command.hpp:262
const StringList & values() const
Definition command.hpp:174
const String & description() const override
Definition command.hpp:138
const StringList & defaultValues() const
Definition command.hpp:199
void setRequired(bool on=true) override
Set required flag.
Definition command.hpp:121
const String & defaultValue() const
Definition command.hpp:183
const String & valueSpecifier() const override
Definition command.hpp:127
void setValueSpecifier(const String &vs)
Set value specifier.
Definition command.hpp:132
virtual ~Command()
Definition command.hpp:97
bool isMisspelledName(const String &nm, StringList &possibleNames) const override
Definition command.hpp:211
void setCurrentSubCommand(Command *sub)
Set current subcommand.
Definition command.hpp:404
void setLongDescription(const String &desc)
Set long description.
Definition command.hpp:157
Context is a list of words in the command line that user presented with interface for interacting wit...
Definition context.hpp:60
Interface for the groups.
String name() const override
std::unique_ptr< ArgIface, details::Deleter< ArgIface > > ArgPtr
Smart pointer to the argument.
void clear() override
Clear state of the argument.
Arguments m_children
List of children.
bool isMisspelledName(const String &name, StringList &possibleNames) const override
void addArg(ArgIface &arg)
Add argument.
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
ArgIface * findArgument(const String &name) override
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Help argument.
Definition help.hpp:76
HelpPrinter is a class that prints help.
Definition api.hpp:42
std::string String
String type.
Definition types.hpp:324
std::vector< String > StringList
List of strings.
Definition types.hpp:346
String eatOneValue(Ctx &context, const String &errorDescription, Cmd *cmdLine)
Eat one value.
ArgType
Type of the argument.
Definition enums.hpp:56
@ Command
Command.
ValueOptions
Options for value property.
Definition enums.hpp:41
@ OneValue
One value.
@ ManyValues
Many values.
@ NoValue
No values.
bool eatValues(Ctx &context, Container &container, const String &errorDescription, Cmd *cmdLine)
Eat values in context.
#define SL(str)
Definition types.hpp:338
#define UNUSED(Var)
Macro to supress warning about unused argument.
Definition utils.hpp:60
#define DISABLE_COPY(Class)
Macro for disabling copy.
Definition utils.hpp:50