diff options
author | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2022-09-21 12:27:56 +0300 |
---|---|---|
committer | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2022-09-22 09:16:23 +0300 |
commit | 01a6760c54358cf1c9422a1c64801fa819351ea6 (patch) | |
tree | 678cd7e4672c4ae4b4dbe7ccd928dea887df8f44 /src/libs | |
parent | 86c68384cbf197dae3c6583c7b752eb21c75e34c (diff) |
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 <katja.marttila@qt.io>
Diffstat (limited to 'src/libs')
-rw-r--r-- | src/libs/installer/commandlineparser.cpp | 39 | ||||
-rw-r--r-- | src/libs/installer/commandlineparser.h | 6 |
2 files changed, 44 insertions, 1 deletions
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<QString, OptionContextFlags> m_optionContextFlagsNameHash; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(CommandLineParser::OptionContextFlags) + #endif // COMMANDLINEPARSER_H |