1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "commandparser.h" |
6 | #include "util/custompaths.h" |
7 | |
8 | #include <QCoreApplication> |
9 | #include <QCommandLineParser> |
10 | #include <QCommandLineOption> |
11 | #include <QDebug> |
12 | #include <QDir> |
13 | |
14 | #include <iostream> |
15 | |
16 | CommandParser &CommandParser::instance() |
17 | { |
18 | static CommandParser ins; |
19 | return ins; |
20 | } |
21 | |
22 | bool CommandParser::isSet(const QString &name) const |
23 | { |
24 | return commandParser->isSet(name); |
25 | } |
26 | |
27 | QString CommandParser::value(const QString &name) const |
28 | { |
29 | return commandParser->value(name); |
30 | } |
31 | |
32 | void CommandParser::process() |
33 | { |
34 | return process(qApp->arguments()); |
35 | } |
36 | |
37 | void CommandParser::process(const QStringList &arguments) |
38 | { |
39 | qDebug() << "App start args: " << arguments; |
40 | commandParser->process(arguments); |
41 | } |
42 | |
43 | void CommandParser::setModel(CommandParser::CommandModel model) |
44 | { |
45 | commandModel = model; |
46 | } |
47 | |
48 | CommandParser::CommandModel CommandParser::getModel() |
49 | { |
50 | return commandModel; |
51 | } |
52 | |
53 | bool CommandParser::isBuildModel() |
54 | { |
55 | if (isSet("b" ) || isSet("k" ) || isSet("a" ) || isSet("d" ) || isSet("t" )) |
56 | return true; |
57 | return false; |
58 | } |
59 | |
60 | void CommandParser::initialize() |
61 | { |
62 | commandParser->setApplicationDescription(QString("%1 helper" ).arg(QCoreApplication::applicationName())); |
63 | initOptions(); |
64 | commandParser->addHelpOption(); |
65 | commandParser->addVersionOption(); |
66 | } |
67 | |
68 | void CommandParser::initOptions() |
69 | { |
70 | QCommandLineOption buildOption(QStringList() |
71 | << "b" |
72 | << "build" , |
73 | "Build with deepin-unioncode(won't work with empty directory)." , |
74 | "source directory" ); |
75 | QCommandLineOption destOption(QStringList() |
76 | << "d" |
77 | << "destination" , |
78 | "Build destination directory to store compiled executable files." , |
79 | "destination directory" ); |
80 | QCommandLineOption kitOption(QStringList() |
81 | << "k" |
82 | << "kit {CMake,Gradle,Maven,Ninja}" , |
83 | "Select build kit to build project.Support cmake,gradle,maven,ninja.It is CMake by default." , |
84 | "name" , "CMake" ); |
85 | QCommandLineOption argsOption(QStringList() |
86 | << "a" |
87 | << "arguments" , |
88 | "Input argument to use kit to build project(eg. -a \"--build . --target all\")." , |
89 | "argument list" ); |
90 | QCommandLineOption addTagOption(QStringList() |
91 | << "t" |
92 | << "tag" , |
93 | "Add tag to binary file.Input a file path which contain the tag content." |
94 | "It is deepin-unioncode.elf by default" , |
95 | "file path" ); |
96 | addOption(buildOption); |
97 | addOption(destOption); |
98 | addOption(kitOption); |
99 | addOption(argsOption); |
100 | addOption(addTagOption); |
101 | } |
102 | |
103 | void CommandParser::addOption(const QCommandLineOption &option) |
104 | { |
105 | commandParser->addOption(option); |
106 | } |
107 | |
108 | QStringList CommandParser::positionalArguments() const |
109 | { |
110 | return commandParser->positionalArguments(); |
111 | } |
112 | |
113 | QStringList CommandParser::unknownOptionNames() const |
114 | { |
115 | return commandParser->unknownOptionNames(); |
116 | } |
117 | |
118 | CommandParser::CommandParser(QObject *parent) |
119 | : QObject(parent), |
120 | commandParser(new QCommandLineParser) |
121 | { |
122 | initialize(); |
123 | } |
124 | |