20#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
25using namespace std::placeholders;
26using namespace std::literals;
66 : lastDetectedArg(reader.lastArg)
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;
78 const char *
const suggestion;
79 const size_t suggestionSize;
80 const size_t editingDistance;
81 const bool hasDashPrefix;
84ArgumentSuggestion::ArgumentSuggestion(
const char *unknownArg,
size_t unknownArgSize,
const char *suggestion,
size_t suggestionSize,
bool isOperation)
85 : suggestion(suggestion)
86 , suggestionSize(suggestionSize)
88 , hasDashPrefix(isOperation)
92ArgumentSuggestion::ArgumentSuggestion(
const char *unknownArg,
size_t unknownArgSize,
const char *suggestion,
bool isOperation)
93 : ArgumentSuggestion(unknownArg, unknownArgSize, suggestion, strlen(suggestion), isOperation)
97bool ArgumentSuggestion::operator<(
const ArgumentSuggestion &other)
const
99 return editingDistance < other.editingDistance;
102void ArgumentSuggestion::addTo(multiset<ArgumentSuggestion> &suggestions,
size_t limit)
const
104 if (suggestions.size() >= limit && !(*
this < *--suggestions.end())) {
107 suggestions.emplace(*
this);
108 while (suggestions.size() > limit) {
109 suggestions.erase(--suggestions.end());
128 , args(parser.m_mainArgs)
133 , argDenotation(nullptr)
134 , completionMode(completionMode)
163bool Argument::matchesDenotation(
const char *denotation,
size_t denotationLength)
const
165 return m_name && !strncmp(m_name, denotation, denotationLength) && *(m_name + denotationLength) ==
'\0';
180 const vector<Argument *> &parentPath = parentArg ? parentArg->
path(parentArg->
occurrences() - 1) : vector<Argument *>();
183 vector<const char *> *values =
nullptr;
199 bool abbreviationFound =
false;
202 abbreviationFound =
false;
214 abbreviationFound =
false;
235 for (; argDenotationLength; matchingArg =
nullptr) {
239 if (arg->abbreviation() && arg->abbreviation() == *
argDenotation) {
241 abbreviationFound =
true;
247 if (arg->matchesDenotation(
argDenotation, argDenotationLength)) {
258 matchingArg->m_occurrences.emplace_back(
index, parentPath, parentArg);
261 values = &matchingArg->m_occurrences.back().values;
265 values->push_back(equationPos + 1);
272 lastArg = lastArgInLevel = matchingArg;
283 const char *
const *
const currentArgValue =
argv;
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()) {
308 if (sibling->matchesDenotation(
argDenotation, argDenotationLength)) {
313 if (parentArgument == pathEnd) {
330 if (arg->denotesOperation() && arg->name() && !strcmp(arg->name(), *
argv)) {
331 (matchingArg = arg)->m_occurrences.emplace_back(
index, parentPath, parentArg);
343 if (arg->isImplicit() && !arg->isPresent() && !arg->wouldConflictWithArgument()
344 && (!uncombinableMainArgPresent || !arg->isMainArgument())) {
345 (matchingArg = arg)->m_occurrences.emplace_back(
index, parentPath, parentArg);
353 if (lastArgInLevel == matchingArg) {
358 values = &matchingArg->m_occurrences.back().values;
362 lastArg = lastArgInLevel = matchingArg;
383 switch (
parser.m_unknownArgBehavior) {
385 cerr << Phrases::Warning <<
"The specified argument \"" << *
argv <<
"\" is unknown and will be ignored." << Phrases::EndFlush;
411 const auto maxColumns = termSize.
columns ? termSize.
columns : numeric_limits<unsigned short>::max();
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') {
421 if (wrapper.m_indentation.
level < maxColumns) {
422 os << wrapper.m_indentation;
423 currentCol = wrapper.m_indentation.
level;
428 if (*currentChar !=
'\n' && (!wrappingRequired || *currentChar !=
' ')) {
440inline bool notEmpty(
const char *str)
465 , m_abbreviation(abbreviation)
466 , m_environmentVar(nullptr)
467 , m_description(description)
469 , m_minOccurrences(0)
470 , m_maxOccurrences(1)
471 , m_requiredValueCount(0)
473 , m_deprecatedBy(nullptr)
477 , m_preDefinedCompletionValues(nullptr)
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);
529 if (notEmpty(
name())) {
543 unsigned int valueNamesPrint = 0;
545 os <<
' ' <<
'[' << *
i <<
']';
552 os <<
" [value " << (valueNamesPrint + 1) <<
']';
561 os <<
'\n' << ident <<
"particularities: mandatory";
563 os <<
" if parent argument is present";
570 bool hasSubArgs =
false;
572 if (arg->isDeprecated()) {
576 arg->printInfo(os, ident.
level);
579 if (ident.
level == 2 && hasSubArgs) {
595 if (arg != except && arg->
isPresent() && !arg->isCombinable()) {
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());
646 const auto requiredCap = m_subArgs.size() +
subArguments.size();
647 if (requiredCap < m_subArgs.capacity()) {
648 m_subArgs.reserve(requiredCap);
653 if (find(arg->m_parents.cbegin(), arg->m_parents.cend(),
this) == arg->m_parents.cend()) {
654 arg->m_parents.push_back(
this);
668 if (find(m_subArgs.cbegin(), m_subArgs.cend(), arg) != m_subArgs.cend()) {
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);
686 for (
const Argument *parent : m_parents) {
687 if (parent->isPresent()) {
720 for (
Argument *parent : m_parents) {
721 for (
Argument *sibling : parent->subArguments()) {
722 if (sibling !=
this && sibling->isPresent() && !sibling->isCombinable()) {
737 if (arg->denotesOperation() && arg->isPresent()) {
774 , m_executable(nullptr)
776 , m_defaultArg(nullptr)
797 arg->m_isMainArg =
true;
803 bool subArgsRequired =
false;
805 if (subArg->isRequired()) {
806 subArgsRequired =
true;
810 if (!subArgsRequired) {
823 argument->m_isMainArg =
true;
824 m_mainArgs.push_back(argument);
833 bool wroteLine =
false;
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;
866 if (hasOperations && hasTopLevelOptions) {
874 os <<
"Available operations:";
875 for (
const Argument *
const arg : m_mainArgs) {
876 if (!arg->denotesOperation() || arg->isDeprecated() || !strcmp(arg->name(),
"help")) {
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")) {
894 os <<
"Available arguments:";
895 for (
const Argument *
const arg : m_mainArgs) {
896 if (arg->isDeprecated() || !strcmp(arg->name(),
"help")) {
908 os <<
"Linked against: " << *
i;
909 for (++
i;
i != end; ++
i) {
910 os <<
',' <<
' ' << *
i;
953 invokeExit(EXIT_FAILURE);
979 m_executable =
nullptr;
982 m_executable = *argv;
988 m_defaultArg->m_occurrences.emplace_back(0);
994 const bool completionMode = !strcmp(*++argv,
"--bash-completion-for");
997 unsigned int currentWordIndex = 0, argcForReader;
998 if (completionMode) {
1017 m_noColorArg.
apply();
1026 if (completionMode) {
1039 arg->resetRecursively();
1052 if (
arg->denotesOperation() &&
arg->isPresent()) {
1065 if (!
arg->isCombinable() &&
arg->isPresent()) {
1072#ifdef CPP_UTILITIES_DEBUG_BUILD
1093 vector<const char *>
names;
1122 if (
arg1->denotesOperation() && !
arg2->denotesOperation()) {
1124 }
else if (!
arg1->denotesOperation() &&
arg2->denotesOperation()) {
1161 completion.lastDetectedArgIndex =
static_cast<size_t>(
reader.lastArgDenotation - argv);
1167 completion.lastSpecifiedArgIndex =
static_cast<unsigned int>(
argc) - 1;
1217 if (
child->isImplicit() &&
child->requiredValueCount()) {
1232 >=
completion.lastDetectedArg->requiredValueCount()) {
1235 if (
subArg->occurrences() <
subArg->maxOccurrences()) {
1281 if (!
arg->preDefinedCompletionValues()) {
1284 for (
const char *
i =
arg->preDefinedCompletionValues(); *
i; ++
i) {
1329static std::string unescape(std::string_view
escaped)
1360 const char *
opening =
nullptr;
1401 cout <<
"COMPREPLY=(";
1408 if (!
arg->preDefinedCompletionValues()) {
1423 if ((
ok = (
i2 == end))) {
1428 }
else if ((
wordStart = (*
i ==
' ') || (*
i ==
'\n'))) {
1431 cout <<
'\'' <<
' ';
1435 }
else if (*
i ==
'=') {
1466 }
else if (
const char *
i =
arg->preDefinedCompletionValues()) {
1489 cout <<
' ' <<
'\'';
1494 cout <<
'\'' <<
' ';
1517 cout <<
'\'' <<
'-' <<
opening <<
arg->abbreviation() <<
'\'' <<
' ';
1520 cout <<
'\'' << *(
reader.argv - 1) <<
'\'' <<
' ';
1522 }
else if (
arg->denotesOperation()) {
1523 cout <<
'\'' <<
arg->name() <<
'\'' <<
' ';
1525 cout <<
'\'' <<
'-' <<
'-' <<
arg->name() <<
'\'' <<
' ';
1540 if (
dir[0] ==
'\"' ||
dir[0] ==
'\'') {
1543 if (
dir.size() > 1 && (
dir[
dir.size() - 2] ==
'\"' ||
dir[
dir.size() - 2] ==
'\'')) {
1544 dir.erase(
dir.size() - 2, 1);
1550 if (
file[0] ==
'\"' ||
file[0] ==
'\'') {
1560#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
1563 const auto replace =
"'"s,
with =
"'\"'\"'"s;
1566 filesystem::directory_iterator
i;
1568 i = filesystem::directory_iterator(
actualDir);
1571 i = filesystem::directory_iterator(
".");
1601 }
catch (
const filesystem::filesystem_error &) {
1610 cout <<
"; compopt -o filenames";
1615 cout <<
"; compopt -o nospace";
1628 const auto occurrences =
arg->occurrences();
1629 if (
arg->isParentPresent() && occurrences >
arg->maxOccurrences()) {
1631 (
arg->maxOccurrences() == 1 ?
" time." :
" times.")));
1635 (
arg->minOccurrences() == 1 ?
" time." :
" times.")));
1638 if (
arg->isMainArgument()) {
1639 if (!
arg->isCombinable() &&
arg->isPresent()) {
1648 for (
size_t i = 0;
i != occurrences; ++
i) {
1649 if (
arg->allRequiredValuesPresent(
i)) {
1652 stringstream
ss(stringstream::in | stringstream::out);
1653 ss <<
"Not all parameters for argument \"" <<
arg->name() <<
"\" ";
1655 ss <<
" (" << (
i + 1) <<
" occurrence) ";
1657 ss <<
"provided. You have to provide the following parameters:";
1659 for (
const auto &name :
arg->m_valueNames) {
1687 if (
arg->m_callbackFunction) {
1700void ArgumentParser::invokeExit(
int code)
1702 if (m_exitFunction) {
1703 m_exitFunction(
code);
1719 :
Argument(
"help",
'h',
"shows this information")
1762#ifdef CPP_UTILITIES_ESCAPE_CODES_ENABLED_BY_DEFAULT
1763 :
Argument(
"no-color",
'\0',
"disables formatted/colorized output")
1765 :
Argument(
"enable-color",
'\0',
"enables formatted/colorized output")
1768 setCombinable(
true);
1771 setEnvironmentVariable(
"ENABLE_ESCAPE_CODES");
1786#ifdef CPP_UTILITIES_ESCAPE_CODES_ENABLED_BY_DEFAULT
1797void ValueConversion::Helper::ArgumentValueConversionError::throwFailure(
const std::vector<Argument *> &
argumentPath)
const
1808void ArgumentOccurrence::throwNumberOfValuesNotSufficient(
unsigned long valuesToConvert)
const
1813 " have been specified."));
#define CPP_UTILITIES_IF_DEBUG_BUILD(x)
Wraps debug-only lines conveniently.
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.
bool isDeprecated() const
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.
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.
Argument::Flags flags() const
Returns Argument::Flags for the argument.
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.
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.
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.
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.
StringType argsToString(Args &&...args)
ValueCompletionBehavior
The ValueCompletionBehavior enum specifies the items to be considered when generating completion for ...
@ FileSystemIfNoPreDefinedValues
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.
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.
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
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
size_t lastDetectedArgIndex
ArgumentCompletionInfo(const ArgumentReader &reader)
Constructs a new completion info for the specified reader.
const char *const * lastSpecifiedArg
const Argument *const lastDetectedArg
vector< Argument * > lastDetectedArgPath
unsigned int lastSpecifiedArgIndex
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