1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "servercmdparse.h" |
6 | |
7 | namespace newlsp { |
8 | |
9 | const QString port {"port" }; |
10 | const QString mode {"mode" }; |
11 | const QString parentPid {"parentPid" }; |
12 | const QString stdio {"stdio" }; |
13 | const QString tcp {"tcp" }; |
14 | const QString portDefault {"3307" }; |
15 | const QString parentPidDefault {"0" }; |
16 | |
17 | class ServerCmdParsePrivate |
18 | { |
19 | friend class ServerCmdParse; |
20 | QList<QCommandLineOption> options |
21 | { |
22 | { |
23 | parentPid, |
24 | "Server use timer to watch parent process id, " |
25 | "if process id no exist, well quit this program.\n" , |
26 | parentPidDefault |
27 | },{ |
28 | mode, |
29 | "Server user Server startup mode,can use " |
30 | "[" + stdio + "] [" + tcp +"], at default is stdio" , |
31 | stdio |
32 | },{ |
33 | port, |
34 | "The parameter passed during startup is valid only when the mode is tcp" , |
35 | portDefault |
36 | } |
37 | }; |
38 | }; |
39 | |
40 | ServerCmdParse::ServerCmdParse(const QCoreApplication &app) |
41 | : QCommandLineParser () |
42 | , d (new ServerCmdParsePrivate()) |
43 | { |
44 | bindOptions(); |
45 | QCommandLineParser::process(app); |
46 | } |
47 | |
48 | ServerCmdParse::~ServerCmdParse() |
49 | { |
50 | if (d) |
51 | delete d; |
52 | } |
53 | |
54 | std::optional<uint> ServerCmdParse::parentPid() const |
55 | { |
56 | std::optional<qint64> ret; |
57 | if (optionNames().contains(newlsp::parentPid)) { |
58 | ret = value(newlsp::parentPid).toUInt(); |
59 | } |
60 | return ret; |
61 | } |
62 | |
63 | std::optional<std::string> ServerCmdParse::mode() const |
64 | { |
65 | std::optional<std::string> ret; |
66 | if (optionNames().contains(newlsp::port)) { |
67 | ret = value(newlsp::port).toStdString(); |
68 | } |
69 | return ret; |
70 | } |
71 | |
72 | std::optional<uint> ServerCmdParse::port() const |
73 | { |
74 | std::optional<qint64> ret; |
75 | if (optionNames().contains(newlsp::port)) { |
76 | ret = value(newlsp::port).toUInt(); |
77 | } |
78 | return ret; |
79 | } |
80 | |
81 | void ServerCmdParse::bindOptions() |
82 | { |
83 | QCommandLineParser::addOptions(d->options); |
84 | } |
85 | |
86 | } // newlsp |
87 | |