From 01a6760c54358cf1c9422a1c64801fa819351ea6 Mon Sep 17 00:00:00 2001 From: Arttu Tarkiainen Date: Wed, 21 Sep 2022 12:27:56 +0300 Subject: CLI: add support for hiding values of printed options Some options may require values that contain information that should not be echoed. Task-number: QTIFW-2756 Change-Id: I84ee7fd0f27ef68a2b3e886bd639c836835a52b6 Reviewed-by: Katja Marttila --- src/libs/installer/commandlineparser.cpp | 39 ++++++++++++++++++++++++++++++++ src/libs/installer/commandlineparser.h | 6 ++++- 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'src/libs') diff --git a/src/libs/installer/commandlineparser.cpp b/src/libs/installer/commandlineparser.cpp index 8585379ad..dc3497875 100644 --- a/src/libs/installer/commandlineparser.cpp +++ b/src/libs/installer/commandlineparser.cpp @@ -287,3 +287,42 @@ CommandLineParser::OptionContextFlags CommandLineParser::optionContextFlags(cons { return m_optionContextFlagsNameHash.value(option); } + +/* + Returns the command line arguments of the application. The returned list + is context-aware, i.e. options that are set on the parser with + \c OptionContextFlag::NoEchoValue are returned with their value hidden. +*/ +QStringList CommandLineParser::arguments() const +{ + const QStringList arguments = QCoreApplication::arguments(); + QStringList returnArguments; + bool skipNext = false; + for (const QString &arg : arguments) { + if (skipNext) { + skipNext = false; + continue; + } + returnArguments << arg; + // Append positional arguments as-is + if (!arg.startsWith(QLatin1String("--")) && !arg.startsWith(QLatin1Char('-'))) + continue; + + QString normalizedOption = arg; + while (normalizedOption.startsWith(QLatin1Char('-'))) + normalizedOption.remove(QLatin1Char('-')); + + const OptionContextFlags flags = optionContextFlags(normalizedOption); + if (!flags.testFlag(OptionContextFlag::NoEchoValue)) + continue; + + QString nextArg = arguments.value(arguments.indexOf(arg) + 1); + if (!nextArg.isEmpty() && !nextArg.startsWith(QLatin1String("--")) + && !nextArg.startsWith(QLatin1Char('-'))) { + nextArg = QLatin1String("******"); + returnArguments << nextArg; + skipNext = true; + } + } + return returnArguments; +} diff --git a/src/libs/installer/commandlineparser.h b/src/libs/installer/commandlineparser.h index 4511969cd..3c14d9f45 100644 --- a/src/libs/installer/commandlineparser.h +++ b/src/libs/installer/commandlineparser.h @@ -37,7 +37,8 @@ class CommandLineParser { public: enum OptionContextFlag { - CommandLineOnly = 0x1 + CommandLineOnly = 0x1, + NoEchoValue = 0x2 }; Q_DECLARE_FLAGS(OptionContextFlags, OptionContextFlag) @@ -56,6 +57,7 @@ public: QStringList optionNames() const { return m_parser.optionNames(); } OptionContextFlags optionContextFlags(const QString &option) const; + QStringList arguments() const; private: QCommandLineParser m_parser; @@ -64,4 +66,6 @@ private: QHash m_optionContextFlagsNameHash; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(CommandLineParser::OptionContextFlags) + #endif // COMMANDLINEPARSER_H -- cgit v1.2.3