Skip to content

Commit

Permalink
QCommandLineParser: include the positional arguments' sizes in --help
Browse files Browse the repository at this point in the history
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
Fixes: QTBUG-131716
Change-Id: Ib1eee62c7cf4462f6a26fffdec233ba849ebf158
Reviewed-by: David Faure <david.faure@kdab.com>
(cherry picked from commit 8928b0f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
  • Loading branch information
thiagomacieira authored and Qt Cherry-pick Bot committed Dec 19, 2024
1 parent f10b13e commit 55a46ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/corelib/tools/qcommandlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -160,14 +173,26 @@ 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);
QCOMPARE(parser.isSet("b"), expectedIsSet);
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()
Expand Down

0 comments on commit 55a46ec

Please sign in to comment.