The window needs a display, which kept the package manager out of a script and off a headless machine. The command line runs the same operations through AppCore, before any widget exists, so providers, the pacman lock, the journal and the history behave exactly as in the window. Refs #22.
213 lines
7.5 KiB
C++
213 lines
7.5 KiB
C++
#include "cli-command.h"
|
|
|
|
#include <QHash>
|
|
|
|
namespace {
|
|
|
|
// Every keyword a command answers to, French first. Kept in one table so the
|
|
// help text and the parser cannot drift apart.
|
|
const QHash<QString, cli::Command>& keywords() {
|
|
static const QHash<QString, cli::Command> table = {
|
|
{QStringLiteral("recherche"), cli::Command::Search},
|
|
{QStringLiteral("search"), cli::Command::Search},
|
|
{QStringLiteral("cherche"), cli::Command::Search},
|
|
|
|
{QStringLiteral("install"), cli::Command::Install},
|
|
{QStringLiteral("installer"), cli::Command::Install},
|
|
|
|
{QStringLiteral("supprimer"), cli::Command::Remove},
|
|
{QStringLiteral("remove"), cli::Command::Remove},
|
|
{QStringLiteral("uninstall"), cli::Command::Remove},
|
|
{QStringLiteral("désinstaller"), cli::Command::Remove},
|
|
{QStringLiteral("desinstaller"), cli::Command::Remove},
|
|
|
|
{QStringLiteral("maj"), cli::Command::Update},
|
|
{QStringLiteral("update"), cli::Command::Update},
|
|
{QStringLiteral("upgrade"), cli::Command::Update},
|
|
|
|
{QStringLiteral("liste"), cli::Command::List},
|
|
{QStringLiteral("list"), cli::Command::List},
|
|
|
|
{QStringLiteral("maj-dispo"), cli::Command::Updates},
|
|
{QStringLiteral("updates"), cli::Command::Updates},
|
|
|
|
{QStringLiteral("info"), cli::Command::Info},
|
|
{QStringLiteral("détails"), cli::Command::Info},
|
|
{QStringLiteral("details"), cli::Command::Info},
|
|
|
|
{QStringLiteral("refresh"), cli::Command::Refresh},
|
|
{QStringLiteral("rafraîchir"), cli::Command::Refresh},
|
|
{QStringLiteral("rafraichir"), cli::Command::Refresh},
|
|
{QStringLiteral("sync"), cli::Command::Refresh},
|
|
|
|
{QStringLiteral("nettoyage"), cli::Command::Cleanup},
|
|
{QStringLiteral("cleanup"), cli::Command::Cleanup},
|
|
|
|
{QStringLiteral("aide"), cli::Command::Help},
|
|
{QStringLiteral("help"), cli::Command::Help},
|
|
|
|
{QStringLiteral("version"), cli::Command::Version},
|
|
};
|
|
return table;
|
|
}
|
|
|
|
bool isYesFlag(const QString& token) {
|
|
return token == QLatin1String("--yes") || token == QLatin1String("-y");
|
|
}
|
|
|
|
bool isHelpFlag(const QString& token) {
|
|
return token == QLatin1String("--help") || token == QLatin1String("-h")
|
|
|| token == QLatin1String("-?");
|
|
}
|
|
|
|
bool isVersionFlag(const QString& token) {
|
|
return token == QLatin1String("--version") || token == QLatin1String("-V")
|
|
|| token == QLatin1String("-v");
|
|
}
|
|
|
|
cli::Command commandFor(const QString& token) {
|
|
const auto it = keywords().constFind(token.toLower());
|
|
return it == keywords().constEnd() ? cli::Command::Unknown : it.value();
|
|
}
|
|
|
|
cli::Parsed refuse(const QString& error) {
|
|
cli::Parsed parsed;
|
|
parsed.command = cli::Command::Unknown;
|
|
parsed.error = error;
|
|
return parsed;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace cli {
|
|
|
|
bool isCliInvocation(const QStringList& args) {
|
|
if (args.isEmpty()) return false;
|
|
|
|
const QString first = args.first();
|
|
// Any leading option, or a known keyword, is aimed at the command line.
|
|
if (first.startsWith(QLatin1Char('-')) || commandFor(first) != Command::Unknown) {
|
|
return true;
|
|
}
|
|
|
|
// A path, or a file with one of the extensions the window knows how to
|
|
// install, is what the desktop hands over ("Ouvrir avec PacManager", the
|
|
// desktop entry declares %U): the window is wanted for those.
|
|
const QString lower = first.toLower();
|
|
const bool looksLikePath = first.contains(QLatin1Char('/'))
|
|
|| first.startsWith(QLatin1Char('~'));
|
|
const bool looksLikeFile = lower.endsWith(QLatin1String(".flatpak"))
|
|
|| lower.endsWith(QLatin1String(".flatpakref"))
|
|
|| lower.endsWith(QLatin1String(".flatpakrepo"))
|
|
|| lower.endsWith(QLatin1String(".fwspacm"));
|
|
if (looksLikePath || looksLikeFile) return false;
|
|
|
|
// A bare word that is not a command is a mistyped command: the command line
|
|
// says so, which is friendlier than opening a window that can do nothing
|
|
// with it.
|
|
return true;
|
|
}
|
|
|
|
Parsed parse(const QStringList& args) {
|
|
Parsed parsed;
|
|
if (args.isEmpty()) return parsed; // Command::None: the window
|
|
|
|
// --help/--version win wherever they stand, like every command line.
|
|
for (const QString& token : args) {
|
|
if (isHelpFlag(token)) {
|
|
parsed.command = Command::Help;
|
|
return parsed;
|
|
}
|
|
if (isVersionFlag(token)) {
|
|
parsed.command = Command::Version;
|
|
return parsed;
|
|
}
|
|
}
|
|
|
|
QStringList tokens;
|
|
for (const QString& token : args) {
|
|
// -y is a modifier, not an operand: it is read wherever it appears.
|
|
if (isYesFlag(token)) {
|
|
parsed.assumeYes = true;
|
|
continue;
|
|
}
|
|
tokens << token;
|
|
}
|
|
|
|
const QString first = tokens.takeFirst();
|
|
parsed.command = commandFor(first);
|
|
if (parsed.command == Command::Unknown) {
|
|
if (first.startsWith(QLatin1Char('-'))) {
|
|
return refuse(QStringLiteral("Option inconnue : %1").arg(first));
|
|
}
|
|
return refuse(QStringLiteral("Commande inconnue : %1").arg(first));
|
|
}
|
|
|
|
parsed.operands = tokens;
|
|
|
|
// What each command needs to be runnable, checked here so the runner never
|
|
// has to guess what a missing operand means.
|
|
const int count = tokens.size();
|
|
switch (parsed.command) {
|
|
case Command::Search:
|
|
case Command::Install:
|
|
case Command::Remove:
|
|
if (count == 0) {
|
|
return refuse(
|
|
QStringLiteral("« %1 » attend au moins un nom de paquet.").arg(first));
|
|
}
|
|
break;
|
|
case Command::Info:
|
|
if (count != 1) {
|
|
return refuse(QStringLiteral("« %1 » attend un nom de paquet.").arg(first));
|
|
}
|
|
break;
|
|
case Command::Update:
|
|
if (count > 1) {
|
|
return refuse(QStringLiteral(
|
|
"« %1 » ne met à jour qu'un paquet à la fois (ou tous sans nom).")
|
|
.arg(first));
|
|
}
|
|
break;
|
|
case Command::List:
|
|
case Command::Updates:
|
|
case Command::Refresh:
|
|
case Command::Cleanup:
|
|
case Command::Help:
|
|
case Command::Version:
|
|
if (count != 0) {
|
|
return refuse(QStringLiteral("« %1 » n'accepte pas d'argument.").arg(first));
|
|
}
|
|
break;
|
|
case Command::None:
|
|
case Command::Unknown:
|
|
break;
|
|
}
|
|
|
|
return parsed;
|
|
}
|
|
|
|
QString helpText() {
|
|
return QStringLiteral(
|
|
"PacManager — gestionnaire de paquets en ligne de commande\n"
|
|
"\n"
|
|
"Usage : pacmanager <commande> [paquet...] [--yes]\n"
|
|
"\n"
|
|
"Commandes :\n"
|
|
" recherche <terme> chercher des paquets\n"
|
|
" install <paquet...> installer un ou plusieurs paquets\n"
|
|
" supprimer <paquet...> supprimer un ou plusieurs paquets\n"
|
|
" maj [paquet] mettre à jour un paquet, ou tout sans nom\n"
|
|
" liste lister les paquets installés\n"
|
|
" maj-dispo lister les mises à jour disponibles\n"
|
|
" info <paquet> détails d'un paquet\n"
|
|
" refresh rafraîchir les bases de données\n"
|
|
" nettoyage lister les paquets inutilisés et legacy\n"
|
|
" aide | version cette aide, ou la version installée\n"
|
|
"\n"
|
|
"Alias anglais : search, remove, update, list, updates, details, cleanup, help.\n"
|
|
" -y, --yes ne pas demander confirmation (suppression, maj globale)");
|
|
}
|
|
|
|
} // namespace cli
|