refactor: Handle Name option in CoreArgParser
This commit is contained in:
parent
ead49c4025
commit
05f377e21b
13 changed files with 42 additions and 36 deletions
|
|
@ -132,8 +132,6 @@ constexpr static auto s_helpGeneralArgs = //
|
|||
" -d, --debug <level> filter out log messages with priority below level.\n"
|
||||
" level may be: FATAL, ERROR, WARNING, NOTE, INFO,\n"
|
||||
" DEBUG, DEBUG1, DEBUG2.\n"
|
||||
" -n, --name <screen-name> use screen-name instead the hostname to identify\n"
|
||||
" this screen in the configuration.\n"
|
||||
" -1, --no-restart do not try to restart on failure.\n"
|
||||
"* --restart restart the server automatically if it fails.\n"
|
||||
" -l --log <file> write log messages to file.\n"
|
||||
|
|
@ -144,7 +142,6 @@ constexpr static auto s_helpVersionArgs = //
|
|||
" -h, --help display this help and exit.\n";
|
||||
|
||||
constexpr static auto s_helpCommonArgs = //
|
||||
" [--name <screen-name>]"
|
||||
" [--restart|--no-restart]"
|
||||
" [--debug <level>]";
|
||||
|
||||
|
|
|
|||
|
|
@ -127,9 +127,6 @@ bool ArgParser::parseGenericArgs(int argc, const char *const *argv, int &i) cons
|
|||
argsBase().m_logFilter = argv[++i];
|
||||
} else if (isArg(i, argc, argv, "-l", "--log", 1)) {
|
||||
argsBase().m_logFile = argv[++i];
|
||||
} else if (isArg(i, argc, argv, "-n", "--name", 1)) {
|
||||
// save screen name
|
||||
argsBase().m_name = argv[++i];
|
||||
} else if (isArg(i, argc, argv, "-1", "--no-restart")) {
|
||||
// don't try to restart
|
||||
argsBase().m_restartable = false;
|
||||
|
|
@ -320,7 +317,6 @@ std::string ArgParser::assembleCommand(
|
|||
|
||||
void ArgParser::updateCommonArgs(const char *const *argv) const
|
||||
{
|
||||
argsBase().m_name = QSysInfo::machineHostName().toStdString();
|
||||
argsBase().m_pname = QFileInfo(argv[0]).fileName().toLocal8Bit().constData();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,9 +50,6 @@ public:
|
|||
/// @brief Contains the X-Server display to use
|
||||
const char *m_display = nullptr;
|
||||
|
||||
/// @brief The name of the current computer
|
||||
std::string m_name;
|
||||
|
||||
/// @brief Will cause the application to exit with OK code when set to true
|
||||
bool m_shouldExitOk = false;
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
#include "client/Client.h"
|
||||
#include "common/Constants.h"
|
||||
#include "common/ExitCodes.h"
|
||||
#include "common/Settings.h"
|
||||
#include "deskflow/ArgParser.h"
|
||||
#include "deskflow/ClientArgs.h"
|
||||
#include "deskflow/ProtocolTypes.h"
|
||||
|
|
@ -319,7 +320,9 @@ bool ClientApp::startClient()
|
|||
try {
|
||||
if (m_clientScreen == nullptr) {
|
||||
clientScreen = openClientScreen();
|
||||
m_client = openClient(args().m_name, *m_serverAddress, clientScreen);
|
||||
m_client = openClient(
|
||||
Settings::value(Settings::Core::ScreenName).toString().toStdString(), *m_serverAddress, clientScreen
|
||||
);
|
||||
m_clientScreen = clientScreen;
|
||||
LOG_NOTE("started client");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,12 +17,13 @@ const QString CoreArgParser::s_headerText = QStringLiteral("%1: %2\n").arg(s_app
|
|||
|
||||
CoreArgParser::CoreArgParser(const QStringList &args)
|
||||
{
|
||||
m_parser.setApplicationDescription(kAppDescription);
|
||||
m_parser.addPositionalArgument("coremode", "The mode to start in either: server or client", "coremode");
|
||||
|
||||
m_parser.addOptions(CoreArgs::options);
|
||||
m_parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
|
||||
m_parser.setOptionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsOptions);
|
||||
m_parser.parse(args);
|
||||
m_parser.setApplicationDescription(kAppDescription);
|
||||
|
||||
m_helpText = m_parser.helpText().replace("<executable_name>", s_appName);
|
||||
m_helpText.replace("[options] coremode", "coremode [options]");
|
||||
|
|
@ -56,6 +57,10 @@ void CoreArgParser::parse()
|
|||
if (m_parser.isSet(CoreArgs::portOption)) {
|
||||
Settings::setValue(Settings::Core::Port, m_parser.value(CoreArgs::portOption));
|
||||
}
|
||||
|
||||
if (m_parser.isSet(CoreArgs::nameOption)) {
|
||||
Settings::setValue(Settings::Core::ScreenName, m_parser.value(CoreArgs::nameOption));
|
||||
}
|
||||
}
|
||||
|
||||
[[noreturn]] void CoreArgParser::showHelpText() const
|
||||
|
|
|
|||
|
|
@ -26,5 +26,8 @@ struct CoreArgs
|
|||
);
|
||||
inline static const auto portOption =
|
||||
QCommandLineOption({"p", "port"}, "Port to use in place of default port", "port");
|
||||
inline static const auto options = {helpOption, versionOption, configOption, interfaceOption, portOption};
|
||||
inline static const auto nameOption =
|
||||
QCommandLineOption({"n", "name"}, "use screen-name instead the hostname to identify this screen", "screen-name");
|
||||
|
||||
inline static const auto options = {helpOption, versionOption, configOption, interfaceOption, portOption, nameOption};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -71,6 +71,7 @@ using namespace deskflow::server;
|
|||
|
||||
ServerApp::ServerApp(IEventQueue *events) : App(events, new deskflow::ServerArgs())
|
||||
{
|
||||
m_name = Settings::value(Settings::Core::ScreenName).toString().toStdString();
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
|
@ -351,7 +352,7 @@ bool ServerApp::initServer()
|
|||
deskflow::Screen *serverScreen = nullptr;
|
||||
PrimaryClient *primaryClient = nullptr;
|
||||
try {
|
||||
std::string name = args().m_config->getCanonicalName(args().m_name);
|
||||
std::string name = args().m_config->getCanonicalName(m_name);
|
||||
serverScreen = openServerScreen();
|
||||
primaryClient = openPrimaryClient(name, serverScreen);
|
||||
m_serverScreen = serverScreen;
|
||||
|
|
@ -570,7 +571,7 @@ int ServerApp::mainLoop()
|
|||
// if configuration has no screens then add this system
|
||||
// as the default
|
||||
if (args().m_config->begin() == args().m_config->end()) {
|
||||
args().m_config->addScreen(args().m_name);
|
||||
args().m_config->addScreen(m_name);
|
||||
}
|
||||
|
||||
// set the contact address, if provided, in the config.
|
||||
|
|
@ -583,8 +584,8 @@ int ServerApp::mainLoop()
|
|||
}
|
||||
|
||||
// canonicalize the primary screen name
|
||||
if (std::string primaryName = args().m_config->getCanonicalName(args().m_name); primaryName.empty()) {
|
||||
LOG_CRIT("unknown screen name `%s'", args().m_name.c_str());
|
||||
if (std::string primaryName = args().m_config->getCanonicalName(m_name); primaryName.empty()) {
|
||||
LOG_CRIT("unknown screen name `%s'", m_name.c_str());
|
||||
return s_exitFailed;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -133,4 +133,5 @@ private:
|
|||
ClientListener *m_listener = nullptr;
|
||||
EventQueueTimer *m_timer = nullptr;
|
||||
NetworkAddress *m_deskflowAddress = nullptr;
|
||||
std::string m_name;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -458,8 +458,6 @@ bool CoreProcess::addGenericArgs(QStringList &args) const
|
|||
{
|
||||
args << "--debug" << Settings::logLevelText();
|
||||
|
||||
args << "--name" << Settings::value(Settings::Core::ScreenName).toString();
|
||||
|
||||
if (Settings::value(Settings::Security::TlsEnabled).toBool()) {
|
||||
args << "--enable-crypto";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -326,22 +326,6 @@ void ArgParserTests::generic_logFileWithSpace()
|
|||
QCOMPARE(i, 2);
|
||||
}
|
||||
|
||||
void ArgParserTests::generic_name()
|
||||
{
|
||||
int i = 1;
|
||||
const int argc = 3;
|
||||
const char *kNameCmd[argc] = {"stub", "--name", "mock"};
|
||||
// Somehow cause a dump if not made here.
|
||||
ArgParser parser(nullptr);
|
||||
static deskflow::ArgsBase base;
|
||||
|
||||
parser.setArgsBase(base);
|
||||
parser.parseGenericArgs(argc, kNameCmd, i);
|
||||
|
||||
QCOMPARE(base.m_name, "mock");
|
||||
QCOMPARE(i, 2);
|
||||
}
|
||||
|
||||
void ArgParserTests::generic_noRestart()
|
||||
{
|
||||
int i = 1;
|
||||
|
|
|
|||
|
|
@ -36,7 +36,6 @@ private Q_SLOTS:
|
|||
void generic_logLevel();
|
||||
void generic_logFile();
|
||||
void generic_logFileWithSpace();
|
||||
void generic_name();
|
||||
void generic_noRestart();
|
||||
void generic_restart();
|
||||
void generic_unknown();
|
||||
|
|
|
|||
|
|
@ -60,4 +60,24 @@ void CoreArgParserTests::portShort()
|
|||
QCOMPARE(Settings::value(Settings::Core::Port).toInt(), 18768);
|
||||
}
|
||||
|
||||
void CoreArgParserTests::nameLong()
|
||||
{
|
||||
QStringList args = {"stub", "client", "--name", "FancyName"};
|
||||
|
||||
CoreArgParser parser(args);
|
||||
parser.parse();
|
||||
|
||||
QCOMPARE(Settings::value(Settings::Core::ScreenName).toString(), "FancyName");
|
||||
}
|
||||
|
||||
void CoreArgParserTests::nameShort()
|
||||
{
|
||||
QStringList args = {"stub", "client", "-n", "ShortName"};
|
||||
|
||||
CoreArgParser parser(args);
|
||||
parser.parse();
|
||||
|
||||
QCOMPARE(Settings::value(Settings::Core::ScreenName).toString(), "ShortName");
|
||||
}
|
||||
|
||||
QTEST_MAIN(CoreArgParserTests)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ private Q_SLOTS:
|
|||
void interfaceShort();
|
||||
void portLong();
|
||||
void portShort();
|
||||
void nameLong();
|
||||
void nameShort();
|
||||
|
||||
private:
|
||||
inline static const QString m_settingsPath = QStringLiteral("tmp/test");
|
||||
|
|
|
|||
Loading…
Reference in a new issue