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().

Fixes: QTBUG-131716
Change-Id: Ib1eee62c7cf4462f6a26fffdec233ba849ebf158
Reviewed-by: David Faure <david.faure@kdab.com>
(cherry picked from commit 8928b0fbb9ca4caf9b63a32b3d2a73a6da096755)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 55a46ec005894394031efbee1b9c2269bb1d8eea)
This commit is contained in:
Thiago Macieira 2024-11-28 09:12:00 -08:00 committed by Qt Cherry-pick Bot
parent eaaab38c3a
commit d490a1ac09
2 changed files with 30 additions and 0 deletions

View File

@ -1122,6 +1122,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;
@ -1142,6 +1143,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

@ -101,8 +101,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()
@ -159,7 +172,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);
@ -167,6 +191,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()