Commit 9591f314 authored by Lisa's avatar Lisa
Browse files

First draft of the new command line parsing system

parent e343ac82
project(akabei) project(akabei)
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 2.6)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules") set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}")
find_package(KDE4 REQUIRED) find_package(KDE4 REQUIRED)
find_package(AkabeiClient REQUIRED) find_package(AkabeiClient REQUIRED)
find_package(AkabeiCore REQUIRED) find_package(AkabeiCore REQUIRED)
include (KDE4Defaults) include (KDE4Defaults)
include (MacroLibrary) include (MacroLibrary)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}")
include_directories(${QT_INCLUDES} include_directories(${QT_INCLUDES}
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}
${KDE4_INCLUDE_DIR} ${KDE4_INCLUDE_DIR}
${AKABEICLIENT_INCLUDE_DIR} ${AKABEICLIENT_INCLUDE_DIR}
${AKABEICORE_INCLUDE_DIR}) ${AKABEICORE_INCLUDE_DIR})
set(apm_SRCS queueoperation.cpp upgradeoperation.cpp removeoperation.cpp syncoperation.cpp apm.cpp queryoperation.cpp main.cpp) set(apm_SRCS queueoperation.cpp upgradeoperation.cpp removeoperation.cpp syncoperation.cpp apm.cpp queryoperation.cpp cmdline.cpp main.cpp)
#qt4_automoc(${guzuta_SRCS}) #qt4_automoc(${guzuta_SRCS})
kde4_add_executable(akabei ${apm_SRCS}) kde4_add_executable(akabei ${apm_SRCS})
target_link_libraries(akabei ${KDE4_KDEUI_LIBRARY} ${KDE4_KIO_LIBRARY} ${AKABEICLIENT_LIBRARIES}) target_link_libraries(akabei ${KDE4_KDEUI_LIBRARY} ${KDE4_KIO_LIBRARY} ${AKABEICLIENT_LIBRARIES})
......
...@@ -22,10 +22,10 @@ ...@@ -22,10 +22,10 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QTimer> #include <QTimer>
APM::APM(OperationType type, QList<Operation> operations, QMultiHash<Operation, QString> options, QStringList args, QObject * parent) OperationManager::OperationManager(APM::OperationType type, APM::OperationName operation, QHash<APM::OptionName, AkabeiOption> options, QStringList args, QObject * parent)
: QObject(parent), : QObject(parent),
m_operationType(type), m_type(type),
m_operations(operations), m_operation(operation),
m_options(options), m_options(options),
m_args(args) m_args(args)
{ {
...@@ -33,43 +33,55 @@ APM::APM(OperationType type, QList<Operation> operations, QMultiHash<Operation, ...@@ -33,43 +33,55 @@ APM::APM(OperationType type, QList<Operation> operations, QMultiHash<Operation,
//QCoreApplication::instance()->quit(); //QCoreApplication::instance()->quit();
//QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit())); //QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit()));
connect(AkabeiClient::Backend::instance(), SIGNAL(statusChanged(Akabei::Backend::Status)), SLOT(statusChanged(Akabei::Backend::Status))); connect(AkabeiClient::Backend::instance(), SIGNAL(statusChanged(Akabei::Backend::Status)), SLOT(statusChanged(Akabei::Backend::Status)));
QString root = options.value(APM::RootDir); QString root = ".";
QString cache = options.value(APM::CacheDir); QString cache = ".";
QString dbs = options.value(APM::DBPath); QString dbs = ".";
if (options.contains(APM::RootDir)) {
root = options[APM::RootDir].args[0];
}
if (options.contains(APM::CacheDir)) {
cache = options[APM::CacheDir].args[0];
}
if (options.contains(APM::DBPath)) {
dbs = options[APM::DBPath].args[0];
}
AkabeiClient::Backend::instance()->initialize(root, cache, dbs); AkabeiClient::Backend::instance()->initialize(root, cache, dbs);
} }
APM::~APM() OperationManager::~OperationManager()
{} {
}
void APM::statusChanged(Akabei::Backend::Status status) void OperationManager::statusChanged(Akabei::Backend::Status status)
{ {
if (status == Akabei::Backend::StatusReady) if (status == Akabei::Backend::StatusReady)
start(); start();
} }
void APM::start() void OperationManager::start()
{ {
switch (m_operationType) { switch (m_type) {
case QueryOperationType: { case APM::QueryOperationType: {
QueryOperation * query = new QueryOperation(this); QueryOperation * query = new QueryOperation(this);
query->start(m_operations, m_options, m_args); query->start(m_operation, m_options, m_args);
break; break;
} }
case RemoveOperationType: { case APM::RemoveOperationType: {
RemoveOperation * remove = new RemoveOperation(this); RemoveOperation * remove = new RemoveOperation(this);
remove->start(m_operations, m_options, m_args); remove->start(m_operation, m_options, m_args);
break; break;
} }
case SyncOperationType: { case APM::SyncOperationType: {
SyncOperation * sync = new SyncOperation(this); SyncOperation * sync = new SyncOperation(this);
sync->start(m_operations, m_options, m_args); sync->start(m_operation, m_options, m_args);
break; break;
} }
case UpgradeOperationType: { case APM::UpgradeOperationType: {
UpgradeOperation * upgrade = new UpgradeOperation(this); UpgradeOperation * upgrade = new UpgradeOperation(this);
upgrade->start(m_operations, m_options, m_args); upgrade->start(m_operation, m_options, m_args);
break; break;
} }
default: default:
......
...@@ -15,74 +15,23 @@ ...@@ -15,74 +15,23 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QStringList> #include <QStringList>
class APM : public QObject #include "cmdline.h"
class OperationManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
enum OperationType { explicit OperationManager(APM::OperationType type, APM::OperationName operation, QHash<APM::OptionName, AkabeiOption> options, QStringList args, QObject* parent = 0);
NoType = 0, virtual ~OperationManager();
DatabaseOperationType = 1,
QueryOperationType = 2,
RemoveOperationType = 3,
SyncOperationType = 4,
UpgradeOperationType = 5
};
enum Operation {
NoOperation = 0,
///Query operations
ShowChangelog,
ShowInstalledAsDeps,
ShowInstalledExplicitely,
ShowPackagesOfGroup, //Also in Sync
ShowInformation, //Also in Sync
CheckFiles,
ShowLocal,
ShowOwner,
QueryPackageFile,
SearchLocal,
ShowNotRequired,
ShowUpgradeable,
ShowLess,
///Sync operations
Install,
AsDeps,//Also in upgrade
AsExplicit,//Also in upgrade
RemoveOldPackages,
RemoveAllPackages,
SkipDependencyCheck,//Also in Remove/Upgrade
Force,//Also in upgrade
ListRepo,
Search,
DownloadOnly,
OnlyNeeded,
Ignore,
IgnoreGroup,
UpdateDatabases,
UpdateSystem,
///Remove operations
Remove,
Cascade,
DatabaseOnly,//Also in upgrade
RemoveConfig,
Recursive,
Unneeded,
///Upgrade operation makes only use of operations specified above
///Random
RootDir,
CacheDir,
DBPath
};
explicit APM(OperationType type, QList<Operation> operations, QMultiHash<Operation, QString> options, QStringList args, QObject* parent = 0);
virtual ~APM();
private slots: private slots:
void statusChanged(Akabei::Backend::Status); void statusChanged(Akabei::Backend::Status);
void start(); void start();
private: private:
OperationType m_operationType; APM::OperationType m_type;
QList<Operation> m_operations; APM::OperationName m_operation;
QMultiHash<Operation, QString> m_options; QHash<APM::OptionName, AkabeiOption> m_options;
QStringList m_args; QStringList m_args;
}; };
......
This diff is collapsed.
/* This file is part of the Chakra project
Copyright (C) 2011 Lisa "shainer" Vitolo <shainer@chakra-project.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef _CMDLINE_H
#define _CMDLINE_H
#include <QString>
#include <QList>
#include <QHash>
#include <klocalizedstring.h>
#include <kcmdlineargs.h>
#include <exception>
namespace APM
{
enum OperationType {
NoType, DatabaseOperationType, QueryOperationType, RemoveOperationType, SyncOperationType, UpgradeOperationType
};
typedef enum OperationType OperationType;
enum OptionName {
QueryPackageFile, ShowLocal, ShowLess, InstallAsDeps, InstallAsExplicit,
SkipDependencyCheck, Force, DownloadOnly, OnlyNeeded, Ignore, IgnoreGroup, DatabaseOnly,
RemoveConfig, Recursive,
RootDir, CacheDir, DBPath
};
typedef enum OptionName OptionName;
enum OperationName {
None,
ShowChangelog, ShowInstalledAsDeps, ShowInstalledExplicitely, ShowPackagesOfGroup, ShowPackagesOfGroupLocal, ShowInformationLocal,
ShowInformation, CheckFiles, ShowOwner, ShowNotRequired, ShowUpgradeable,
Install, RemoveOldFromCache, RemoveAllCache, ListRepo, Search, UpdateDatabases, UpdateSystem, UpdateDatabaseAndSystem,
Remove, Cascade, Unneeded, SearchLocal,
DatabaseOperation, Upgrade, Help, Version
};
typedef enum OperationName OperationName;
};
/**
* \class AkabeiOption cmdline.h "cmdline.h"
* \brief Representation of a command line option
*/
class AkabeiOption
{
private:
void init(APM::OptionName, const char *, const char *, const char *, bool, QStringList);
public:
APM::OptionName name;
QByteArray commandShort, commandLong;
KLocalizedString description;
bool hasArg;
/*
* Args are required by some options like --ignore or --ignoregroup
*/
QStringList args;
AkabeiOption(APM::OptionName, const char *, const char *, const char *, bool);
AkabeiOption(APM::OptionName, const char *, const char *, const char *, bool, QStringList);
AkabeiOption() {}
};
typedef QList<AkabeiOption> AkabeiOptionList;
/**
* \class AkabeiOperation cmdline.h "cmdline.h"
* \brief Representation of a command line operation
*/
class AkabeiOperation
{
private:
void init(APM::OperationType, APM::OperationName, const char *, const char *, const char *, bool, QStringList);
public:
APM::OperationType type;
APM::OperationName name;
QByteArray commandShort, commandLong;
KLocalizedString description;
QHash<APM::OptionName, AkabeiOption> options; /* options that can be associated to this operation */
bool hasFreeArgs;
/*
* An example of free args is the list of packages following an install command
*/
QStringList freeArgs;
AkabeiOperation();
AkabeiOperation(APM::OperationType, APM::OperationName, const char *, const char *, const char *, bool);
AkabeiOperation(APM::OperationType, APM::OperationName, const char *, const char *, const char *, bool, QStringList);
/*
* Checks, with special rules, than no more than one operation is specified on the command line
* see cmdline.cpp for a more detailed explanation
*/
void set(AkabeiOperation &);
/*
* Options checking
*/
void addOption(AkabeiOption &);
bool isOptionSupported(APM::OptionName);
};
typedef QList<AkabeiOperation> AkabeiOperationList;
class CmdLine
{
private:
AkabeiOperation m_operation;
QHash<APM::OptionName, AkabeiOption> m_options;
QStringList m_freeArgs;
/* Database */
QMultiHash<QByteArray, AkabeiOperation> acceptedOperations;
AkabeiOptionList acceptedOptions;
QHash<QByteArray, APM::OperationType> typechars;
bool parsed;
public:
CmdLine();
void parse(int argc, char **argv);
APM::OperationType type();
APM::OperationName operation();
QHash<APM::OptionName, AkabeiOption> options();
QStringList freeArgs();
};
class CmdLineException : public std::exception
{
const char* message;
public:
CmdLineException(const char *m)
: message(m)
{}
virtual const char *what() const throw() {
return message;
}
};
#endif
...@@ -13,256 +13,23 @@ ...@@ -13,256 +13,23 @@
#include <kaboutdata.h> #include <kaboutdata.h>
#include <kdebug.h> #include <kdebug.h>
#include "apm.h" #include "apm.h"
#include "cmdline.h"
#include "../../buildsystem/scripts/prova/usr/include/rpcsvc/sm_inter.h"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
KAboutData aboutData("akabei", 0, ki18n("Akabei"),
QByteArray("0.0.1"),
ki18n("Chakra's CLI package manager"),
KAboutData::License_GPL,
ki18n("(C) 2011 Lukas Appelhans"),
ki18n("<a href=\"mailto:boom1992@chakra-project.org\">boom1992@chakra-project.org</a>"));
aboutData.addAuthor(ki18n("Lukas Appelhans"), ki18n("Maintainer"), "boom1992@chakra-project.org");
KCmdLineArgs::init(argc, argv, &aboutData);
KCmdLineOptions options;
//options.add("r");
options.add("root <path>", ki18n("Set a different root dir"));
options.add("b");
options.add("dbpath <path>", ki18n("Set a different database dir"));
options.add("cachedir <path>", ki18n("Set a different cache dir"));
options.add("D", ki18n("Database operation"));
//FIXME: For example with owns we should use "owns <File>", this needs different parsing later as we need to use getOption
//Doesn't work really with -Qo stuff then :/s
options.add("Q", ki18n("Query operation"));
options.add("c");
options.add("changelog", ki18n("Shows changelog of the package"));
options.add("d");
options.add("deps", ki18n("Shows packages which got installed as dependencies"));
options.add("e");
options.add("explicit", ki18n("Shows packages which got installed explicitely"));
options.add("g");
options.add("groups", ki18n("Shows all packages in a package group"));
options.add("i");
options.add("info", ki18n("Shows package information"));
options.add("k");
options.add("check", ki18n("Check if the files of the package are available"));
options.add("m");
options.add("foreign", ki18n("Shows packages which were not found in the Sync-db(s)"));
options.add("o");
options.add("owns", ki18n("Returns the package which contains the file"));
options.add("p");
options.add("file", ki18n("Queries a package file instead of the database"));
options.add("s");
options.add("search", ki18n("Searches locally installed packages"));
options.add("t");
options.add("unrequired", ki18n("Returns packages who are not required by others"));
options.add("u");
options.add("upgrades", ki18n("Returns upgradable packages"));
options.add("q");
options.add("quiet", ki18n("Shows less information in queries and searches"));
options.add("R", ki18n("Remove operation"));
options.add("c");
options.add("cascade", ki18n("Removes all packages and those depending on them"));
options.add("d");
options.add("nodeps", ki18n("Skip dependency check"));
options.add("k");
options.add("dbonly", ki18n("Only remove database entries"));
options.add("n", ki18n("Also remove configuration files"), false);
options.add("nosave", ki18n("Also remove configuration files"), false);
options.add("s");
options.add("recursive", ki18n("Remove dependencies as well. (-ss includes explicitely installed dependencies"));
options.add("u");
options.add("unneeded", ki18n("Removes not used packages"));
options.add("S", ki18n("Sync operation"));
options.add("asdeps", ki18n("Install all packages as non-explicit"));
options.add("asexplicit", ki18n("Install all packages as explicit"));
options.add("c");
options.add("clean", ki18n("Remove old packages from cache"));
options.add("cc", ki18n("Remove all packages from cache"));
options.add("d");
options.add("nodeps", ki18n("Skip dependency check"));
options.add("f");
options.add("force", ki18n("Force installation. Overwrite file conflicts"));
options.add("g");
options.add("groups", ki18n("Show all packages in a group"));
options.add("i");
options.add("info", ki18n("Show package information"));
options.add("l");
options.add("list", ki18n("List all packages of a specific repository"));
options.add("s");
options.add("search", ki18n("Searches repositories for a word"));
options.add("w");
options.add("downloadonly", ki18n("Downloads packages without installing or upgrading them"));
options.add("needed", ki18n("Up-to-date packages are not reinstalled"));
options.add("ignore <package>", ki18n("Ignores a certain package. Can be used more than once."));
options.add("ignoregroup <group>", ki18n("Ignores upgrade of a group"));
options.add("y");
options.add("refresh", ki18n("Refresh database"));
options.add("u");
options.add("sysupgrade", ki18n("Update system"));
options.add("U", ki18n("Upgrade operation"));
options.add("+[arg1]", ki18n("An optional argument 'arg1'"));
KCmdLineArgs::addCmdLineOptions(options);
// Add options from other components
//KUniqueApplication::addCmdLineOptions();
//if (!KUniqueApplication::start()) {
// fprintf(stderr, "Akabei is already running!\n");
// return 0;
//}
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
CmdLine cmd;
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
QList<APM::Operation> list;
APM::OperationType type = APM::NoType;
QMultiHash<APM::Operation, QString> opts;
///NOTE: The actual order the arguments get checked is later transferred into when they are going to be done try {
if (args->isSet("D")) { cmd.parse(argc, argv);
type = APM::DatabaseOperationType; } catch (CmdLineException& ex) {
} else if (args->isSet("Q")) { qDebug() << "!! Fatal error parsing command line: " << ex.what();
type = APM::QueryOperationType;
if (args->isSet("c"))
list.append(APM::ShowChangelog);
if (args->isSet("d"))
list.append(APM::ShowInstalledAsDeps);
if (args->isSet("e"))
list.append(APM::ShowInstalledExplicitely);
if (args->isSet("g"))
list.append(APM::ShowPackagesOfGroup);
if (args->isSet("i"))
list.append(APM::ShowInformation);
if (args->isSet("k"))
list.append(APM::CheckFiles);
if (args->isSet("m"))
list.append(APM::ShowLocal);
if (args->isSet("o"))
list.append(APM::ShowOwner);
if (args->isSet("p"))
list.append(APM::QueryPackageFile);
if (args->isSet("s"))
list.append(APM::SearchLocal);
if (args->isSet("t"))
list.append(APM::ShowNotRequired);
if (args->isSet("u"))
list.append(APM::ShowUpgradeable);
if (args->isSet("q"))
list.append(APM::ShowLess);
} else if (args->isSet("R")) {
type = APM::RemoveOperationType;
list.append(APM::Remove);
if (args->isSet("c"))
list.append(APM::Cascade);
if (args->isSet("d"))
list.append(APM::SkipDependencyCheck);
if (args->isSet("k"))
list.append(APM::DatabaseOnly);
if (args->isSet("n"))
list.append(APM::RemoveConfig);
if (args->isSet("s"))
list.append(APM::Recursive);
if (args->isSet("u"))
list.append(APM::Unneeded);
} else if (args->isSet("S")) {
type = APM::SyncOperationType;
list.append(APM::Install);
if (args->isSet("y"))
list.append(APM::UpdateDatabases);
if (args->isSet("u"))
list.append(APM::UpdateSystem);
if (args->isSet("asdeps"))
list.append(APM::AsDeps);
if (args->isSet("asexplicit"))
list.append(APM::AsExplicit);
if (args->isSet("c"))
list.append(APM::RemoveOldPackages);
if (args->isSet("cc"))
list.append(APM::RemoveAllPackages);
if (args->isSet("d"))
list.append(APM::SkipDependencyCheck);
if (args->isSet("f"))
list.append(APM::Force);
if (args->isSet("g"))
list.append(APM::ShowPackagesOfGroup);
if (args->isSet("i"))
list.append(APM::ShowInformation);
if (args->isSet("l"))
list.append(APM::ListRepo);
if (args->isSet("s"))
list.append(APM::Search);
if (args->isSet("w"))
list.append(