savegame ex.: give some TLC to main()

- include what you use
- make 'args' const, so we don't detach in op[]
- make boolean variables const
- use QString::compare(lhs, rhs, Qt::CaseInsensitive) instead of
  lhs.toLower() == rhs
- use new _L1 UDL
- fix indentation of a return statement

Pick-to: 6.5
Change-Id: If9da4fbe975d9a97939ea01558b2a8cef7ad3a24
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-02-23 11:34:16 +01:00
parent 26f0657fee
commit 9834e80833

View File

@ -4,25 +4,28 @@
#include "game.h"
#include <QCoreApplication>
#include <QStringList>
#include <QString>
#include <QTextStream>
using namespace Qt::StringLiterals; // for _L1
//! [0]
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QStringList args = QCoreApplication::arguments();
bool newGame = true;
if (args.length() > 1)
newGame = (args[1].toLower() != QStringLiteral("load"));
bool json = true;
if (args.length() > 2)
json = (args[2].toLower() != QStringLiteral("binary"));
const QStringList args = QCoreApplication::arguments();
const bool newGame
= args.size() <= 1 || QString::compare(args[1], "load"_L1, Qt::CaseInsensitive) == 0;
const bool json
= args.size() <= 2 || QString::compare(args[2], "binary"_L1, Qt::CaseInsensitive) == 0;
Game game;
if (newGame)
game.newGame();
else if (!game.loadGame(json ? Game::Json : Game::Binary))
return 1;
return 1;
// Game is played; changes are made...
//! [0]
//! [1]