Util+Test: Add support for '--' to enable non-option mode

This commit is contained in:
Riyyi
2021-09-05 23:39:08 +02:00
parent 04482fb10d
commit a5c422a2cc
3 changed files with 63 additions and 2 deletions
+11 -2
View File
@@ -204,6 +204,9 @@ bool ArgParser::parse(int argc, const char* argv[])
{
bool result = true;
// By default parse all '-' prefixed parameters as options
m_nonOptionMode = false;
// Get program name
m_name = argv[0] + std::string_view(argv[0]).find_last_of('/') + 1;
@@ -214,6 +217,7 @@ bool ArgParser::parse(int argc, const char* argv[])
for (; m_optionIndex < argc; ++m_optionIndex) {
printf("argv[%d]: %s\n", m_optionIndex, argv[m_optionIndex]);
// Get the current and next parameter
argument = argv[m_optionIndex];
if (m_optionIndex + 1 < argc && argv[m_optionIndex + 1][0] != '-') {
next = argv[m_optionIndex + 1];
@@ -222,15 +226,20 @@ bool ArgParser::parse(int argc, const char* argv[])
next = {};
}
// Stop parsing '-' prefixed parameters as options afer '--'
if (argument.compare("--") == 0) {
m_nonOptionMode = true;
}
// Long Option
if (argument[0] == '-' && argument[1] == '-') {
if (!m_nonOptionMode && argument[0] == '-' && argument[1] == '-') {
argument = argument.substr(argument.find_first_not_of('-'));
if (!parseLongOption(argument, next)) {
result = false;
}
}
// Short Option
else if (argument[0] == '-') {
else if (!m_nonOptionMode && argument[0] == '-') {
argument = argument.substr(argument.find_first_not_of('-'));
if (!parseShortOption(argument, next)) {
result = false;
+1
View File
@@ -60,6 +60,7 @@ private:
bool parseLongOption(std::string_view option, std::string_view next);
int m_optionIndex { 1 };
bool m_nonOptionMode { false };
bool m_exitOnFirstError { true };
bool m_errorMessages { true };
// TODO: Implement this, maybe combine with error messages flag, enum class? or bitfield