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
7namespace newlsp {
8
9const QString port {"port"};
10const QString mode {"mode"};
11const QString parentPid {"parentPid"};
12const QString stdio {"stdio"};
13const QString tcp {"tcp"};
14const QString portDefault {"3307"};
15const QString parentPidDefault {"0"};
16
17class 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
40ServerCmdParse::ServerCmdParse(const QCoreApplication &app)
41 : QCommandLineParser ()
42 , d (new ServerCmdParsePrivate())
43{
44 bindOptions();
45 QCommandLineParser::process(app);
46}
47
48ServerCmdParse::~ServerCmdParse()
49{
50 if (d)
51 delete d;
52}
53
54std::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
63std::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
72std::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
81void ServerCmdParse::bindOptions()
82{
83 QCommandLineParser::addOptions(d->options);
84}
85
86} // newlsp
87