| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2013 David Faure <faure@kde.org> | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <QDebug> | 
| 30 | #include <QCoreApplication> | 
| 31 | #include <QCommandLineParser> | 
| 32 |  | 
| 33 | #include <stdio.h> | 
| 34 |  | 
| 35 | int main(int argc, char *argv[]) | 
| 36 | { | 
| 37 |     QCoreApplication app(argc, argv); | 
| 38 |     app.setApplicationVersion("1.0" ); | 
| 39 |  | 
| 40 |     // Test for QCoreApplication::arguments() | 
| 41 |     const QStringList incomingArgs = QCoreApplication::arguments(); | 
| 42 |     for (int i = 0; i < argc; ++i) { | 
| 43 |         if (incomingArgs.at(i) != QLatin1String(argv[i])) | 
| 44 |             qDebug() << "ERROR: arguments["  << i << "] was"  << incomingArgs.at(i) << "expected"  << argv[i]; | 
| 45 |     } | 
| 46 |  | 
| 47 |     QCommandLineParser parser; | 
| 48 |     parser.setApplicationDescription("Test helper" ); | 
| 49 |     parser.addHelpOption(); | 
| 50 |     parser.addVersionOption(); | 
| 51 |     parser.addPositionalArgument(name: "parsingMode" , description: "The parsing mode to test." ); | 
| 52 |     parser.addPositionalArgument(name: "command" , description: "The command to execute." ); | 
| 53 |     parser.addOption(commandLineOption: QCommandLineOption("load" , "Load file from URL." , "url" )); | 
| 54 |     parser.addOption(commandLineOption: QCommandLineOption(QStringList() << "o"  << "output" , "Set output file." , "file" )); | 
| 55 |     parser.addOption(commandLineOption: QCommandLineOption("D" , "Define macro." , "key=value" )); | 
| 56 |     parser.addOption(commandLineOption: QCommandLineOption("long-option" )); | 
| 57 |  | 
| 58 |     // An option with a longer description, to test wrapping | 
| 59 |     QCommandLineOption noImplicitIncludesOption(QStringList() << QStringLiteral("n" ) << QStringLiteral("no-implicit-includes" )); | 
| 60 |     noImplicitIncludesOption.setDescription(QStringLiteral("Disable magic generation of implicit #include-directives." )); | 
| 61 |     parser.addOption(commandLineOption: noImplicitIncludesOption); | 
| 62 |  | 
| 63 |     QCommandLineOption newlineOption(QStringList() << QStringLiteral("newline" )); | 
| 64 |     newlineOption.setDescription(QString::fromLatin1(str: "This is an option with a rather long\n"  | 
| 65 |                 "description using explicit newline characters "  | 
| 66 |                 "(but testing automatic wrapping too). In addition, "  | 
| 67 |                 "here, we test breaking after a comma. Testing -option. "  | 
| 68 |                 "Long URL: http://qt-project.org/wiki/How_to_create_a_library_with_Qt_and_use_it_in_an_application "  | 
| 69 |                 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" )); | 
| 70 |     parser.addOption(commandLineOption: newlineOption); | 
| 71 |  | 
| 72 |     // A hidden option | 
| 73 |     QCommandLineOption hiddenOption(QStringList() << QStringLiteral("hidden" )); | 
| 74 |     hiddenOption.setDescription(QStringLiteral("THIS SHOULD NEVER APPEAR" )); | 
| 75 |     hiddenOption.setFlags(QCommandLineOption::HiddenFromHelp); | 
| 76 |     parser.addOption(commandLineOption: hiddenOption); | 
| 77 | #if QT_DEPRECATED_SINCE(5, 8) | 
| 78 |     QCommandLineOption hiddenOption2(QStringList() << QStringLiteral("hidden2" )); | 
| 79 |     hiddenOption2.setDescription(QStringLiteral("NEITHER SHOULD THIS" )); | 
| 80 |     hiddenOption2.setHidden(true); | 
| 81 |     parser.addOption(commandLineOption: hiddenOption2); | 
| 82 | #endif | 
| 83 |  | 
| 84 |     // This program supports different options depending on the "command" (first argument). | 
| 85 |     // Call parse() to find out the positional arguments. | 
| 86 |     parser.parse(arguments: QCoreApplication::arguments()); | 
| 87 |  | 
| 88 |     QStringList args = parser.positionalArguments(); | 
| 89 |     if (args.isEmpty()) | 
| 90 |         parser.showHelp(exitCode: 1); | 
| 91 |     parser.setSingleDashWordOptionMode(QCommandLineParser::SingleDashWordOptionMode(args.takeFirst().toInt())); | 
| 92 |     const QString command = args.isEmpty() ? QString() : args.first(); | 
| 93 |     if (command == "resize" ) { | 
| 94 |         parser.clearPositionalArguments(); | 
| 95 |         parser.addPositionalArgument(name: "resize" , description: "Resize the object to a new size." , syntax: "resize [resize_options]" ); | 
| 96 |         parser.addOption(commandLineOption: QCommandLineOption("size" , "New size." , "size" )); | 
| 97 |         parser.process(app); | 
| 98 |         const QString size = parser.value(name: "size" ); | 
| 99 |         printf(format: "Resizing %s to %s and saving to %s\n" , qPrintable(parser.value("load" )), qPrintable(size), qPrintable(parser.value("o" ))); | 
| 100 |     } else if (command == "long" ) { | 
| 101 |         // A very long option (QTBUG-79926) | 
| 102 |         QCommandLineOption longOption(QStringList{QStringLiteral("looooooooooooong-option" ), QStringLiteral("looooong-opt-alias" )}); | 
| 103 |         longOption.setDescription(QStringLiteral("Short description" )); | 
| 104 |         longOption.setValueName(QStringLiteral("looooooooooooong-value-name" )); | 
| 105 |         parser.addOption(commandLineOption: longOption); | 
| 106 |         parser.process(app); | 
| 107 |     } else { | 
| 108 |         // Call process again, to handle unknown options this time. | 
| 109 |         parser.process(app); | 
| 110 |     } | 
| 111 |  | 
| 112 |     printf(format: "Positional arguments: %s\n" , qPrintable(parser.positionalArguments().join("," ))); | 
| 113 |     printf(format: "Macros: %s\n" , qPrintable(parser.values("D" ).join("," ))); | 
| 114 |  | 
| 115 |     return 0; | 
| 116 | } | 
| 117 |  | 
| 118 |  |