QCommandLineParser: include the positional arguments' sizes in --help

We were mostly ignoring them because it looks like most people's options
were longer than their positional arguments. The rest must have just
accepted the enforced wrapping.

But if you have very short options like single-letter only ones or none
at all, the positional argument wrapping is unnecessarily short.

[ChangeLog][QtCore][QCommandLineParser] Made it so the positional
argument descriptions are taken into account in the aligning of text for
helpText().

Pick-to: 6.8 6.9
Fixes: QTBUG-131716
Change-Id: Ib1eee62c7cf4462f6a26fffdec233ba849ebf158
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Thiago Macieira 2024-11-28 09:12:00 -08:00
parent 5735d7ac86
commit 8928b0fbb9
2 changed files with 30 additions and 0 deletions

View File

@ -1159,6 +1159,7 @@ QString QCommandLineParserPrivate::helpText(bool includeQtOptions) const
text += nl;
if (!options.isEmpty())
text += QCommandLineParser::tr("Options:") + nl;
QStringList optionNameList;
optionNameList.reserve(options.size());
qsizetype longestOptionNameString = 0;
@ -1179,6 +1180,10 @@ QString QCommandLineParserPrivate::helpText(bool includeQtOptions) const
optionNameList.append(optionNamesString);
longestOptionNameString = qMax(longestOptionNameString, optionNamesString.size());
}
for (const PositionalArgumentDefinition &arg : positionalArgumentDefinitions)
longestOptionNameString = qMax(longestOptionNameString, arg.name.size());
++longestOptionNameString;
const int optionNameMaxWidth = qMin(50, int(longestOptionNameString));
auto optionNameIterator = optionNameList.cbegin();

View File

@ -102,8 +102,21 @@ void tst_QCommandLineParser::testPositionalArguments()
{
QCoreApplication app(empty_argc, empty_argv);
QCommandLineParser parser;
const QString exeName = QCoreApplication::instance()->arguments().first(); // e.g. debug\tst_qcommandlineparser.exe on Windows
QString expectedNoPositional =
"Usage: " + exeName + "\n"
"\n";
QCOMPARE(parser.helpText(), expectedNoPositional);
QVERIFY(parser.parse(QStringList() << "tst_qcommandlineparser" << "file.txt"));
QCOMPARE(parser.positionalArguments(), QStringList() << QStringLiteral("file.txt"));
parser.addPositionalArgument("file", "File names", "<foobar>");
QString expected =
"Usage: " + exeName + " <foobar>\n"
"\n"
"Arguments:\n"
" file File names\n";
}
void tst_QCommandLineParser::testBooleanOption_data()
@ -160,7 +173,18 @@ void tst_QCommandLineParser::testOptionsAndPositional()
QCoreApplication app(empty_argc, empty_argv);
QCommandLineParser parser;
const QString exeName = QCoreApplication::instance()->arguments().first(); // e.g. debug\tst_qcommandlineparser.exe on Windows
const QString expectedHelpText =
"Usage: " + exeName + " [options] input\n"
"\n"
"Options:\n"
" -b a boolean option\n"
"\n"
"Arguments:\n"
" input File names\n";
parser.setOptionsAfterPositionalArgumentsMode(parsingMode);
parser.addPositionalArgument("input", "File names");
QVERIFY(parser.addOption(QCommandLineOption(QStringLiteral("b"), QStringLiteral("a boolean option"))));
QVERIFY(parser.parse(args));
QCOMPARE(parser.optionNames(), expectedOptionNames);
@ -168,6 +192,7 @@ void tst_QCommandLineParser::testOptionsAndPositional()
QTest::ignoreMessage(QtWarningMsg, "QCommandLineParser: option not expecting values: \"b\"");
QCOMPARE(parser.values("b"), QStringList());
QCOMPARE(parser.positionalArguments(), expectedPositionalArguments);
QCOMPARE(parser.helpText(), expectedHelpText);
}
void tst_QCommandLineParser::testMultipleNames_data()