C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
argumentparser.cpp
Go to the documentation of this file.
1#include "./argumentparser.h"
4
8#include "../io/path.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
24using namespace std;
25using namespace std::placeholders;
26using namespace std::literals;
27using namespace CppUtilities::EscapeCodes;
28
33namespace CppUtilities {
34
38enum ArgumentDenotationType : unsigned char {
39 Value = 0,
41 FullName = 2
42};
43
49
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
66 : lastDetectedArg(reader.lastArg)
67{
68}
69
71struct 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
84ArgumentSuggestion::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
92ArgumentSuggestion::ArgumentSuggestion(const char *unknownArg, size_t unknownArgSize, const char *suggestion, bool isOperation)
93 : ArgumentSuggestion(unknownArg, unknownArgSize, suggestion, strlen(suggestion), isOperation)
94{
95}
96
97bool ArgumentSuggestion::operator<(const ArgumentSuggestion &other) const
98{
99 return editingDistance < other.editingDistance;
100}
101
102void 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}
113
126ArgumentReader::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
141ArgumentReader &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
156{
157 return read(args);
158}
159
163bool 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
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;
204 } else {
205 // determine denotation type
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;
216 if (*argDenotation == '-') {
219 if (*argDenotation == '-') {
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
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;
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);
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) {
385 cerr << Phrases::Warning << "The specified argument \"" << *argv << "\" is unknown and will be ignored." << Phrases::EndFlush;
386 [[fallthrough]];
388 // ignore unknown denotation
389 ++index;
390 ++argv;
391 argDenotation = nullptr;
392 break;
394 return false;
395 }
396 }
397 } // while(argv != end)
398 return true;
399}
400
407ostream &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
437
439
440inline bool notEmpty(const char *str)
441{
442 return str && *str;
443}
444
446
463Argument::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)
477 , m_preDefinedCompletionValues(nullptr)
478{
479}
480
487
495const 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
509const 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
521void Argument::printInfo(ostream &os, unsigned char indentation) const
522{
523 if (isDeprecated()) {
524 return;
525 }
526 Indentation ident(indentation);
527 os << ident;
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 }
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 }
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
593{
594 for (Argument *arg : args) {
595 if (arg != except && arg->isPresent() && !arg->isCombinable()) {
596 return arg;
597 }
598 }
599 return nullptr;
600}
601
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();
626}
627
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
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
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
706
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
735{
736 for (Argument *arg : m_subArgs) {
737 if (arg->denotesOperation() && arg->isPresent()) {
738 return arg;
739 }
740 }
741 return nullptr;
742}
743
749{
750 for (Argument *arg : m_subArgs) {
751 arg->resetRecursively();
752 }
753 reset();
754}
755
773 : m_actualArgc(0)
774 , m_executable(nullptr)
775 , m_unknownArgBehavior(UnknownArgumentBehavior::Fail)
776 , m_defaultArg(nullptr)
777 , m_helpArg(*this)
778{
779}
780
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
822{
823 argument->m_isMainArg = true;
824 m_mainArgs.push_back(argument);
825}
826
830void ArgumentParser::printHelp(ostream &os) const
831{
833 bool wroteLine = false;
835 os << applicationInfo.name;
837 os << ',' << ' ';
838 }
839 wroteLine = true;
840 }
842 os << "version " << applicationInfo.version;
843 wroteLine = true;
844 }
845 if (wroteLine) {
846 os << '\n' << '\n';
847 }
849
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';
908 os << "Linked against: " << *i;
909 for (++i; i != end; ++i) {
910 os << ',' << ' ' << *i;
911 }
912 os << '\n';
913 }
914
916 os << "\nProject website: " << applicationInfo.url << endl;
917 }
918}
919
936void ArgumentParser::parseArgs(int argc, const char *const *argv, ParseArgumentBehavior behavior)
937{
938 try {
939 readArgs(argc, argv);
940 if (!argc) {
941 return;
942 }
944 checkConstraints(m_mainArgs);
945 }
947 invokeCallbacks(m_mainArgs);
948 }
949 } catch (const ParseError &failure) {
952 cerr << failure;
953 invokeExit(EXIT_FAILURE);
954 }
955 throw;
956 }
957}
958
972void 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 if (m_defaultArg) {
988 m_defaultArg->m_occurrences.emplace_back(0);
989 }
990 return;
991 }
992
993 // check for completion mode: if first arg (after executable name) is "--bash-completion-for", bash completion for the following arguments is requested
994 const bool completionMode = !strcmp(*++argv, "--bash-completion-for");
995
996 // determine the index of the current word for completion and the number of arguments to be passed to ArgumentReader
997 unsigned int currentWordIndex = 0, argcForReader;
998 if (completionMode) {
999 // the first argument after "--bash-completion-for" is the index of the current word
1000 try {
1001 currentWordIndex = (--argc ? stringToNumber<unsigned int, string>(*(++argv)) : 0);
1002 if (argc) {
1003 ++argv;
1004 --argc;
1005 }
1006 } catch (const ConversionException &) {
1007 currentWordIndex = static_cast<unsigned int>(argc - 1);
1008 }
1009 argcForReader = min(static_cast<unsigned int>(argc), currentWordIndex + 1);
1010 } else {
1011 argcForReader = static_cast<unsigned int>(argc);
1012 }
1013
1014 // read specified arguments
1015 ArgumentReader reader(*this, argv, argv + argcForReader, completionMode);
1016 const bool allArgsProcessed(reader.read());
1017 m_noColorArg.apply();
1018
1019 // fail when not all arguments could be processed, except when in completion mode
1020 if (!completionMode && !allArgsProcessed) {
1021 const auto suggestions(findSuggestions(argc, argv, static_cast<unsigned int>(argc - 1), reader));
1022 throw ParseError(argsToString("The specified argument \"", *reader.argv, "\" is unknown.", suggestions));
1023 }
1024
1025 // print Bash completion and prevent the application to continue with the regular execution
1026 if (completionMode) {
1027 printBashCompletion(argc, argv, currentWordIndex, reader);
1028 invokeExit(EXIT_SUCCESS);
1029 }
1030}
1031
1037{
1038 for (Argument *arg : m_mainArgs) {
1039 arg->resetRecursively();
1040 }
1041 m_actualArgc = 0;
1042}
1043
1050{
1051 for (Argument *arg : m_mainArgs) {
1052 if (arg->denotesOperation() && arg->isPresent()) {
1053 return arg;
1054 }
1055 }
1056 return nullptr;
1057}
1058
1063{
1064 for (const Argument *arg : m_mainArgs) {
1065 if (!arg->isCombinable() && arg->isPresent()) {
1066 return true;
1067 }
1068 }
1069 return false;
1070}
1071
1072#ifdef CPP_UTILITIES_DEBUG_BUILD
1087void ArgumentParser::verifyArgs(const ArgumentVector &args)
1088{
1089 vector<const Argument *> verifiedArgs;
1090 verifiedArgs.reserve(args.size());
1091 vector<char> abbreviations;
1092 abbreviations.reserve(abbreviations.size() + args.size());
1093 vector<const char *> names;
1094 names.reserve(names.size() + args.size());
1095 bool hasImplicit = false;
1096 for (const Argument *arg : args) {
1097 assert(find(verifiedArgs.cbegin(), verifiedArgs.cend(), arg) == verifiedArgs.cend());
1098 verifiedArgs.push_back(arg);
1099 assert(!arg->isImplicit() || !hasImplicit);
1100 hasImplicit |= arg->isImplicit();
1101 assert(!arg->abbreviation() || find(abbreviations.cbegin(), abbreviations.cend(), arg->abbreviation()) == abbreviations.cend());
1102 abbreviations.push_back(arg->abbreviation());
1103 assert(!arg->name() || find_if(names.cbegin(), names.cend(), [arg](const char *name) { return !strcmp(arg->name(), name); }) == names.cend());
1104 assert(arg->requiredValueCount() == 0 || arg->subArguments().size() == 0 || (arg->flags() & Argument::Flags::Greedy));
1105 names.emplace_back(arg->name());
1106 }
1107 for (const Argument *arg : args) {
1108 verifyArgs(arg->subArguments());
1109 }
1110}
1111#endif
1112
1120bool compareArgs(const Argument *arg1, const Argument *arg2)
1121{
1122 if (arg1->denotesOperation() && !arg2->denotesOperation()) {
1123 return true;
1124 } else if (!arg1->denotesOperation() && arg2->denotesOperation()) {
1125 return false;
1126 } else {
1127 return strcmp(arg1->name(), arg2->name()) < 0;
1128 }
1129}
1130
1135void insertSiblings(const ArgumentVector &siblings, list<const Argument *> &target)
1136{
1137 bool onlyCombinable = false;
1138 for (const Argument *sibling : siblings) {
1139 if (sibling->isPresent() && !sibling->isCombinable()) {
1140 onlyCombinable = true;
1141 break;
1142 }
1143 }
1144 for (const Argument *sibling : siblings) {
1145 if ((!onlyCombinable || sibling->isCombinable()) && sibling->occurrences() < sibling->maxOccurrences()) {
1146 target.push_back(sibling);
1147 }
1148 }
1149}
1150
1154ArgumentCompletionInfo ArgumentParser::determineCompletionInfo(
1155 int argc, const char *const *argv, unsigned int currentWordIndex, const ArgumentReader &reader) const
1156{
1157 ArgumentCompletionInfo completion(reader);
1158
1159 // determine last detected arg
1160 if (completion.lastDetectedArg) {
1161 completion.lastDetectedArgIndex = static_cast<size_t>(reader.lastArgDenotation - argv);
1162 completion.lastDetectedArgPath = completion.lastDetectedArg->path(completion.lastDetectedArg->occurrences() - 1);
1163 }
1164
1165 // determine last arg, omitting trailing empty args
1166 if (argc) {
1167 completion.lastSpecifiedArgIndex = static_cast<unsigned int>(argc) - 1;
1168 completion.lastSpecifiedArg = argv + completion.lastSpecifiedArgIndex;
1169 for (; completion.lastSpecifiedArg >= argv && **completion.lastSpecifiedArg == '\0';
1170 --completion.lastSpecifiedArg, --completion.lastSpecifiedArgIndex)
1171 ;
1172 }
1173
1174 // just return main arguments if no args detected
1175 if (!completion.lastDetectedArg || !completion.lastDetectedArg->isPresent()) {
1176 completion.nextArgumentOrValue = true;
1177 insertSiblings(m_mainArgs, completion.relevantArgs);
1178 completion.relevantArgs.sort(compareArgs);
1179 return completion;
1180 }
1181
1182 completion.nextArgumentOrValue = currentWordIndex > completion.lastDetectedArgIndex;
1183 if (!completion.nextArgumentOrValue) {
1184 // since the argument could be detected (hopefully unambiguously?) just return it for "final completion"
1185 completion.relevantArgs.push_back(completion.lastDetectedArg);
1186 completion.relevantArgs.sort(compareArgs);
1187 return completion;
1188 }
1189
1190 // define function to add parameter values of argument as possible completions
1191 const auto addValueCompletionsForArg = [&completion](const Argument *arg) {
1192 if (arg->valueCompletionBehaviour() & ValueCompletionBehavior::PreDefinedValues) {
1193 completion.relevantPreDefinedValues.push_back(arg);
1194 }
1195 if (!(arg->valueCompletionBehaviour() & ValueCompletionBehavior::FileSystemIfNoPreDefinedValues) || !arg->preDefinedCompletionValues()) {
1196 completion.completeFiles = completion.completeFiles || arg->valueCompletionBehaviour() & ValueCompletionBehavior::Files;
1197 completion.completeDirs = completion.completeDirs || arg->valueCompletionBehaviour() & ValueCompletionBehavior::Directories;
1198 }
1199 };
1200
1201 // detect number of specified values
1202 auto currentValueCount = completion.lastDetectedArg->values(completion.lastDetectedArg->occurrences() - 1).size();
1203 // ignore values which are specified after the current word
1204 if (currentValueCount) {
1205 const auto currentWordIndexRelativeToLastDetectedArg = currentWordIndex - completion.lastDetectedArgIndex;
1206 if (currentValueCount > currentWordIndexRelativeToLastDetectedArg) {
1207 currentValueCount -= currentWordIndexRelativeToLastDetectedArg;
1208 } else {
1209 currentValueCount = 0;
1210 }
1211 }
1212
1213 // add value completions for implicit child if there are no value specified and there are no values required by the
1214 // last detected argument itself
1215 if (!currentValueCount && !completion.lastDetectedArg->requiredValueCount()) {
1216 for (const Argument *child : completion.lastDetectedArg->subArguments()) {
1217 if (child->isImplicit() && child->requiredValueCount()) {
1218 addValueCompletionsForArg(child);
1219 break;
1220 }
1221 }
1222 }
1223
1224 // add value completions for last argument if there are further values required
1225 if (completion.lastDetectedArg->requiredValueCount() == Argument::varValueCount
1226 || (currentValueCount < completion.lastDetectedArg->requiredValueCount())) {
1227 addValueCompletionsForArg(completion.lastDetectedArg);
1228 }
1229
1230 if (completion.lastDetectedArg->requiredValueCount() == Argument::varValueCount
1231 || completion.lastDetectedArg->values(completion.lastDetectedArg->occurrences() - 1).size()
1232 >= completion.lastDetectedArg->requiredValueCount()) {
1233 // sub arguments of the last arg are possible completions
1234 for (const Argument *subArg : completion.lastDetectedArg->subArguments()) {
1235 if (subArg->occurrences() < subArg->maxOccurrences()) {
1236 completion.relevantArgs.push_back(subArg);
1237 }
1238 }
1239
1240 // siblings of parents are possible completions as well
1241 for (auto parentArgument = completion.lastDetectedArgPath.crbegin(), end = completion.lastDetectedArgPath.crend();; ++parentArgument) {
1242 insertSiblings(parentArgument != end ? (*parentArgument)->subArguments() : m_mainArgs, completion.relevantArgs);
1243 if (parentArgument == end) {
1244 break;
1245 }
1246 }
1247 }
1248
1249 return completion;
1250}
1251
1255string ArgumentParser::findSuggestions(int argc, const char *const *argv, unsigned int cursorPos, const ArgumentReader &reader) const
1256{
1257 // determine completion info
1258 const auto completionInfo(determineCompletionInfo(argc, argv, cursorPos, reader));
1259
1260 // determine the unknown/misspelled argument
1261 const auto *unknownArg(*reader.argv);
1262 auto unknownArgSize(strlen(unknownArg));
1263 // -> refuse suggestions for long args to prevent huge memory allocation for Damerau-Levenshtein algo
1264 if (unknownArgSize > 16) {
1265 return string();
1266 }
1267 // -> remove dashes since argument names internally don't have them
1268 if (unknownArgSize >= 2 && unknownArg[0] == '-' && unknownArg[1] == '-') {
1269 unknownArg += 2;
1270 unknownArgSize -= 2;
1271 }
1272
1273 // find best suggestions limiting the results to 2
1274 multiset<ArgumentSuggestion> bestSuggestions;
1275 // -> consider relevant arguments
1276 for (const Argument *const arg : completionInfo.relevantArgs) {
1277 ArgumentSuggestion(unknownArg, unknownArgSize, arg->name(), !arg->denotesOperation()).addTo(bestSuggestions, 2);
1278 }
1279 // -> consider relevant values
1280 for (const Argument *const arg : completionInfo.relevantPreDefinedValues) {
1281 if (!arg->preDefinedCompletionValues()) {
1282 continue;
1283 }
1284 for (const char *i = arg->preDefinedCompletionValues(); *i; ++i) {
1285 const char *const wordStart(i);
1286 const char *wordEnd(wordStart + 1);
1287 for (; *wordEnd && *wordEnd != ' '; ++wordEnd)
1288 ;
1289 ArgumentSuggestion(unknownArg, unknownArgSize, wordStart, static_cast<size_t>(wordEnd - wordStart), false).addTo(bestSuggestions, 2);
1290 i = wordEnd;
1291 }
1292 }
1293
1294 // format suggestion
1295 string suggestionStr;
1296 if (const auto suggestionCount = bestSuggestions.size()) {
1297 // allocate memory
1298 size_t requiredSize = 15;
1299 for (const auto &suggestion : bestSuggestions) {
1300 requiredSize += suggestion.suggestionSize + 2;
1301 if (suggestion.hasDashPrefix) {
1302 requiredSize += 2;
1303 }
1304 }
1305 suggestionStr.reserve(requiredSize);
1306
1307 // add each suggestion to end up with something like "Did you mean status (1), pause (3), cat (4), edit (5) or rescan-all (8)?"
1308 suggestionStr += "\nDid you mean ";
1309 size_t i = 0;
1310 for (const auto &suggestion : bestSuggestions) {
1311 if (++i == suggestionCount && suggestionCount != 1) {
1312 suggestionStr += " or ";
1313 } else if (i > 1) {
1314 suggestionStr += ", ";
1315 }
1316 if (suggestion.hasDashPrefix) {
1317 suggestionStr += "--";
1318 }
1319 suggestionStr.append(suggestion.suggestion, suggestion.suggestionSize);
1320 }
1321 suggestionStr += '?';
1322 }
1323 return suggestionStr;
1324}
1325
1329static std::string unescape(std::string_view escaped)
1330{
1331 auto unescaped = std::string();
1332 auto onEscaping = false;
1333 unescaped.reserve(escaped.size());
1334 for (const auto c : escaped) {
1335 if (!onEscaping && c == '\\') {
1336 onEscaping = true;
1337 } else {
1338 unescaped += c;
1339 onEscaping = false;
1340 }
1341 }
1342 return unescaped;
1343}
1344
1350void ArgumentParser::printBashCompletion(int argc, const char *const *argv, unsigned int currentWordIndex, const ArgumentReader &reader) const
1351{
1352 // determine completion info and sort relevant arguments
1353 const auto completionInfo([&] {
1354 auto clutteredCompletionInfo(determineCompletionInfo(argc, argv, currentWordIndex, reader));
1355 clutteredCompletionInfo.relevantArgs.sort(compareArgs);
1356 return clutteredCompletionInfo;
1357 }());
1358
1359 // read the "opening" (started but not finished argument denotation)
1360 const char *opening = nullptr;
1361 string compoundOpening;
1362 size_t openingLen = 0, compoundOpeningStartLen = 0;
1363 unsigned char openingDenotationType = Value;
1364 if (argc && completionInfo.nextArgumentOrValue) {
1365 if (currentWordIndex < static_cast<unsigned int>(argc)) {
1366 opening = argv[currentWordIndex];
1367 // For some reason completions for eg. "set --values disk=1 tag=a" are split so the
1368 // equation sign is an own argument ("set --values disk = 1 tag = a").
1369 // This is not how values are treated by the argument parser. Hence the opening
1370 // must be joined again. In this case only the part after the equation sign needs to be
1371 // provided for completion so compoundOpeningStartLen is set to number of characters to skip.
1372 const size_t minCurrentWordIndex = (completionInfo.lastDetectedArg ? completionInfo.lastDetectedArgIndex : 0);
1373 if (currentWordIndex > minCurrentWordIndex && !strcmp(opening, "=")) {
1374 compoundOpening.reserve(compoundOpeningStartLen = strlen(argv[--currentWordIndex]) + 1);
1375 compoundOpening = argv[currentWordIndex];
1376 compoundOpening += '=';
1377 } else if (currentWordIndex > (minCurrentWordIndex + 1) && !strcmp(argv[currentWordIndex - 1], "=")) {
1378 compoundOpening.reserve((compoundOpeningStartLen = strlen(argv[currentWordIndex -= 2]) + 1) + strlen(opening));
1379 compoundOpening = argv[currentWordIndex];
1380 compoundOpening += '=';
1381 compoundOpening += opening;
1382 }
1383 if (!compoundOpening.empty()) {
1384 opening = compoundOpening.data();
1385 }
1386 } else {
1387 opening = *completionInfo.lastSpecifiedArg;
1388 }
1389 if (*opening == '-') {
1390 ++opening;
1391 ++openingDenotationType;
1392 if (*opening == '-') {
1393 ++opening;
1394 ++openingDenotationType;
1395 }
1396 }
1397 openingLen = strlen(opening);
1398 }
1399
1400 // print "COMPREPLY" bash array
1401 cout << "COMPREPLY=(";
1402 // -> completions for parameter values
1403 bool noWhitespace = false;
1404 for (const Argument *const arg : completionInfo.relevantPreDefinedValues) {
1405 if (arg->valueCompletionBehaviour() & ValueCompletionBehavior::InvokeCallback && arg->m_callbackFunction) {
1406 arg->m_callbackFunction(arg->isPresent() ? arg->m_occurrences.front() : ArgumentOccurrence(Argument::varValueCount));
1407 }
1408 if (!arg->preDefinedCompletionValues()) {
1409 continue;
1410 }
1411 const bool appendEquationSign = arg->valueCompletionBehaviour() & ValueCompletionBehavior::AppendEquationSign;
1412 if (argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening) {
1413 if (openingDenotationType != Value) {
1414 continue;
1415 }
1416 bool wordStart = true, ok = false, equationSignAlreadyPresent = false;
1417 size_t wordIndex = 0;
1418 for (const char *i = arg->preDefinedCompletionValues(), *end = opening + openingLen; *i;) {
1419 if (wordStart) {
1420 const char *i1 = i, *i2 = opening;
1421 for (; *i1 && i2 != end && *i1 == *i2; ++i1, ++i2)
1422 ;
1423 if ((ok = (i2 == end))) {
1424 cout << '\'';
1425 }
1426 wordStart = false;
1427 wordIndex = 0;
1428 } else if ((wordStart = (*i == ' ') || (*i == '\n'))) {
1429 equationSignAlreadyPresent = false;
1430 if (ok) {
1431 cout << '\'' << ' ';
1432 }
1433 ++i;
1434 continue;
1435 } else if (*i == '=') {
1436 equationSignAlreadyPresent = true;
1437 }
1438 if (!ok) {
1439 ++i;
1440 continue;
1441 }
1442 if (!compoundOpeningStartLen || wordIndex >= compoundOpeningStartLen) {
1443 if (*i == '\'') {
1444 cout << "'\"'\"'";
1445 } else {
1446 cout << *i;
1447 }
1448 }
1449 ++i;
1450 ++wordIndex;
1451 switch (*i) {
1452 case ' ':
1453 case '\n':
1454 case '\0':
1455 if (appendEquationSign && !equationSignAlreadyPresent) {
1456 cout << '=';
1457 noWhitespace = true;
1458 equationSignAlreadyPresent = false;
1459 }
1460 if (*i == '\0') {
1461 cout << '\'';
1462 }
1463 }
1464 }
1465 cout << ' ';
1466 } else if (const char *i = arg->preDefinedCompletionValues()) {
1467 bool equationSignAlreadyPresent = false;
1468 cout << '\'';
1469 while (*i) {
1470 if (*i == '\'') {
1471 cout << "'\"'\"'";
1472 } else {
1473 cout << *i;
1474 }
1475 switch (*(++i)) {
1476 case '=':
1477 equationSignAlreadyPresent = true;
1478 break;
1479 case ' ':
1480 case '\n':
1481 case '\0':
1482 if (appendEquationSign && !equationSignAlreadyPresent) {
1483 cout << '=';
1484 equationSignAlreadyPresent = false;
1485 }
1486 if (*i != '\0') {
1487 cout << '\'';
1488 if (*(++i)) {
1489 cout << ' ' << '\'';
1490 }
1491 }
1492 }
1493 }
1494 cout << '\'' << ' ';
1495 }
1496 }
1497 // -> completions for further arguments
1498 for (const Argument *const arg : completionInfo.relevantArgs) {
1499 if (argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening) {
1500 switch (openingDenotationType) {
1501 case Value:
1502 if (!arg->denotesOperation() || strncmp(arg->name(), opening, openingLen)) {
1503 continue;
1504 }
1505 break;
1506 case Abbreviation:
1507 break;
1508 case FullName:
1509 if (strncmp(arg->name(), opening, openingLen)) {
1510 continue;
1511 }
1512 }
1513 }
1514
1515 if (opening && openingDenotationType == Abbreviation && !completionInfo.nextArgumentOrValue) {
1516 // TODO: add test for this case
1517 cout << '\'' << '-' << opening << arg->abbreviation() << '\'' << ' ';
1518 } else if (completionInfo.lastDetectedArg && reader.argDenotationType == Abbreviation && !completionInfo.nextArgumentOrValue) {
1519 if (reader.argv == reader.end) {
1520 cout << '\'' << *(reader.argv - 1) << '\'' << ' ';
1521 }
1522 } else if (arg->denotesOperation()) {
1523 cout << '\'' << arg->name() << '\'' << ' ';
1524 } else {
1525 cout << '\'' << '-' << '-' << arg->name() << '\'' << ' ';
1526 }
1527 }
1528 // -> completions for files and dirs
1529 // -> if there's already an "opening", determine the dir part and the file part
1530 auto actualDir = std::string(), actualFile = std::string();
1531 auto haveFileOrDirCompletions = false;
1532 if (argc && currentWordIndex == completionInfo.lastSpecifiedArgIndex && opening) {
1533 // the "opening" might contain escaped characters which need to be unescaped first
1534 const auto unescapedOpening = unescape(opening);
1535 // determine the "directory" part
1536 auto dir = directory(unescapedOpening);
1537 if (dir.empty()) {
1538 actualDir = ".";
1539 } else {
1540 if (dir[0] == '\"' || dir[0] == '\'') {
1541 dir.erase(0, 1);
1542 }
1543 if (dir.size() > 1 && (dir[dir.size() - 2] == '\"' || dir[dir.size() - 2] == '\'')) {
1544 dir.erase(dir.size() - 2, 1);
1545 }
1546 actualDir = std::move(dir);
1547 }
1548 // determine the "file" part
1549 auto file = fileName(unescapedOpening);
1550 if (file[0] == '\"' || file[0] == '\'') {
1551 file.erase(0, 1);
1552 }
1553 if (file.size() > 1 && (file[file.size() - 2] == '\"' || file[file.size() - 2] == '\'')) {
1554 file.erase(file.size() - 2, 1);
1555 }
1556 actualFile = std::move(file);
1557 }
1558
1559 // -> completion for files and dirs
1560#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
1561 if (completionInfo.completeFiles || completionInfo.completeDirs) {
1562 try {
1563 const auto replace = "'"s, with = "'\"'\"'"s;
1564 const auto useActualDir = argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening;
1565 const auto dirEntries = [&] {
1566 filesystem::directory_iterator i;
1567 if (useActualDir) {
1568 i = filesystem::directory_iterator(actualDir);
1569 findAndReplace(actualDir, replace, with);
1570 } else {
1571 i = filesystem::directory_iterator(".");
1572 }
1573 return i;
1574 }();
1575 for (const auto &dirEntry : dirEntries) {
1576 if (!completionInfo.completeDirs && dirEntry.is_directory()) {
1577 continue;
1578 }
1579 if (!completionInfo.completeFiles && !dirEntry.is_directory()) {
1580 continue;
1581 }
1582 auto dirEntryName = dirEntry.path().filename().string();
1583 auto hasStartingQuote = false;
1584 if (useActualDir) {
1585 if (!startsWith(dirEntryName, actualFile)) {
1586 continue;
1587 }
1588 cout << '\'';
1589 hasStartingQuote = true;
1590 if (actualDir != ".") {
1591 cout << actualDir;
1592 }
1593 }
1594 findAndReplace(dirEntryName, replace, with);
1595 if (!hasStartingQuote) {
1596 cout << '\'';
1597 }
1598 cout << dirEntryName << '\'' << ' ';
1599 haveFileOrDirCompletions = true;
1600 }
1601 } catch (const filesystem::filesystem_error &) {
1602 // ignore filesystem errors; there's no good way to report errors when printing bash completion
1603 }
1604 }
1605#endif
1606 cout << ')';
1607
1608 // ensure file or dir completions are formatted appropriately
1609 if (haveFileOrDirCompletions) {
1610 cout << "; compopt -o filenames";
1611 }
1612
1613 // ensure trailing whitespace is omitted
1614 if (noWhitespace) {
1615 cout << "; compopt -o nospace";
1616 }
1617
1618 cout << endl;
1619}
1620
1626{
1627 for (const Argument *arg : args) {
1628 const auto occurrences = arg->occurrences();
1629 if (arg->isParentPresent() && occurrences > arg->maxOccurrences()) {
1630 throw ParseError(argsToString("The argument \"", arg->name(), "\" mustn't be specified more than ", arg->maxOccurrences(),
1631 (arg->maxOccurrences() == 1 ? " time." : " times.")));
1632 }
1633 if (arg->isParentPresent() && occurrences < arg->minOccurrences()) {
1634 throw ParseError(argsToString("The argument \"", arg->name(), "\" must be specified at least ", arg->minOccurrences(),
1635 (arg->minOccurrences() == 1 ? " time." : " times.")));
1636 }
1637 Argument *conflictingArgument = nullptr;
1638 if (arg->isMainArgument()) {
1639 if (!arg->isCombinable() && arg->isPresent()) {
1640 conflictingArgument = firstPresentUncombinableArg(m_mainArgs, arg);
1641 }
1642 } else {
1643 conflictingArgument = arg->conflictsWithArgument();
1644 }
1645 if (conflictingArgument) {
1646 throw ParseError(argsToString("The argument \"", conflictingArgument->name(), "\" can not be combined with \"", arg->name(), "\"."));
1647 }
1648 for (size_t i = 0; i != occurrences; ++i) {
1649 if (arg->allRequiredValuesPresent(i)) {
1650 continue;
1651 }
1652 stringstream ss(stringstream::in | stringstream::out);
1653 ss << "Not all parameters for argument \"" << arg->name() << "\" ";
1654 if (i) {
1655 ss << " (" << (i + 1) << " occurrence) ";
1656 }
1657 ss << "provided. You have to provide the following parameters:";
1658 size_t valueNamesPrint = 0;
1659 for (const auto &name : arg->m_valueNames) {
1660 ss << ' ' << name;
1661 ++valueNamesPrint;
1662 }
1663 if (arg->m_requiredValueCount != Argument::varValueCount) {
1664 while (valueNamesPrint < arg->m_requiredValueCount) {
1665 ss << "\nvalue " << (++valueNamesPrint);
1666 }
1667 }
1668 throw ParseError(ss.str());
1669 }
1670
1671 // check constraints of sub arguments recursively
1672 checkConstraints(arg->m_subArgs);
1673 }
1674}
1675
1684{
1685 for (const Argument *arg : args) {
1686 // invoke the callback for each occurrence of the argument
1687 if (arg->m_callbackFunction) {
1688 for (const auto &occurrence : arg->m_occurrences) {
1689 arg->m_callbackFunction(occurrence);
1690 }
1691 }
1692 // invoke the callbacks for sub arguments recursively
1693 invokeCallbacks(arg->m_subArgs);
1694 }
1695}
1696
1700void ArgumentParser::invokeExit(int code)
1701{
1702 if (m_exitFunction) {
1703 m_exitFunction(code);
1704 return;
1705 }
1706 std::exit(code);
1707}
1708
1719 : Argument("help", 'h', "shows this information")
1720{
1721 setCallback([&parser](const ArgumentOccurrence &) {
1723 parser.printHelp(cout);
1724 });
1725}
1726
1762#ifdef CPP_UTILITIES_ESCAPE_CODES_ENABLED_BY_DEFAULT
1763 : Argument("no-color", '\0', "disables formatted/colorized output")
1764#else
1765 : Argument("enable-color", '\0', "enables formatted/colorized output")
1766#endif
1767{
1768 setCombinable(true);
1769
1770 // set the environment variable (not directly used and just assigned for printing help)
1771 setEnvironmentVariable("ENABLE_ESCAPE_CODES");
1772
1773 // initialize EscapeCodes::enabled from environment variable
1774 const auto escapeCodesEnabled = isEnvVariableSet(environmentVariable());
1775 if (escapeCodesEnabled.has_value()) {
1776 EscapeCodes::enabled = escapeCodesEnabled.value();
1777 }
1778}
1779
1784{
1785 if (isPresent()) {
1786#ifdef CPP_UTILITIES_ESCAPE_CODES_ENABLED_BY_DEFAULT
1787 EscapeCodes::enabled = false;
1788#else
1789 EscapeCodes::enabled = true;
1790#endif
1791 }
1792}
1793
1797void ValueConversion::Helper::ArgumentValueConversionError::throwFailure(const std::vector<Argument *> &argumentPath) const
1798{
1799 throw ParseError(argumentPath.empty()
1800 ? argsToString("Conversion of top-level value \"", valueToConvert, "\" to type \"", targetTypeName, "\" failed: ", errorMessage)
1801 : argsToString("Conversion of value \"", valueToConvert, "\" (for argument --", argumentPath.back()->name(), ") to type \"",
1802 targetTypeName, "\" failed: ", errorMessage));
1803}
1804
1808void ArgumentOccurrence::throwNumberOfValuesNotSufficient(unsigned long valuesToConvert) const
1809{
1810 throw ParseError(path.empty()
1811 ? argsToString("Expected ", valuesToConvert, " top-level values to be present but only ", values.size(), " have been specified.")
1812 : argsToString("Expected ", valuesToConvert, " values for argument --", path.back()->name(), " to be present but only ", values.size(),
1813 " have been specified."));
1814}
1815
1816} // namespace CppUtilities
#define CPP_UTILITIES_IF_DEBUG_BUILD(x)
Wraps debug-only lines conveniently.
Definition global.h:102
The ArgumentParser class provides a means for handling command line arguments.
void checkConstraints()
Checks whether constraints are violated.
ArgumentParser()
Constructs a new ArgumentParser.
void readArgs(int argc, const char *const *argv)
Parses the specified command line arguments.
const ArgumentVector & mainArguments() const
Returns the main arguments.
bool isUncombinableMainArgPresent() const
Checks whether at least one uncombinable main argument is present.
void setMainArguments(const ArgumentInitializerList &mainArguments)
Sets the main arguments for the parser.
void printHelp(std::ostream &os) const
Prints help text for all assigned arguments.
void addMainArgument(Argument *argument)
Adds the specified argument to the main argument.
void parseArgs(int argc, const char *const *argv, ParseArgumentBehavior behavior=ParseArgumentBehavior::CheckConstraints|ParseArgumentBehavior::InvokeCallbacks|ParseArgumentBehavior::ExitOnFailure)
Parses the specified command line arguments.
void invokeCallbacks()
Invokes all assigned callbacks.
void resetArgs()
Resets all Argument instances assigned as mainArguments() and sub arguments.
Argument * specifiedOperation() const
Returns the first operation argument specified by the user or nullptr if no operation has been specif...
The ArgumentReader class internally encapsulates the process of reading command line arguments.
const char *const * lastArgDenotation
Points to the element in argv where lastArg was encountered. Unspecified if lastArg is not set.
const char * argDenotation
The currently processed abbreviation denotation (should be substring of one of the args in argv)....
ArgumentReader(ArgumentParser &parser, const char *const *argv, const char *const *end, bool completionMode=false)
Initializes the internal reader for the specified parser and arguments.
ArgumentVector & args
The Argument instances to store the results. Sub arguments of args are considered as well.
const char *const * argv
Points to the first argument denotation and will be incremented when a denotation has been processed.
Argument * lastArg
The last Argument instance which could be detected. Set to nullptr in the initial call....
bool completionMode
Whether completion mode is enabled. In this case reading args will be continued even if an denotation...
unsigned char argDenotationType
The type of the currently processed abbreviation denotation. Unspecified if argDenotation is not set.
std::size_t index
An index which is incremented when an argument is encountered (the current index is stored in the occ...
ArgumentReader & reset(const char *const *argv, const char *const *end)
Resets the ArgumentReader to continue reading new argv.
ArgumentParser & parser
The associated ArgumentParser instance.
bool read()
Reads the commands line arguments specified when constructing the object.
const char *const * end
Points to the end of the argv array.
The Argument class is a wrapper for command line argument information.
const char * description() const
Returns the description of the argument.
Argument(const char *name, char abbreviation='\0', const char *description=nullptr, const char *example=nullptr)
Constructs an Argument with the given name, abbreviation and description.
bool isParentPresent() const
Returns whether at least one parent argument is present.
void addSubArgument(Argument *arg)
Adds arg as a secondary argument for this argument.
static constexpr std::size_t varValueCount
Denotes a variable number of values.
const std::vector< Argument * > & path(std::size_t occurrence=0) const
Returns the path of the specified occurrence.
bool isMainArgument() const
Returns an indication whether the argument is used as main argument.
const char * firstValue() const
Returns the first parameter value of the first occurrence of the argument.
const ArgumentVector & subArguments() const
Returns the secondary arguments for this argument.
const char * example() const
Returns the usage example of the argument.
Flags
The Flags enum specifies options for treating the argument in a special way.
const std::vector< const char * > & valueNames() const
Returns the names of the required values.
void addSubArguments(const ArgumentInitializerList &subArguments)
Sets the secondary arguments for this argument.
ValueCompletionBehavior valueCompletionBehaviour() const
Returns the items to be considered when generating completion for the values.
char abbreviation() const
Returns the abbreviation of the argument.
std::size_t occurrences() const
Returns how often the argument could be detected when parsing.
Argument * specifiedOperation() const
Returns the first operation argument specified by the user or nullptr if no operation has been specif...
const char * name() const
Returns the name of the argument.
bool isCombinable() const
Returns an indication whether the argument is combinable.
bool denotesOperation() const
Returns whether the argument denotes an operation.
void resetRecursively()
Resets this argument and all sub arguments recursively.
std::size_t maxOccurrences() const
Returns the maximum number of occurrences.
Argument::Flags flags() const
Returns Argument::Flags for the argument.
const char * preDefinedCompletionValues() const
Returns the assigned values used when generating completion for the values.
std::size_t requiredValueCount() const
Returns the number of values which are required to be given for this argument.
const char * environmentVariable() const
Returns the environment variable queried when firstValue() is called.
Argument * wouldConflictWithArgument() const
Checks if this argument would conflict with other arguments if it was present.
void reset()
Resets occurrences (indices, values and paths).
~Argument()
Destroys the Argument.
bool isPresent() const
Returns an indication whether the argument could be detected when parsing.
Argument * conflictsWithArgument() const
Checks if this arguments conflicts with other arguments.
std::size_t minOccurrences() const
Returns the minimum number of occurrences.
bool allRequiredValuesPresent(std::size_t occurrence=0) const
Returns an indication whether all required values are present.
void setSubArguments(const ArgumentInitializerList &subArguments)
Sets the secondary arguments for this argument.
void setCallback(CallbackFunction callback)
Sets a callback function which will be called by the parser if the argument could be found and no par...
bool isRequired() const
Returns an indication whether the argument is mandatory.
void printInfo(std::ostream &os, unsigned char indentation=0) const
Writes the name, the abbreviation and other information about the Argument to the give ostream.
const char * firstValueOr(const char *fallback) const
Returns the first value like Argument::firstValue() but returns fallback instead of nullptr if there'...
The ConversionException class is thrown by the various conversion functions of this library when a co...
HelpArgument(ArgumentParser &parser)
Constructs a new help argument for the specified parser.
The Indentation class allows printing indentation conveniently, eg.
void apply() const
Sets EscapeCodes::enabled according to the presence of the first instantiation of NoColorArgument.
NoColorArgument()
Constructs a new NoColorArgument argument.
The ParseError class is thrown by an ArgumentParser when a parsing error occurs.
Definition parseerror.h:11
The Wrapper class is internally used print text which might needs to be wrapped preserving the indent...
#define CMD_UTILS_START_CONSOLE
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
Encapsulates functions for formatted terminal output using ANSI escape codes.
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
void setStyle(std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
Contains all utilities provides by the c++utilities library.
void findAndReplace(StringType1 &str, const StringType2 &find, const StringType3 &replace)
Replaces all occurrences of find with relpace in the specified str.
CPP_UTILITIES_EXPORT TerminalSize determineTerminalSize()
Returns the current size of the terminal.
std::initializer_list< Argument * > ArgumentInitializerList
Argument * firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except)
This function return the first present and uncombinable argument of the given list of arguments.
std::vector< Argument * > ArgumentVector
CPP_UTILITIES_EXPORT std::ostream & operator<<(std::ostream &out, Indentation indentation)
UnknownArgumentBehavior
The UnknownArgumentBehavior enum specifies the behavior of the argument parser when an unknown argume...
bool startsWith(const StringType &str, const StringType &phrase)
Returns whether str starts with phrase.
IntegralType stringToNumber(const StringType &string, BaseType base=10)
Converts the given string to an unsigned/signed number assuming string uses the specified base.
ParseArgumentBehavior
The ParseArgumentBehavior enum specifies the behavior when parsing arguments.
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition testutils.h:241
StringType argsToString(Args &&...args)
ValueCompletionBehavior
The ValueCompletionBehavior enum specifies the items to be considered when generating completion for ...
ArgumentDenotationType
The ArgumentDenotationType enum specifies the type of a given argument denotation.
CPP_UTILITIES_EXPORT std::optional< bool > isEnvVariableSet(const char *variableName)
Returns whether the specified env variable is set to a non-zero and non-white-space-only value.
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition math.h:88
void insertSiblings(const ArgumentVector &siblings, list< const Argument * > &target)
Inserts the specified siblings in the target list.
bool compareArgs(const Argument *arg1, const Argument *arg2)
Returns whether arg1 should be listed before arg2 when printing completion.
CPP_UTILITIES_EXPORT std::size_t computeDamerauLevenshteinDistance(const char *str1, std::size_t size1, const char *str2, std::size_t size2)
CPP_UTILITIES_EXPORT ApplicationInfo applicationInfo
Stores global application info used by ArgumentParser::printHelp() and AboutDialog.
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
Definition path.cpp:17
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
Definition path.cpp:25
STL namespace.
Stores information about an application.
std::vector< const char * > dependencyVersions
The ArgumentCompletionInfo struct holds information internally used for shell completion and suggesti...
list< const Argument * > relevantArgs
ArgumentCompletionInfo(const ArgumentReader &reader)
Constructs a new completion info for the specified reader.
list< const Argument * > relevantPreDefinedValues
The ArgumentOccurrence struct holds argument values for an occurrence of an argument.
std::vector< const char * > values
The parameter values which have been specified after the occurrence of the argument.
std::vector< Argument * > path
The "path" of the occurrence (the parent elements which have been specified before).
The TerminalSize struct describes a terminal size.
unsigned short columns
number of columns
constexpr int i