Debian Codegraph @

application/argumentparser.cpp

raw symbols SCIP
1 #include "./argumentparser.h"
2 #include "./argumentparserprivate.h"
3 #include "./commandlineutils.h"
4
5 #include "../conversion/stringbuilder.h"
6 #include "../conversion/stringconversion.h"
7 #include "../io/ansiescapecodes.h"
8 #include "../io/path.h"
9 #include "../misc/levenshtein.h"
10 #include "../misc/parseerror.h"
11
12 #include <algorithm>
13 #include <cstdlib>
14 #include <cstring>
15 #include <iostream>
16 #include <set>
17 #include <sstream>
18 #include <string>
19
20 #ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
21 #include <filesystem>
22 #endif
23
24 using namespace std;
25 using namespace std::placeholders;
26 using namespace std::literals;
27 using namespace CppUtilities::EscapeCodes;
28
29 /*!
30 * \namespace CppUtilities
31 * \brief Contains all utilities provided by the c++utilities library.
32 */
33 namespace CppUtilities {
34
35 /*!
36 * \brief The ArgumentDenotationType enum specifies the type of a given argument denotation.
37 */
38 enum ArgumentDenotationType : unsigned char {
39 Value = 0, /**< parameter value */
40 Abbreviation = 1, /**< argument abbreviation */
41 FullName = 2 /**< full argument name */
42 };
43
44 /*!
45 * \brief The ArgumentCompletionInfo struct holds information internally used for shell completion and suggestions.
46 */
47 struct ArgumentCompletionInfo {
48 ArgumentCompletionInfo(const ArgumentReader &reader);
49
50 const Argument *const lastDetectedArg;
51 size_t lastDetectedArgIndex = 0;
52 vector<Argument *> lastDetectedArgPath;
53 list<const Argument *> relevantArgs;
54 list<const Argument *> relevantPreDefinedValues;
55 const char *const *lastSpecifiedArg = nullptr;
56 unsigned int lastSpecifiedArgIndex = 0;
57 bool nextArgumentOrValue = false;
58 bool completeFiles = false, completeDirs = false;
59 };
60
61 /*!
62 * \brief Constructs a new completion info for the specified \a reader.
63 * \remarks Only assigns some defaults. Use ArgumentParser::determineCompletionInfo() to populate the struct with actual data.
64 */
65 ArgumentCompletionInfo::ArgumentCompletionInfo(const ArgumentReader &reader)
66 : lastDetectedArg(reader.lastArg)
67 {
68 }
69
70 /// \cond
71 struct ArgumentSuggestion {
72 ArgumentSuggestion(const char *unknownArg, size_t unknownArgSize, const char *suggestion, bool hasDashPrefix);
73 ArgumentSuggestion(const char *unknownArg, size_t unknownArgSize, const char *suggestion, size_t suggestionSize, bool hasDashPrefix);
74 bool operator<(const ArgumentSuggestion &other) const;
75 bool operator==(const ArgumentSuggestion &other) const;
76 void addTo(multiset<ArgumentSuggestion> &suggestions, size_t limit) const;
77
78 const char *const suggestion;
79 const size_t suggestionSize;
80 const size_t editingDistance;
81 const bool hasDashPrefix;
82 };
83
84 ArgumentSuggestion::ArgumentSuggestion(const char *unknownArg, size_t unknownArgSize, const char *suggestion, size_t suggestionSize, bool isOperation)
85 : suggestion(suggestion)
86 , suggestionSize(suggestionSize)
87 , editingDistance(computeDamerauLevenshteinDistance(unknownArg, unknownArgSize, suggestion, suggestionSize))
88 , hasDashPrefix(isOperation)
89 {
90 }
91
92 ArgumentSuggestion::ArgumentSuggestion(const char *unknownArg, size_t unknownArgSize, const char *suggestion, bool isOperation)
93 : ArgumentSuggestion(unknownArg, unknownArgSize, suggestion, strlen(suggestion), isOperation)
94 {
95 }
96
97 bool ArgumentSuggestion::operator<(const ArgumentSuggestion &other) const
98 {
99 return editingDistance < other.editingDistance;
100 }
101
102 void ArgumentSuggestion::addTo(multiset<ArgumentSuggestion> &suggestions, size_t limit) const
103 {
104 if (suggestions.size() >= limit && !(*this < *--suggestions.end())) {
105 return;
106 }
107 suggestions.emplace(*this);
108 while (suggestions.size() > limit) {
109 suggestions.erase(--suggestions.end());
110 }
111 }
112 /// \endcond
113
114 /*!
115 * \class ArgumentReader
116 * \brief The ArgumentReader class internally encapsulates the process of reading command line arguments.
117 * \remarks
118 * - For meaning of parameters see documentation of corresponding member variables.
119 * - Results are stored in specified \a args and assigned sub arguments.
120 * - This class is explicitly *not* part of the public API.
121 */
122
123 /*!
124 * \brief Initializes the internal reader for the specified \a parser and arguments.
125 */
126 ArgumentReader::ArgumentReader(ArgumentParser &parser, const char *const *argv, const char *const *end, bool completionMode)
127 : parser(parser)
128 , args(parser.m_mainArgs)
129 , index(0)
130 , argv(argv)
131 , end(end)
132 , lastArg(nullptr)
133 , argDenotation(nullptr)
134 , completionMode(completionMode)
135 {
136 }
137
138 /*!
139 * \brief Resets the ArgumentReader to continue reading new \a argv.
140 */
141 ArgumentReader &ArgumentReader::reset(const char *const *argv, const char *const *end)
142 {
143 this->argv = argv;
144 this->end = end;
145 index = 0;
146 lastArg = nullptr;
147 argDenotation = nullptr;
148 return *this;
149 }
150
151 /*!
152 * \brief Reads the commands line arguments specified when constructing the object.
153 * \remarks Reads on main-argument-level.
154 */
155 bool ArgumentReader::read()
156 {
157 return read(args);
158 }
159
160 /*!
161 * \brief Returns whether the \a denotation with the specified \a denotationLength matches the argument's \a name.
162 */
163 bool Argument::matchesDenotation(const char *denotation, size_t denotationLength) const
164 {
165 return m_name && !strncmp(m_name, denotation, denotationLength) && *(m_name + denotationLength) == '\0';
166 }
167
168 /*!
169 * \brief Reads the commands line arguments specified when constructing the object.
170 * \remarks The argument definitions to look for are specified via \a args. The method calls itself recursively
171 * to check for nested arguments as well.
172 * \returns Returns true if all arguments have been processed. Returns false on early exit because some argument
173 * is unknown and behavior for this case is set to UnknownArgumentBehavior::Fail.
174 */
175 bool ArgumentReader::read(ArgumentVector &args)
176 {
177 // method is called recursively for sub args to the last argument (which is nullptr in the initial call) is the current parent argument
178 Argument *const parentArg = lastArg;
179 // determine the current path
180 const vector<Argument *> &parentPath = parentArg ? parentArg->path(parentArg->occurrences() - 1) : vector<Argument *>();
181
182 Argument *lastArgInLevel = nullptr;
183 vector<const char *> *values = nullptr;
184
185 // iterate through all argument denotations; loop might exit earlier when a denotation is unknown
186 while (argv != end) {
187 // check whether there are still values to read
188 if (values && ((lastArgInLevel->requiredValueCount() != Argument::varValueCount) || (lastArgInLevel->flags() & Argument::Flags::Greedy))
189 && values->size() < lastArgInLevel->requiredValueCount()) {
190 // read arg as value and continue with next arg
191 values->emplace_back(argDenotation ? argDenotation : *argv);
192 ++index;
193 ++argv;
194 argDenotation = nullptr;
195 continue;
196 }
197
198 // determine how denotation must be processed
199 bool abbreviationFound = false;
200 if (argDenotation) {
201 // continue reading children for abbreviation denotation already detected
202 abbreviationFound = false;
203 argDenotationType = Abbreviation;
204 } else {
205 // determine denotation type
206 argDenotation = *argv;
207 if (!*argDenotation && (!lastArgInLevel || values->size() >= lastArgInLevel->requiredValueCount())) {
208 // skip empty arguments
209 ++index;
210 ++argv;
211 argDenotation = nullptr;
212 continue;
213 }
214 abbreviationFound = false;
215 argDenotationType = Value;
216 if (*argDenotation == '-') {
217 ++argDenotation;
218 ++argDenotationType;
219 if (*argDenotation == '-') {
220 ++argDenotation;
221 ++argDenotationType;
222 }
223 }
224 }
225
226 // try to find matching Argument instance
227 Argument *matchingArg = nullptr;
228 if (argDenotationType != Value) {
229 // determine actual denotation length (everything before equation sign)
230 const char *const equationPos = strchr(argDenotation, '=');
231 const auto argDenotationLength = equationPos ? static_cast<size_t>(equationPos - argDenotation) : strlen(argDenotation);
232
233 // loop through each "part" of the denotation
234 // names are read at once, but for abbreviations each character is considered individually
235 for (; argDenotationLength; matchingArg = nullptr) {
236 // search for arguments by abbreviation or name depending on the previously determined denotation type
237 if (argDenotationType == Abbreviation) {
238 for (Argument *const arg : args) {
239 if (arg->abbreviation() && arg->abbreviation() == *argDenotation) {
240 matchingArg = arg;
241 abbreviationFound = true;
242 break;
243 }
244 }
245 } else {
246 for (Argument *const arg : args) {
247 if (arg->matchesDenotation(argDenotation, argDenotationLength)) {
248 matchingArg = arg;
249 break;
250 }
251 }
252 }
253 if (!matchingArg) {
254 break;
255 }
256
257 // an argument matched the specified denotation so add an occurrence
258 matchingArg->m_occurrences.emplace_back(index, parentPath, parentArg);
259
260 // prepare reading parameter values
261 values = &matchingArg->m_occurrences.back().values;
262
263 // read value after equation sign
264 if ((argDenotationType != Abbreviation && equationPos) || (++argDenotation == equationPos)) {
265 values->push_back(equationPos + 1);
266 argDenotation = nullptr;
267 }
268
269 // read sub arguments, distinguish whether further abbreviations follow
270 ++index;
271 ++parser.m_actualArgc;
272 lastArg = lastArgInLevel = matchingArg;
273 lastArgDenotation = argv;
274 if (argDenotationType != Abbreviation || !argDenotation || !*argDenotation) {
275 // no further abbreviations follow -> read sub args for next argv
276 ++argv;
277 argDenotation = nullptr;
278 read(lastArg->m_subArgs);
279 argDenotation = nullptr;
280 break;
281 } else {
282 // further abbreviations follow -> remember current arg value
283 const char *const *const currentArgValue = argv;
284 // don't increment argv, keep processing outstanding chars of argDenotation
285 read(lastArg->m_subArgs);
286 // stop further processing if the denotation has been consumed or even the next value has already been loaded
287 if (!argDenotation || currentArgValue != argv) {
288 argDenotation = nullptr;
289 break;
290 }
291 }
292 }
293
294 // continue with next arg if we've got a match already
295 if (matchingArg) {
296 continue;
297 }
298
299 // unknown argument might be a sibling of the parent element
300 for (auto parentArgument = parentPath.crbegin(), pathEnd = parentPath.crend();; ++parentArgument) {
301 for (Argument *const sibling : (parentArgument != pathEnd ? (*parentArgument)->subArguments() : parser.m_mainArgs)) {
302 if (sibling->occurrences() < sibling->maxOccurrences()) {
303 // check whether the denoted abbreviation matches the sibling's abbreviatiopn
304 if (argDenotationType == Abbreviation && (sibling->abbreviation() && sibling->abbreviation() == *argDenotation)) {
305 return false;
306 }
307 // check whether the denoted name matches the sibling's name
308 if (sibling->matchesDenotation(argDenotation, argDenotationLength)) {
309 return false;
310 }
311 }
312 }
313 if (parentArgument == pathEnd) {
314 break;
315 }
316 }
317 }
318
319 // unknown argument might just be a parameter value of the last argument
320 if (lastArgInLevel && values->size() < lastArgInLevel->requiredValueCount()) {
321 values->emplace_back(abbreviationFound ? argDenotation : *argv);
322 ++index;
323 ++argv;
324 argDenotation = nullptr;
325 continue;
326 }
327
328 // first value might denote "operation"
329 for (Argument *const arg : args) {
330 if (arg->denotesOperation() && arg->name() && !strcmp(arg->name(), *argv)) {
331 (matchingArg = arg)->m_occurrences.emplace_back(index, parentPath, parentArg);
332 lastArgDenotation = argv;
333 ++index;
334 ++argv;
335 break;
336 }
337 }
338
339 // use the first default argument which is not already present if there is still no match
340 if (!matchingArg && (!completionMode || (argv + 1 != end))) {
341 const bool uncombinableMainArgPresent = parentArg ? false : parser.isUncombinableMainArgPresent();
342 for (Argument *const arg : args) {
343 if (arg->isImplicit() && !arg->isPresent() && !arg->wouldConflictWithArgument()
344 && (!uncombinableMainArgPresent || !arg->isMainArgument())) {
345 (matchingArg = arg)->m_occurrences.emplace_back(index, parentPath, parentArg);
346 break;
347 }
348 }
349 }
350
351 if (matchingArg) {
352 // an argument matched the specified denotation
353 if (lastArgInLevel == matchingArg) {
354 break; // break required? -> TODO: add test for this condition
355 }
356
357 // prepare reading parameter values
358 values = &matchingArg->m_occurrences.back().values;
359
360 // read sub arguments
361 ++parser.m_actualArgc;
362 lastArg = lastArgInLevel = matchingArg;
363 argDenotation = nullptr;
364 if ((values->size() < matchingArg->requiredValueCount()) && (matchingArg->flags() & Argument::Flags::Greedy)) {
365 continue;
366 }
367 read(lastArg->m_subArgs);
368 argDenotation = nullptr;
369 continue;
370 }
371
372 // argument denotation is unknown -> handle error
373 if (parentArg) {
374 // continue with parent level
375 return false;
376 }
377 if (completionMode) {
378 // ignore unknown denotation
379 ++index;
380 ++argv;
381 argDenotation = nullptr;
382 } else {
383 switch (parser.m_unknownArgBehavior) {
384 case UnknownArgumentBehavior::Warn:
385 cerr << Phrases::Warning << "The specified argument \"" << *argv << "\" is unknown and will be ignored." << Phrases::EndFlush;
386 [[fallthrough]];
387 case UnknownArgumentBehavior::Ignore:
388 // ignore unknown denotation
389 ++index;
390 ++argv;
391 argDenotation = nullptr;
392 break;
393 case UnknownArgumentBehavior::Fail:
394 return false;
395 }
396 }
397 } // while(argv != end)
398 return true;
399 }
400
401 /*!
402 * \class Wrapper
403 * \brief The Wrapper class is internally used print text which might needs to be wrapped preserving the indentation.
404 * \remarks This class is explicitly *not* part of the public API.
405 */
406
407 ostream &operator<<(ostream &os, const Wrapper &wrapper)
408 {
409 // determine max. number of columns
410 static const TerminalSize termSize(determineTerminalSize());
411 const auto maxColumns = termSize.columns ? termSize.columns : numeric_limits<unsigned short>::max();
412
413 // print wrapped string considering indentation
414 unsigned short currentCol = wrapper.m_indentation.level;
415 for (const char *currentChar = wrapper.m_str; *currentChar; ++currentChar) {
416 const bool wrappingRequired = currentCol >= maxColumns;
417 if (wrappingRequired || *currentChar == '\n') {
418 // insert newline (TODO: wrap only at end of a word)
419 os << '\n';
420 // print indentation (if enough space)
421 if (wrapper.m_indentation.level < maxColumns) {
422 os << wrapper.m_indentation;
423 currentCol = wrapper.m_indentation.level;
424 } else {
425 currentCol = 0;
426 }
427 }
428 if (*currentChar != '\n' && (!wrappingRequired || *currentChar != ' ')) {
429 os << *currentChar;
430 ++currentCol;
431 }
432 }
433 return os;
434 }
435
436 CPP_UTILITIES_EXPORT ApplicationInfo applicationInfo;
437
438 /// \cond
439
440 inline bool notEmpty(const char *str)
441 {
442 return str && *str;
443 }
444
445 /// \endcond
446
447 /*!
448 * \class Argument
449 * \brief The Argument class is a wrapper for command line argument information.
450 *
451 * Instances of the Argument class are used as definition when parsing command line
452 * arguments. Arguments can be assigned to an ArgumentParser using
453 * ArgumentParser::setMainArguments() and to another Argument instance using
454 * Argument::setSubArguments().
455 */
456
457 /*!
458 * \brief Constructs an Argument with the given \a name, \a abbreviation and \a description.
459 *
460 * The \a name and the abbreviation mustn't contain any whitespaces.
461 * The \a name mustn't be empty. The \a abbreviation and the \a description might be empty.
462 */
463 Argument::Argument(const char *name, char abbreviation, const char *description, const char *example)
464 : m_name(name)
465 , m_abbreviation(abbreviation)
466 , m_environmentVar(nullptr)
467 , m_description(description)
468 , m_example(example)
469 , m_minOccurrences(0)
470 , m_maxOccurrences(1)
471 , m_requiredValueCount(0)
472 , m_flags(Flags::None)
473 , m_deprecatedBy(nullptr)
474 , m_isMainArg(false)
475 , m_valueCompletionBehavior(ValueCompletionBehavior::PreDefinedValues | ValueCompletionBehavior::Files | ValueCompletionBehavior::Directories
476 | ValueCompletionBehavior::FileSystemIfNoPreDefinedValues)
477 , m_preDefinedCompletionValues(nullptr)
478 {
479 }
480
481 /*!
482 * \brief Destroys the Argument.
483 */
484 Argument::~Argument()
485 {
486 }
487
488 /*!
489 * \brief Returns the first parameter value of the first occurrence of the argument.
490 * \remarks
491 * - If the argument is not present and the an environment variable has been set
492 * using setEnvironmentVariable() the value of the specified variable will be returned.
493 * - Returns nullptr if no value is available though.
494 */
495 const char *Argument::firstValue() const
496 {
497 if (!m_occurrences.empty() && !m_occurrences.front().values.empty()) {
498 return m_occurrences.front().values.front();
499 } else if (m_environmentVar) {
500 return getenv(m_environmentVar);
501 } else {
502 return nullptr;
503 }
504 }
505
506 /*!
507 * \brief Returns the first value like Argument::firstValue() but returns \a fallback instead of nullptr if there's no value.
508 */
509 const char *Argument::firstValueOr(const char *fallback) const
510 {
511 if (const auto *const v = firstValue()) {
512 return v;
513 } else {
514 return fallback;
515 }
516 }
517
518 /*!
519 * \brief Writes the name, the abbreviation and other information about the Argument to the give ostream.
520 */
521 void Argument::printInfo(ostream &os, unsigned char indentation) const
522 {
523 if (isDeprecated()) {
524 return;
525 }
526 Indentation ident(indentation);
527 os << ident;
528 EscapeCodes::setStyle(os, EscapeCodes::TextAttribute::Bold);
529 if (notEmpty(name())) {
530 if (!denotesOperation()) {
531 os << '-' << '-';
532 }
533 os << name();
534 }
535 if (notEmpty(name()) && abbreviation()) {
536 os << ',' << ' ';
537 }
538 if (abbreviation()) {
539 os << '-' << abbreviation();
540 }
541 EscapeCodes::setStyle(os);
542 if (requiredValueCount()) {
543 unsigned int valueNamesPrint = 0;
544 for (auto i = valueNames().cbegin(), end = valueNames().cend(); i != end && valueNamesPrint < requiredValueCount(); ++i) {
545 os << ' ' << '[' << *i << ']';
546 ++valueNamesPrint;
547 }
548 if (requiredValueCount() == Argument::varValueCount) {
549 os << " ...";
550 } else {
551 for (; valueNamesPrint < requiredValueCount(); ++valueNamesPrint) {
552 os << " [value " << (valueNamesPrint + 1) << ']';
553 }
554 }
555 }
556 ident.level += 2;
557 if (notEmpty(description())) {
558 os << '\n' << ident << Wrapper(description(), ident);
559 }
560 if (isRequired()) {
561 os << '\n' << ident << "particularities: mandatory";
562 if (!isMainArgument()) {
563 os << " if parent argument is present";
564 }
565 }
566 if (environmentVariable()) {
567 os << '\n' << ident << "default environment variable: " << Wrapper(environmentVariable(), ident + 30);
568 }
569 os << '\n';
570 bool hasSubArgs = false;
571 for (const auto *const arg : subArguments()) {
572 if (arg->isDeprecated()) {
573 continue;
574 }
575 hasSubArgs = true;
576 arg->printInfo(os, ident.level);
577 }
578 if (notEmpty(example())) {
579 if (ident.level == 2 && hasSubArgs) {
580 os << '\n';
581 }
582 os << ident << "example: " << Wrapper(example(), ident + 9);
583 os << '\n';
584 }
585 }
586
587 /*!
588 * \brief This function return the first present and uncombinable argument of the given list of arguments.
589 *
590 * The Argument \a except will be ignored.
591 */
592 Argument *firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except)
593 {
594 for (Argument *arg : args) {
595 if (arg != except && arg->isPresent() && !arg->isCombinable()) {
596 return arg;
597 }
598 }
599 return nullptr;
600 }
601
602 /*!
603 * \brief Sets the secondary arguments for this argument.
604 *
605 * The given arguments will be considered as secondary arguments of this argument by the argument parser.
606 * This means that the parser will complain if these arguments are given, but not this argument.
607 * If secondary arguments are labeled as mandatory their parent is also mandatory.
608 *
609 * The Argument does not take ownership. Do not destroy the given arguments as long as they are
610 * used as secondary arguments.
611 *
612 * \sa addSubArguments()
613 * \sa secondaryArguments()
614 * \sa addSecondaryArgument()
615 * \sa hasSubArguments()
616 */
617 void Argument::setSubArguments(const ArgumentInitializerList &subArguments)
618 {
619 // remove this argument from the parents list of the previous secondary arguments
620 for (Argument *const arg : m_subArgs) {
621 arg->m_parents.erase(remove(arg->m_parents.begin(), arg->m_parents.end(), this), arg->m_parents.end());
622 }
623 // clear currently assigned args before adding new ones
624 m_subArgs.clear();
625 addSubArguments(subArguments);
626 }
627
628 /*!
629 * \brief Sets the secondary arguments for this argument.
630 *
631 * The given arguments will be considered as secondary arguments of this argument by the argument parser.
632 * This means that the parser will complain if these arguments are given, but not this argument.
633 * If secondary arguments are labeled as mandatory their parent is also mandatory.
634 *
635 * The Argument does not take ownership. Do not destroy the given arguments as long as they are
636 * used as secondary arguments.
637 *
638 * \sa setSubArguments()
639 * \sa secondaryArguments()
640 * \sa addSecondaryArgument()
641 * \sa hasSubArguments()
642 */
643 void Argument::addSubArguments(const ArgumentInitializerList &subArguments)
644 {
645 // append secondary arguments
646 const auto requiredCap = m_subArgs.size() + subArguments.size();
647 if (requiredCap < m_subArgs.capacity()) {
648 m_subArgs.reserve(requiredCap); // does insert this for us?
649 }
650 m_subArgs.insert(m_subArgs.end(), subArguments.begin(), subArguments.end());
651 // add this argument to the parents list of the assigned secondary arguments and set the parser
652 for (Argument *const arg : subArguments) {
653 if (find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) {
654 arg->m_parents.push_back(this);
655 }
656 }
657 }
658
659 /*!
660 * \brief Adds \a arg as a secondary argument for this argument.
661 *
662 * \sa secondaryArguments()
663 * \sa setSubArguments()
664 * \sa hasSubArguments()
665 */
666 void Argument::addSubArgument(Argument *arg)
667 {
668 if (find(m_subArgs.cbegin(), m_subArgs.cend(), arg) != m_subArgs.cend()) {
669 return;
670 }
671 m_subArgs.push_back(arg);
672 if (find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) {
673 arg->m_parents.push_back(this);
674 }
675 }
676
677 /*!
678 * \brief Returns whether at least one parent argument is present.
679 * \remarks Returns always true for main arguments.
680 */
681 bool Argument::isParentPresent() const
682 {
683 if (isMainArgument()) {
684 return true;
685 }
686 for (const Argument *parent : m_parents) {
687 if (parent->isPresent()) {
688 return true;
689 }
690 }
691 return false;
692 }
693
694 /*!
695 * \brief Checks if this arguments conflicts with other arguments.
696 *
697 * If the argument is in conflict with an other argument this argument will be returned.
698 * Otherwise nullptr will be returned.
699 *
700 * \remarks Conflicts with main arguments aren't considered by this method!
701 */
702 Argument *Argument::conflictsWithArgument() const
703 {
704 return isPresent() ? wouldConflictWithArgument() : nullptr;
705 }
706
707 /*!
708 * \brief Checks if this argument would conflict with other arguments if it was present.
709 *
710 * If the argument is in conflict with an other argument this argument will be returned.
711 * Otherwise nullptr will be returned.
712 *
713 * \remarks Conflicts with main arguments aren't considered by this method!
714 */
715 Argument *Argument::wouldConflictWithArgument() const
716 {
717 if (isCombinable()) {
718 return nullptr;
719 }
720 for (Argument *parent : m_parents) {
721 for (Argument *sibling : parent->subArguments()) {
722 if (sibling != this && sibling->isPresent() && !sibling->isCombinable()) {
723 return sibling;
724 }
725 }
726 }
727 return nullptr;
728 }
729
730 /*!
731 * \brief Returns the first operation argument specified by the user or nullptr if no operation has been specified.
732 * \remarks Only direct sub arguments of this argument are considered.
733 */
734 Argument *Argument::specifiedOperation() const
735 {
736 for (Argument *arg : m_subArgs) {
737 if (arg->denotesOperation() && arg->isPresent()) {
738 return arg;
739 }
740 }
741 return nullptr;
742 }
743
744 /*!
745 * \brief Resets this argument and all sub arguments recursively.
746 * \sa Argument::reset()
747 */
748 void Argument::resetRecursively()
749 {
750 for (Argument *arg : m_subArgs) {
751 arg->resetRecursively();
752 }
753 reset();
754 }
755
756 /*!
757 * \class ArgumentParser
758 * \brief The ArgumentParser class provides a means for handling command line arguments.
759 *
760 * To setup the parser create instances of ApplicationUtilities::Argument to define a
761 * set of known arguments and assign these to the parser using setMainArguments().
762 *
763 * To invoke parsing call parseArgs(). The parser will verify the previously
764 * assigned definitions (and might throw std::invalid_argument) and then parse the
765 * given command line arguments according the definitions (and might throw
766 * CppUtilities::Failure).
767 */
768
769 /*!
770 * \brief Constructs a new ArgumentParser.
771 */
772 ArgumentParser::ArgumentParser()
773 : m_actualArgc(0)
774 , m_executable(nullptr)
775 , m_unknownArgBehavior(UnknownArgumentBehavior::Fail)
776 , m_defaultArg(nullptr)
777 , m_helpArg(*this)
778 {
779 }
780
781 /*!
782 * \brief Sets the main arguments for the parser. The parser will use these argument definitions
783 * to when parsing the command line arguments and when printing help information.
784 * \remarks
785 * - The parser does not take ownership. Do not destroy the arguments as long as they are used as
786 * main arguments.
787 * - Sets the first specified argument as default argument if none has been assigned yet and the
788 * first argument does not require any values or has no mandatory sub arguments.
789 */
790 void ArgumentParser::setMainArguments(const ArgumentInitializerList &mainArguments)
791 {
792 if (!mainArguments.size()) {
793 m_mainArgs.clear();
794 return;
795 }
796 for (Argument *arg : mainArguments) {
797 arg->m_isMainArg = true;
798 }
799 m_mainArgs.assign(mainArguments);
800 if (m_defaultArg || (*mainArguments.begin())->requiredValueCount()) {
801 return;
802 }
803 bool subArgsRequired = false;
804 for (const Argument *subArg : (*mainArguments.begin())->subArguments()) {
805 if (subArg->isRequired()) {
806 subArgsRequired = true;
807 break;
808 }
809 }
810 if (!subArgsRequired) {
811 m_defaultArg = *mainArguments.begin();
812 }
813 }
814
815 /*!
816 * \brief Adds the specified \a argument to the main argument.
817 * \remarks
818 * The parser does not take ownership. Do not destroy the argument as long as it is used as
819 * main argument.
820 */
821 void ArgumentParser::addMainArgument(Argument *argument)
822 {
823 argument->m_isMainArg = true;
824 m_mainArgs.push_back(argument);
825 }
826
827 /*!
828 * \brief Prints help text for all assigned arguments.
829 */
830 void ArgumentParser::printHelp(ostream &os) const
831 {
832 EscapeCodes::setStyle(os, EscapeCodes::TextAttribute::Bold);
833 bool wroteLine = false;
834 if (applicationInfo.name && *applicationInfo.name) {
835 os << applicationInfo.name;
836 if (applicationInfo.version && *applicationInfo.version) {
837 os << ',' << ' ';
838 }
839 wroteLine = true;
840 }
841 if (applicationInfo.version && *applicationInfo.version) {
842 os << "version " << applicationInfo.version;
843 wroteLine = true;
844 }
845 if (wroteLine) {
846 os << '\n' << '\n';
847 }
848 EscapeCodes::setStyle(os);
849
850 if (applicationInfo.description && *applicationInfo.description) {
851 os << applicationInfo.description;
852 wroteLine = true;
853 }
854 if (wroteLine) {
855 os << '\n' << '\n';
856 }
857
858 if (!m_mainArgs.empty()) {
859 bool hasOperations = false, hasTopLevelOptions = false;
860 for (const Argument *const arg : m_mainArgs) {
861 if (arg->denotesOperation()) {
862 hasOperations = true;
863 } else if (strcmp(arg->name(), "help")) {
864 hasTopLevelOptions = true;
865 }
866 if (hasOperations && hasTopLevelOptions) {
867 break;
868 }
869 }
870
871 // check whether operations are available
872 if (hasOperations) {
873 // split top-level operations and other configurations
874 os << "Available operations:";
875 for (const Argument *const arg : m_mainArgs) {
876 if (!arg->denotesOperation() || arg->isDeprecated() || !strcmp(arg->name(), "help")) {
877 continue;
878 }
879 os << '\n';
880 arg->printInfo(os);
881 }
882 if (hasTopLevelOptions) {
883 os << "\nAvailable top-level options:";
884 for (const Argument *const arg : m_mainArgs) {
885 if (arg->denotesOperation() || arg->isDeprecated() || !strcmp(arg->name(), "help")) {
886 continue;
887 }
888 os << '\n';
889 arg->printInfo(os);
890 }
891 }
892 } else {
893 // just show all args if no operations are available
894 os << "Available arguments:";
895 for (const Argument *const arg : m_mainArgs) {
896 if (arg->isDeprecated() || !strcmp(arg->name(), "help")) {
897 continue;
898 }
899 os << '\n';
900 arg->printInfo(os);
901 }
902 }
903 }
904
905 if (!applicationInfo.dependencyVersions.empty()) {
906 os << '\n';
907 auto i = applicationInfo.dependencyVersions.begin(), end = applicationInfo.dependencyVersions.end();
908 os << "Linked against: " << *i;
909 for (++i; i != end; ++i) {
910 os << ',' << ' ' << *i;
911 }
912 os << '\n';
913 }
914
915 if (applicationInfo.url && *applicationInfo.url) {
916 os << "\nProject website: " << applicationInfo.url << endl;
917 }
918 }
919
920 /*!
921 * \brief Parses the specified command line arguments.
922 *
923 * The behavior is configurable by specifying the \a behavior argument. See ParseArgumentBehavior for
924 * the options. By default, all options are present.
925 *
926 * \remarks
927 * - The results are stored in the Argument instances assigned as main arguments and sub arguments.
928 * - This method will not return in the error case if the ParseArgumentBehavior::ExitOnFailure is present
929 * (default).
930 * - This method will not return in case shell completion is requested. This behavior can be altered
931 * by overriding the exit function via ArgumentParser::setExitFunction() which defaults to &std::exit.
932 * \throws Throws Failure if the specified arguments are invalid and the ParseArgumentBehavior::ExitOnFailure
933 * flag is *not* present.
934 * \sa readArgs()
935 */
936 void ArgumentParser::parseArgs(int argc, const char *const *argv, ParseArgumentBehavior behavior)
937 {
938 try {
939 readArgs(argc, argv);
940 if (!argc) {
941 return;
942 }
943 if (behavior & ParseArgumentBehavior::CheckConstraints) {
944 checkConstraints(m_mainArgs);
945 }
946 if (behavior & ParseArgumentBehavior::InvokeCallbacks) {
947 invokeCallbacks(m_mainArgs);
948 }
949 } catch (const ParseError &failure) {
950 if (behavior & ParseArgumentBehavior::ExitOnFailure) {
951 CMD_UTILS_START_CONSOLE;
952 cerr << failure;
953 invokeExit(EXIT_FAILURE);
954 }
955 throw;
956 }
957 }
958
959 /*!
960 * \brief Parses the specified command line arguments.
961 * \remarks
962 * - The results are stored in the Argument instances assigned as main arguments and sub arguments.
963 * - In contrast to parseArgs() this method does not check whether constraints are violated and it
964 * does not call any callbacks.
965 * - This method will not return in case shell completion is requested. This behavior can be altered
966 * by overriding the exit function via ArgumentParser::setExitFunction() which defaults to &std::exit.
967 * \throws Throws Failure if the specified arguments are invalid.
968 * \sa parseArgs()
969 * \deprecated In next major release, this method will be private. parseArgs() can serve the same
970 * purpose then.
971 */
972 void ArgumentParser::readArgs(int argc, const char *const *argv)
973 {
974 CPP_UTILITIES_IF_DEBUG_BUILD(verifyArgs(m_mainArgs);)
975 m_actualArgc = 0;
976
977 // the first argument is the executable name
978 if (!argc) {
979 m_executable = nullptr;
980 return;
981 }
982 m_executable = *argv;
983
984 // check for further arguments
985 if (!--argc) {
986 // no arguments specified -> flag default argument as present if one is assigned
987 assumeDefaultArgument();
988 return;
989 }
990
991 // check for completion mode: if first arg (after executable name) is "--bash-completion-for", bash completion for the following arguments is requested
992 const bool completionMode = !strcmp(*++argv, "--bash-completion-for");
993
994 // determine the index of the current word for completion and the number of arguments to be passed to ArgumentReader
995 unsigned int currentWordIndex = 0, argcForReader;
996 if (completionMode) {
997 // the first argument after "--bash-completion-for" is the index of the current word
998 try {
999 currentWordIndex = (--argc ? stringToNumber<unsigned int, string>(*(++argv)) : 0);
1000 if (argc) {
1001 ++argv;
1002 --argc;
1003 }
1004 } catch (const ConversionException &) {
1005 currentWordIndex = static_cast<unsigned int>(argc - 1);
1006 }
1007 argcForReader = min(static_cast<unsigned int>(argc), currentWordIndex + 1);
1008 } else {
1009 argcForReader = static_cast<unsigned int>(argc);
1010 }
1011
1012 // read specified arguments
1013 ArgumentReader reader(*this, argv, argv + argcForReader, completionMode);
1014 const bool allArgsProcessed(reader.read());
1015 m_noColorArg.apply();
1016
1017 // fail when not all arguments could be processed, except when in completion mode
1018 if (!completionMode && !allArgsProcessed) {
1019 const auto suggestions(findSuggestions(argc, argv, static_cast<unsigned int>(argc - 1), reader));
1020 throw ParseError(argsToString("The specified argument \"", *reader.argv, "\" is unknown.", suggestions));
1021 }
1022
1023 // print Bash completion and prevent the application to continue with the regular execution
1024 if (completionMode) {
1025 printBashCompletion(argc, argv, currentWordIndex, reader);
1026 invokeExit(EXIT_SUCCESS);
1027 }
1028 }
1029
1030 /*!
1031 * \brief Resets all Argument instances assigned as mainArguments() and sub arguments.
1032 * \sa Argument::reset()
1033 */
1034 void ArgumentParser::resetArgs()
1035 {
1036 for (Argument *arg : m_mainArgs) {
1037 arg->resetRecursively();
1038 }
1039 m_actualArgc = 0;
1040 }
1041
1042 /*!
1043 * \brief Assumes the default argument is present (if not already present).
1044 * \remarks
1045 * - Adds an occurrence to the default argument so this argument is assumed to be present (even though
1046 * it is not actually present).
1047 * - This function is automatically called if there are no arguments present at all.
1048 * - This function may be called manually if no actually relevant arguments are present, e.g. if only
1049 * global flags are present but no main operation argument. Note that for this concrete example there
1050 * is also ensureDefaultOperation() (which will call assumeDefaultArgument() internally).
1051 */
1052 void ArgumentParser::assumeDefaultArgument()
1053 {
1054 if (m_defaultArg && m_defaultArg->m_occurrences.empty()) {
1055 m_defaultArg->m_occurrences.emplace_back(0);
1056 }
1057 }
1058
1059 /*!
1060 * \brief Ensures a main operation argument is present.
1061 * \remarks
1062 * - Assumes the default argument is present if no other main operation argument or the help argument
1063 * is present.
1064 * - Requires the default argument to be an operation argument.
1065 */
1066 void ArgumentParser::ensureDefaultOperation()
1067 {
1068 if (!m_defaultArg || m_defaultArg->isPresent() || !m_defaultArg->denotesOperation()) {
1069 return;
1070 }
1071 for (auto *const arg : m_mainArgs) {
1072 if (arg->isPresent() && (arg->denotesOperation() || arg == &m_helpArg)) {
1073 return;
1074 }
1075 }
1076 assumeDefaultArgument();
1077 }
1078
1079 /*!
1080 * \brief Returns the first operation argument specified by the user or nullptr if no operation has been specified.
1081 * \remarks Only main arguments are considered. See Argument::specifiedOperation() to check sub arguments of a specific
1082 * argument.
1083 */
1084 Argument *ArgumentParser::specifiedOperation() const
1085 {
1086 for (Argument *arg : m_mainArgs) {
1087 if (arg->denotesOperation() && arg->isPresent()) {
1088 return arg;
1089 }
1090 }
1091 return nullptr;
1092 }
1093
1094 /*!
1095 * \brief Checks whether at least one uncombinable main argument is present.
1096 */
1097 bool ArgumentParser::isUncombinableMainArgPresent() const
1098 {
1099 for (const Argument *arg : m_mainArgs) {
1100 if (!arg->isCombinable() && arg->isPresent()) {
1101 return true;
1102 }
1103 }
1104 return false;
1105 }
1106
1107 #ifdef CPP_UTILITIES_DEBUG_BUILD
1108 /*!
1109 * \brief Verifies the specified \a argument definitions.
1110 *
1111 * Asserts that
1112 * - The same argument has not been added twice to the same parent.
1113 * - Only one argument within a parent is default or implicit.
1114 * - Only main arguments denote operations.
1115 * - Argument abbreviations are unique within the same level.
1116 * - Argument names are unique within within the same level.
1117 *
1118 * \remarks
1119 * - Verifies the sub arguments, too.
1120 * - For debugging purposes only; hence only used in debug builds.
1121 */
1122 void ArgumentParser::verifyArgs(const ArgumentVector &args)
1123 {
1124 vector<const Argument *> verifiedArgs;
1125 verifiedArgs.reserve(args.size());
1126 vector<char> abbreviations;
1127 abbreviations.reserve(abbreviations.size() + args.size());
1128 vector<const char *> names;
1129 names.reserve(names.size() + args.size());
1130 bool hasImplicit = false;
1131 for (const Argument *arg : args) {
1132 assert(find(verifiedArgs.cbegin(), verifiedArgs.cend(), arg) == verifiedArgs.cend());
1133 verifiedArgs.push_back(arg);
1134 assert(!arg->isImplicit() || !hasImplicit);
1135 hasImplicit |= arg->isImplicit();
1136 assert(!arg->abbreviation() || find(abbreviations.cbegin(), abbreviations.cend(), arg->abbreviation()) == abbreviations.cend());
1137 abbreviations.push_back(arg->abbreviation());
1138 assert(!arg->name() || find_if(names.cbegin(), names.cend(), [arg](const char *name) { return !strcmp(arg->name(), name); }) == names.cend());
1139 assert(arg->requiredValueCount() == 0 || arg->subArguments().size() == 0 || (arg->flags() & Argument::Flags::Greedy));
1140 names.emplace_back(arg->name());
1141 }
1142 for (const Argument *arg : args) {
1143 verifyArgs(arg->subArguments());
1144 }
1145 }
1146 #endif
1147
1148 /*!
1149 * \brief Returns whether \a arg1 should be listed before \a arg2 when
1150 * printing completion.
1151 *
1152 * Arguments are sorted by name (ascending order). However, all arguments
1153 * denoting an operation are listed before all other arguments.
1154 */
1155 bool compareArgs(const Argument *arg1, const Argument *arg2)
1156 {
1157 if (arg1->denotesOperation() && !arg2->denotesOperation()) {
1158 return true;
1159 } else if (!arg1->denotesOperation() && arg2->denotesOperation()) {
1160 return false;
1161 } else {
1162 return strcmp(arg1->name(), arg2->name()) < 0;
1163 }
1164 }
1165
1166 /*!
1167 * \brief Inserts the specified \a siblings in the \a target list.
1168 * \remarks Only inserts siblings which could still occur at least once more.
1169 */
1170 void insertSiblings(const ArgumentVector &siblings, list<const Argument *> &target)
1171 {
1172 bool onlyCombinable = false;
1173 for (const Argument *sibling : siblings) {
1174 if (sibling->isPresent() && !sibling->isCombinable()) {
1175 onlyCombinable = true;
1176 break;
1177 }
1178 }
1179 for (const Argument *sibling : siblings) {
1180 if ((!onlyCombinable || sibling->isCombinable()) && sibling->occurrences() < sibling->maxOccurrences()) {
1181 target.push_back(sibling);
1182 }
1183 }
1184 }
1185
1186 /*!
1187 * \brief Determines arguments relevant for Bash completion or suggestions in case of typo.
1188 */
1189 ArgumentCompletionInfo ArgumentParser::determineCompletionInfo(
1190 int argc, const char *const *argv, unsigned int currentWordIndex, const ArgumentReader &reader) const
1191 {
1192 ArgumentCompletionInfo completion(reader);
1193
1194 // determine last detected arg
1195 if (completion.lastDetectedArg) {
1196 completion.lastDetectedArgIndex = static_cast<size_t>(reader.lastArgDenotation - argv);
1197 completion.lastDetectedArgPath = completion.lastDetectedArg->path(completion.lastDetectedArg->occurrences() - 1);
1198 }
1199
1200 // determine last arg, omitting trailing empty args
1201 if (argc) {
1202 completion.lastSpecifiedArgIndex = static_cast<unsigned int>(argc) - 1;
1203 completion.lastSpecifiedArg = argv + completion.lastSpecifiedArgIndex;
1204 for (; completion.lastSpecifiedArg >= argv && **completion.lastSpecifiedArg == '\0';
1205 --completion.lastSpecifiedArg, --completion.lastSpecifiedArgIndex)
1206 ;
1207 }
1208
1209 // just return main arguments if no args detected
1210 if (!completion.lastDetectedArg || !completion.lastDetectedArg->isPresent()) {
1211 completion.nextArgumentOrValue = true;
1212 insertSiblings(m_mainArgs, completion.relevantArgs);
1213 completion.relevantArgs.sort(compareArgs);
1214 return completion;
1215 }
1216
1217 completion.nextArgumentOrValue = currentWordIndex > completion.lastDetectedArgIndex;
1218 if (!completion.nextArgumentOrValue) {
1219 // since the argument could be detected (hopefully unambiguously?) just return it for "final completion"
1220 completion.relevantArgs.push_back(completion.lastDetectedArg);
1221 completion.relevantArgs.sort(compareArgs);
1222 return completion;
1223 }
1224
1225 // define function to add parameter values of argument as possible completions
1226 const auto addValueCompletionsForArg = [&completion](const Argument *arg) {
1227 if (arg->valueCompletionBehaviour() & ValueCompletionBehavior::PreDefinedValues) {
1228 completion.relevantPreDefinedValues.push_back(arg);
1229 }
1230 if (!(arg->valueCompletionBehaviour() & ValueCompletionBehavior::FileSystemIfNoPreDefinedValues) || !arg->preDefinedCompletionValues()) {
1231 completion.completeFiles = completion.completeFiles || arg->valueCompletionBehaviour() & ValueCompletionBehavior::Files;
1232 completion.completeDirs = completion.completeDirs || arg->valueCompletionBehaviour() & ValueCompletionBehavior::Directories;
1233 }
1234 };
1235
1236 // detect number of specified values
1237 auto currentValueCount = completion.lastDetectedArg->values(completion.lastDetectedArg->occurrences() - 1).size();
1238 // ignore values which are specified after the current word
1239 if (currentValueCount) {
1240 const auto currentWordIndexRelativeToLastDetectedArg = currentWordIndex - completion.lastDetectedArgIndex;
1241 if (currentValueCount > currentWordIndexRelativeToLastDetectedArg) {
1242 currentValueCount -= currentWordIndexRelativeToLastDetectedArg;
1243 } else {
1244 currentValueCount = 0;
1245 }
1246 }
1247
1248 // add value completions for implicit child if there are no value specified and there are no values required by the
1249 // last detected argument itself
1250 if (!currentValueCount && !completion.lastDetectedArg->requiredValueCount()) {
1251 for (const Argument *child : completion.lastDetectedArg->subArguments()) {
1252 if (child->isImplicit() && child->requiredValueCount()) {
1253 addValueCompletionsForArg(child);
1254 break;
1255 }
1256 }
1257 }
1258
1259 // add value completions for last argument if there are further values required
1260 if (completion.lastDetectedArg->requiredValueCount() == Argument::varValueCount
1261 || (currentValueCount < completion.lastDetectedArg->requiredValueCount())) {
1262 addValueCompletionsForArg(completion.lastDetectedArg);
1263 }
1264
1265 if (completion.lastDetectedArg->requiredValueCount() == Argument::varValueCount
1266 || completion.lastDetectedArg->values(completion.lastDetectedArg->occurrences() - 1).size()
1267 >= completion.lastDetectedArg->requiredValueCount()) {
1268 // sub arguments of the last arg are possible completions
1269 for (const Argument *subArg : completion.lastDetectedArg->subArguments()) {
1270 if (subArg->occurrences() < subArg->maxOccurrences()) {
1271 completion.relevantArgs.push_back(subArg);
1272 }
1273 }
1274
1275 // siblings of parents are possible completions as well
1276 for (auto parentArgument = completion.lastDetectedArgPath.crbegin(), end = completion.lastDetectedArgPath.crend();; ++parentArgument) {
1277 insertSiblings(parentArgument != end ? (*parentArgument)->subArguments() : m_mainArgs, completion.relevantArgs);
1278 if (parentArgument == end) {
1279 break;
1280 }
1281 }
1282 }
1283
1284 return completion;
1285 }
1286
1287 /*!
1288 * \brief Returns the suggestion string printed in error case due to unknown arguments.
1289 */
1290 string ArgumentParser::findSuggestions(int argc, const char *const *argv, unsigned int cursorPos, const ArgumentReader &reader) const
1291 {
1292 // determine completion info
1293 const auto completionInfo(determineCompletionInfo(argc, argv, cursorPos, reader));
1294
1295 // determine the unknown/misspelled argument
1296 const auto *unknownArg(*reader.argv);
1297 auto unknownArgSize(strlen(unknownArg));
1298 // -> refuse suggestions for long args to prevent huge memory allocation for Damerau-Levenshtein algo
1299 if (unknownArgSize > 16) {
1300 return string();
1301 }
1302 // -> remove dashes since argument names internally don't have them
1303 if (unknownArgSize >= 2 && unknownArg[0] == '-' && unknownArg[1] == '-') {
1304 unknownArg += 2;
1305 unknownArgSize -= 2;
1306 }
1307
1308 // find best suggestions limiting the results to 2
1309 multiset<ArgumentSuggestion> bestSuggestions;
1310 // -> consider relevant arguments
1311 for (const Argument *const arg : completionInfo.relevantArgs) {
1312 ArgumentSuggestion(unknownArg, unknownArgSize, arg->name(), !arg->denotesOperation()).addTo(bestSuggestions, 2);
1313 }
1314 // -> consider relevant values
1315 for (const Argument *const arg : completionInfo.relevantPreDefinedValues) {
1316 if (!arg->preDefinedCompletionValues()) {
1317 continue;
1318 }
1319 for (const char *i = arg->preDefinedCompletionValues(); *i; ++i) {
1320 const char *const wordStart(i);
1321 const char *wordEnd(wordStart + 1);
1322 for (; *wordEnd && *wordEnd != ' '; ++wordEnd)
1323 ;
1324 ArgumentSuggestion(unknownArg, unknownArgSize, wordStart, static_cast<size_t>(wordEnd - wordStart), false).addTo(bestSuggestions, 2);
1325 i = wordEnd;
1326 }
1327 }
1328
1329 // format suggestion
1330 string suggestionStr;
1331 if (const auto suggestionCount = bestSuggestions.size()) {
1332 // allocate memory
1333 size_t requiredSize = 15;
1334 for (const auto &suggestion : bestSuggestions) {
1335 requiredSize += suggestion.suggestionSize + 2;
1336 if (suggestion.hasDashPrefix) {
1337 requiredSize += 2;
1338 }
1339 }
1340 suggestionStr.reserve(requiredSize);
1341
1342 // add each suggestion to end up with something like "Did you mean status (1), pause (3), cat (4), edit (5) or rescan-all (8)?"
1343 suggestionStr += "\nDid you mean ";
1344 size_t i = 0;
1345 for (const auto &suggestion : bestSuggestions) {
1346 if (++i == suggestionCount && suggestionCount != 1) {
1347 suggestionStr += " or ";
1348 } else if (i > 1) {
1349 suggestionStr += ", ";
1350 }
1351 if (suggestion.hasDashPrefix) {
1352 suggestionStr += "--";
1353 }
1354 suggestionStr.append(suggestion.suggestion, suggestion.suggestionSize);
1355 }
1356 suggestionStr += '?';
1357 }
1358 return suggestionStr;
1359 }
1360
1361 /*!
1362 * \brief Returns a copy of \a escaped with escaping characters removed.
1363 */
1364 static std::string unescape(std::string_view escaped)
1365 {
1366 auto unescaped = std::string();
1367 auto onEscaping = false;
1368 unescaped.reserve(escaped.size());
1369 for (const auto c : escaped) {
1370 if (!onEscaping && c == '\\') {
1371 onEscaping = true;
1372 } else {
1373 unescaped += c;
1374 onEscaping = false;
1375 }
1376 }
1377 return unescaped;
1378 }
1379
1380 /*!
1381 * \brief Prints the bash completion for the specified arguments and the specified \a lastPath.
1382 * \remarks Arguments must have been parsed before with readSpecifiedArgs(). When calling this method, completionMode must
1383 * be set to true.
1384 */
1385 void ArgumentParser::printBashCompletion(int argc, const char *const *argv, unsigned int currentWordIndex, const ArgumentReader &reader) const
1386 {
1387 // determine completion info and sort relevant arguments
1388 const auto completionInfo([&] {
1389 auto clutteredCompletionInfo(determineCompletionInfo(argc, argv, currentWordIndex, reader));
1390 clutteredCompletionInfo.relevantArgs.sort(compareArgs);
1391 return clutteredCompletionInfo;
1392 }());
1393
1394 // read the "opening" (started but not finished argument denotation)
1395 const char *opening = nullptr;
1396 string compoundOpening;
1397 size_t openingLen = 0, compoundOpeningStartLen = 0;
1398 unsigned char openingDenotationType = Value;
1399 if (argc && completionInfo.nextArgumentOrValue) {
1400 if (currentWordIndex < static_cast<unsigned int>(argc)) {
1401 opening = argv[currentWordIndex];
1402 // For some reason completions for eg. "set --values disk=1 tag=a" are split so the
1403 // equation sign is an own argument ("set --values disk = 1 tag = a").
1404 // This is not how values are treated by the argument parser. Hence the opening
1405 // must be joined again. In this case only the part after the equation sign needs to be
1406 // provided for completion so compoundOpeningStartLen is set to number of characters to skip.
1407 const size_t minCurrentWordIndex = (completionInfo.lastDetectedArg ? completionInfo.lastDetectedArgIndex : 0);
1408 if (currentWordIndex > minCurrentWordIndex && !strcmp(opening, "=")) {
1409 compoundOpening.reserve(compoundOpeningStartLen = strlen(argv[--currentWordIndex]) + 1);
1410 compoundOpening = argv[currentWordIndex];
1411 compoundOpening += '=';
1412 } else if (currentWordIndex > (minCurrentWordIndex + 1) && !strcmp(argv[currentWordIndex - 1], "=")) {
1413 compoundOpening.reserve((compoundOpeningStartLen = strlen(argv[currentWordIndex -= 2]) + 1) + strlen(opening));
1414 compoundOpening = argv[currentWordIndex];
1415 compoundOpening += '=';
1416 compoundOpening += opening;
1417 }
1418 if (!compoundOpening.empty()) {
1419 opening = compoundOpening.data();
1420 }
1421 } else {
1422 opening = *completionInfo.lastSpecifiedArg;
1423 }
1424 if (*opening == '-') {
1425 ++opening;
1426 ++openingDenotationType;
1427 if (*opening == '-') {
1428 ++opening;
1429 ++openingDenotationType;
1430 }
1431 }
1432 openingLen = strlen(opening);
1433 }
1434
1435 // print "COMPREPLY" bash array
1436 cout << "COMPREPLY=(";
1437 // -> completions for parameter values
1438 bool noWhitespace = false;
1439 for (const Argument *const arg : completionInfo.relevantPreDefinedValues) {
1440 if (arg->valueCompletionBehaviour() & ValueCompletionBehavior::InvokeCallback && arg->m_callbackFunction) {
1441 arg->m_callbackFunction(arg->isPresent() ? arg->m_occurrences.front() : ArgumentOccurrence(Argument::varValueCount));
1442 }
1443 if (!arg->preDefinedCompletionValues()) {
1444 continue;
1445 }
1446 const bool appendEquationSign = arg->valueCompletionBehaviour() & ValueCompletionBehavior::AppendEquationSign;
1447 if (argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening) {
1448 if (openingDenotationType != Value) {
1449 continue;
1450 }
1451 bool wordStart = true, ok = false, equationSignAlreadyPresent = false;
1452 size_t wordIndex = 0;
1453 for (const char *i = arg->preDefinedCompletionValues(), *end = opening + openingLen; *i;) {
1454 if (wordStart) {
1455 const char *i1 = i, *i2 = opening;
1456 for (; *i1 && i2 != end && *i1 == *i2; ++i1, ++i2)
1457 ;
1458 if ((ok = (i2 == end))) {
1459 cout << '\'';
1460 }
1461 wordStart = false;
1462 wordIndex = 0;
1463 } else if ((wordStart = (*i == ' ') || (*i == '\n'))) {
1464 equationSignAlreadyPresent = false;
1465 if (ok) {
1466 cout << '\'' << ' ';
1467 }
1468 ++i;
1469 continue;
1470 } else if (*i == '=') {
1471 equationSignAlreadyPresent = true;
1472 }
1473 if (!ok) {
1474 ++i;
1475 continue;
1476 }
1477 if (!compoundOpeningStartLen || wordIndex >= compoundOpeningStartLen) {
1478 if (*i == '\'') {
1479 cout << "'\"'\"'";
1480 } else {
1481 cout << *i;
1482 }
1483 }
1484 ++i;
1485 ++wordIndex;
1486 switch (*i) {
1487 case ' ':
1488 case '\n':
1489 case '\0':
1490 if (appendEquationSign && !equationSignAlreadyPresent) {
1491 cout << '=';
1492 noWhitespace = true;
1493 equationSignAlreadyPresent = false;
1494 }
1495 if (*i == '\0') {
1496 cout << '\'';
1497 }
1498 }
1499 }
1500 cout << ' ';
1501 } else if (const char *i = arg->preDefinedCompletionValues()) {
1502 bool equationSignAlreadyPresent = false;
1503 cout << '\'';
1504 while (*i) {
1505 if (*i == '\'') {
1506 cout << "'\"'\"'";
1507 } else {
1508 cout << *i;
1509 }
1510 switch (*(++i)) {
1511 case '=':
1512 equationSignAlreadyPresent = true;
1513 break;
1514 case ' ':
1515 case '\n':
1516 case '\0':
1517 if (appendEquationSign && !equationSignAlreadyPresent) {
1518 cout << '=';
1519 equationSignAlreadyPresent = false;
1520 }
1521 if (*i != '\0') {
1522 cout << '\'';
1523 if (*(++i)) {
1524 cout << ' ' << '\'';
1525 }
1526 }
1527 }
1528 }
1529 cout << '\'' << ' ';
1530 }
1531 }
1532 // -> completions for further arguments
1533 for (const Argument *const arg : completionInfo.relevantArgs) {
1534 if (argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening) {
1535 switch (openingDenotationType) {
1536 case Value:
1537 if (!arg->denotesOperation() || strncmp(arg->name(), opening, openingLen)) {
1538 continue;
1539 }
1540 break;
1541 case Abbreviation:
1542 break;
1543 case FullName:
1544 if (strncmp(arg->name(), opening, openingLen)) {
1545 continue;
1546 }
1547 }
1548 }
1549
1550 if (opening && openingDenotationType == Abbreviation && !completionInfo.nextArgumentOrValue) {
1551 // TODO: add test for this case
1552 cout << '\'' << '-' << opening << arg->abbreviation() << '\'' << ' ';
1553 } else if (completionInfo.lastDetectedArg && reader.argDenotationType == Abbreviation && !completionInfo.nextArgumentOrValue) {
1554 if (reader.argv == reader.end) {
1555 cout << '\'' << *(reader.argv - 1) << '\'' << ' ';
1556 }
1557 } else if (arg->denotesOperation()) {
1558 cout << '\'' << arg->name() << '\'' << ' ';
1559 } else {
1560 cout << '\'' << '-' << '-' << arg->name() << '\'' << ' ';
1561 }
1562 }
1563 // -> completions for files and dirs
1564 // -> if there's already an "opening", determine the dir part and the file part
1565 auto actualDir = std::string(), actualFile = std::string();
1566 auto haveFileOrDirCompletions = false;
1567 if (argc && currentWordIndex == completionInfo.lastSpecifiedArgIndex && opening) {
1568 // the "opening" might contain escaped characters which need to be unescaped first
1569 const auto unescapedOpening = unescape(opening);
1570 // determine the "directory" part
1571 auto dir = directory(unescapedOpening);
1572 if (dir.empty()) {
1573 actualDir = ".";
1574 } else {
1575 if (dir[0] == '\"' || dir[0] == '\'') {
1576 dir.erase(0, 1);
1577 }
1578 if (dir.size() > 1 && (dir[dir.size() - 2] == '\"' || dir[dir.size() - 2] == '\'')) {
1579 dir.erase(dir.size() - 2, 1);
1580 }
1581 actualDir = std::move(dir);
1582 }
1583 // determine the "file" part
1584 auto file = fileName(unescapedOpening);
1585 if (file[0] == '\"' || file[0] == '\'') {
1586 file.erase(0, 1);
1587 }
1588 if (file.size() > 1 && (file[file.size() - 2] == '\"' || file[file.size() - 2] == '\'')) {
1589 file.erase(file.size() - 2, 1);
1590 }
1591 actualFile = std::move(file);
1592 }
1593
1594 // -> completion for files and dirs
1595 #ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
1596 if (completionInfo.completeFiles || completionInfo.completeDirs) {
1597 try {
1598 const auto replace = "'"s, with = "'\"'\"'"s;
1599 const auto useActualDir = argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening;
1600 const auto dirEntries = [&] {
1601 filesystem::directory_iterator i;
1602 if (useActualDir) {
1603 i = filesystem::directory_iterator(actualDir);
1604 findAndReplace(actualDir, replace, with);
1605 } else {
1606 i = filesystem::directory_iterator(".");
1607 }
1608 return i;
1609 }();
1610 for (const auto &dirEntry : dirEntries) {
1611 if (!completionInfo.completeDirs && dirEntry.is_directory()) {
1612 continue;
1613 }
1614 if (!completionInfo.completeFiles && !dirEntry.is_directory()) {
1615 continue;
1616 }
1617 auto dirEntryName = dirEntry.path().filename().string();
1618 auto hasStartingQuote = false;
1619 if (useActualDir) {
1620 if (!startsWith(dirEntryName, actualFile)) {
1621 continue;
1622 }
1623 cout << '\'';
1624 hasStartingQuote = true;
1625 if (actualDir != ".") {
1626 cout << actualDir;
1627 }
1628 }
1629 findAndReplace(dirEntryName, replace, with);
1630 if (!hasStartingQuote) {
1631 cout << '\'';
1632 }
1633 cout << dirEntryName << '\'' << ' ';
1634 haveFileOrDirCompletions = true;
1635 }
1636 } catch (const filesystem::filesystem_error &) {
1637 // ignore filesystem errors; there's no good way to report errors when printing bash completion
1638 }
1639 }
1640 #endif
1641 cout << ')';
1642
1643 // ensure file or dir completions are formatted appropriately
1644 if (haveFileOrDirCompletions) {
1645 cout << "; compopt -o filenames";
1646 }
1647
1648 // ensure trailing whitespace is omitted
1649 if (noWhitespace) {
1650 cout << "; compopt -o nospace";
1651 }
1652
1653 cout << endl;
1654 }
1655
1656 /*!
1657 * \brief Checks the constrains of the specified \a args.
1658 * \remarks Checks the constraints of sub arguments, too.
1659 */
1660 void ArgumentParser::checkConstraints(const ArgumentVector &args)
1661 {
1662 for (const Argument *arg : args) {
1663 const auto occurrences = arg->occurrences();
1664 if (arg->isParentPresent() && occurrences > arg->maxOccurrences()) {
1665 throw ParseError(argsToString("The argument \"", arg->name(), "\" mustn't be specified more than ", arg->maxOccurrences(),
1666 (arg->maxOccurrences() == 1 ? " time." : " times.")));
1667 }
1668 if (arg->isParentPresent() && occurrences < arg->minOccurrences()) {
1669 throw ParseError(argsToString("The argument \"", arg->name(), "\" must be specified at least ", arg->minOccurrences(),
1670 (arg->minOccurrences() == 1 ? " time." : " times.")));
1671 }
1672 Argument *conflictingArgument = nullptr;
1673 if (arg->isMainArgument()) {
1674 if (!arg->isCombinable() && arg->isPresent()) {
1675 conflictingArgument = firstPresentUncombinableArg(m_mainArgs, arg);
1676 }
1677 } else {
1678 conflictingArgument = arg->conflictsWithArgument();
1679 }
1680 if (conflictingArgument) {
1681 throw ParseError(argsToString("The argument \"", conflictingArgument->name(), "\" can not be combined with \"", arg->name(), "\"."));
1682 }
1683 for (size_t i = 0; i != occurrences; ++i) {
1684 if (arg->allRequiredValuesPresent(i)) {
1685 continue;
1686 }
1687 stringstream ss(stringstream::in | stringstream::out);
1688 ss << "Not all parameters for argument \"" << arg->name() << "\" ";
1689 if (i) {
1690 ss << " (" << (i + 1) << " occurrence) ";
1691 }
1692 ss << "provided. You have to provide the following parameters:";
1693 size_t valueNamesPrint = 0;
1694 for (const auto &name : arg->m_valueNames) {
1695 ss << ' ' << name;
1696 ++valueNamesPrint;
1697 }
1698 if (arg->m_requiredValueCount != Argument::varValueCount) {
1699 while (valueNamesPrint < arg->m_requiredValueCount) {
1700 ss << "\nvalue " << (++valueNamesPrint);
1701 }
1702 }
1703 throw ParseError(ss.str());
1704 }
1705
1706 // check constraints of sub arguments recursively
1707 checkConstraints(arg->m_subArgs);
1708 }
1709 }
1710
1711 /*!
1712 * \brief Invokes the callbacks for the specified \a args.
1713 * \remarks
1714 * - Checks the callbacks for sub arguments, too.
1715 * - Invokes the assigned callback methods for each occurrence of
1716 * the argument.
1717 */
1718 void ArgumentParser::invokeCallbacks(const ArgumentVector &args)
1719 {
1720 for (const Argument *arg : args) {
1721 // invoke the callback for each occurrence of the argument
1722 if (arg->m_callbackFunction) {
1723 for (const auto &occurrence : arg->m_occurrences) {
1724 arg->m_callbackFunction(occurrence);
1725 }
1726 }
1727 // invoke the callbacks for sub arguments recursively
1728 invokeCallbacks(arg->m_subArgs);
1729 }
1730 }
1731
1732 /*!
1733 * \brief Exits using the assigned function or std::exit().
1734 */
1735 void ArgumentParser::invokeExit(int code)
1736 {
1737 if (m_exitFunction) {
1738 m_exitFunction(code);
1739 return;
1740 }
1741 std::exit(code);
1742 }
1743
1744 /*!
1745 * \class HelpArgument
1746 * \brief The HelpArgument class prints help information for an argument parser
1747 * when present (--help, -h).
1748 */
1749
1750 /*!
1751 * \brief Constructs a new help argument for the specified parser.
1752 */
1753 HelpArgument::HelpArgument(ArgumentParser &parser)
1754 : Argument("help", 'h', "shows this information")
1755 {
1756 setCallback([&parser](const ArgumentOccurrence &) {
1757 CMD_UTILS_START_CONSOLE;
1758 parser.printHelp(cout);
1759 });
1760 }
1761
1762 /*!
1763 * \class OperationArgument
1764 * \brief The OperationArgument class is an Argument where denotesOperation() is true by default.
1765 */
1766
1767 /*!
1768 * \class ConfigValueArgument
1769 * \brief The ConfigValueArgument class is an Argument where setCombinable() is true by default.
1770 * \sa ConfigValueArgument::ConfigValueArgument()
1771 */
1772
1773 /*!
1774 * \class NoColorArgument
1775 * \brief The NoColorArgument class allows to specify whether use of escape codes or similar technique to provide formatted output
1776 * on the terminal should be enabled/disabled.
1777 *
1778 * This argument will either prevent or explicitly allow the use of escape codes or similar technique to provide formatted output
1779 * on the terminal. More explicitly, the argument will always allow to negate the default value of EscapeCodes::enabled which can be
1780 * configured at build time by setting the CMake variable ENABLE_ESCAPE_CODES_BY_DEFAULT.
1781 *
1782 * \remarks
1783 * - Only the first instance is considered for actually altering the value of EscapeCodes::enabled so it makes no sense to
1784 * instantiate this class multiple times.
1785 * - It is ensure that EscapeCodes::enabled will be set before any callback functions are invoked and even in the error case (if
1786 * the error doesn't prevent the argument from being detected). Hence this feature is implemented via NoColorArgument::apply()
1787 * rather than the usual callback mechanism.
1788 *
1789 * \sa NoColorArgument::NoColorArgument(), EscapeCodes::enabled
1790 */
1791
1792 /*!
1793 * \brief Constructs a new NoColorArgument argument.
1794 * \remarks This will also set EscapeCodes::enabled to the value of the environment variable ENABLE_ESCAPE_CODES.
1795 */
1796 NoColorArgument::NoColorArgument()
1797 #ifdef CPP_UTILITIES_ESCAPE_CODES_ENABLED_BY_DEFAULT
1798 : Argument("no-color", '\0', "disables formatted/colorized output")
1799 #else
1800 : Argument("enable-color", '\0', "enables formatted/colorized output")
1801 #endif
1802 {
1803 setCombinable(true);
1804
1805 // set the environment variable (not directly used and just assigned for printing help)
1806 setEnvironmentVariable("ENABLE_ESCAPE_CODES");
1807
1808 // initialize EscapeCodes::enabled from environment variable
1809 const auto escapeCodesEnabled = isEnvVariableSet(environmentVariable());
1810 if (escapeCodesEnabled.has_value()) {
1811 EscapeCodes::enabled = escapeCodesEnabled.value();
1812 }
1813 }
1814
1815 /*!
1816 * \brief Sets EscapeCodes::enabled according to the presence of the first instantiation of NoColorArgument.
1817 */
1818 void NoColorArgument::apply() const
1819 {
1820 if (isPresent()) {
1821 #ifdef CPP_UTILITIES_ESCAPE_CODES_ENABLED_BY_DEFAULT
1822 EscapeCodes::enabled = false;
1823 #else
1824 EscapeCodes::enabled = true;
1825 #endif
1826 }
1827 }
1828
1829 /*!
1830 * \brief Throws a Failure for the current instance and the specified \a argumentPath.
1831 */
1832 void ValueConversion::Helper::ArgumentValueConversionError::throwFailure(const std::vector<Argument *> &argumentPath) const
1833 {
1834 throw ParseError(argumentPath.empty()
1835 ? argsToString("Conversion of top-level value \"", valueToConvert, "\" to type \"", targetTypeName, "\" failed: ", errorMessage)
1836 : argsToString("Conversion of value \"", valueToConvert, "\" (for argument --", argumentPath.back()->name(), ") to type \"",
1837 targetTypeName, "\" failed: ", errorMessage));
1838 }
1839
1840 /*!
1841 * \brief Throws a Failure for insufficient number of values.
1842 */
1843 void ArgumentOccurrence::throwNumberOfValuesNotSufficient(unsigned long valuesToConvert) const
1844 {
1845 throw ParseError(path.empty()
1846 ? argsToString("Expected ", valuesToConvert, " top-level values to be present but only ", values.size(), " have been specified.")
1847 : argsToString("Expected ", valuesToConvert, " values for argument --", path.back()->name(), " to be present but only ", values.size(),
1848 " have been specified."));
1849 }
1850
1851 } // namespace CppUtilities