Commit c5bbfc67 authored by Lisa's avatar Lisa

Command line parsing fixed and simplified. Added license comments in a couple of files.

parent 2aeca88d
......@@ -99,12 +99,12 @@ AkabeiOperation::AkabeiOperation()
: m_name(APM::None)
{}
AkabeiOperation::AkabeiOperation(APM::OperationType t, APM::OperationName n, QString cmd, QString cmdLong, KLocalizedString desc, bool a)
AkabeiOperation::AkabeiOperation(APM::OperationType t, APM::OperationName n, QString cmd, QString cmdLong, const char *desc, bool a)
: m_type(t)
, m_name(n)
, m_commandShort(cmd)
, m_commandLong(cmdLong)
, m_description(desc)
, m_description(ki18n(desc))
, m_freeArgs(a)
{}
......
......@@ -16,7 +16,7 @@
#include <QStringList>
#include <QHash>
#include <klocalizedstring.h>
#include "akabeienums.h"
#include <akabeienums.h>
/*
* These are useful when showing an aligned list in the help message to represent
......@@ -120,7 +120,7 @@ private:
public:
AkabeiOperation();
AkabeiOperation(APM::OperationType, APM::OperationName, QString, QString, KLocalizedString, bool a = false);
AkabeiOperation(APM::OperationType, APM::OperationName, QString, QString, const char *, bool a = false);
/**
* @returns the operation type
......
......@@ -16,10 +16,10 @@ AkabeiParser::AkabeiParser(int argc, char **argv, const KAboutData& about)
: CommandLineParser(argc, argv, about)
{
/* Special operations */
AkabeiOperation help(APM::NoType, APM::Help, "", "help", ki18n("Show an help message"));
AkabeiOperation version(APM::NoType, APM::Version, "", "version", ki18n("Show version information"));
AkabeiOperation license(APM::NoType, APM::License, "", "license", ki18n("Show license information"));
AkabeiOperation authors(APM::NoType, APM::Authors, "", "authors", ki18n("Show information about the authors"));
AkabeiOperation help(APM::NoType, APM::Help, "", "help", "Show an help message");
AkabeiOperation version(APM::NoType, APM::Version, "", "version", "Show version information");
AkabeiOperation license(APM::NoType, APM::License, "", "license", "Show license information");
AkabeiOperation authors(APM::NoType, APM::Authors, "", "authors", "Show information about the authors");
m_acceptedOperations.insert("help", help);
m_acceptedOperations.insert("version", version);
......@@ -27,12 +27,13 @@ AkabeiParser::AkabeiParser(int argc, char **argv, const KAboutData& about)
m_acceptedOperations.insert("authors", authors);
}
void AkabeiParser::addOperation(APM::OperationType type, APM::OperationName name, QString command, QString commandLong, const char *desc, bool args)
void AkabeiParser::addOperation(AkabeiOperation &op)
{
AkabeiOperation operation(type, name, command, commandLong, ki18n(desc), args);
foreach (APM::OptionName opt, generalOptions) {
op.addConnectionWithOption(opt);
}
m_acceptedOperations.insert(commandLong, operation);
m_lastOperation = commandLong; /* saves the operation so we know where to add the option (see addConnectionWithOption) */
m_acceptedOperations.insert(op.commandLong(), op);
}
void AkabeiParser::addOption(APM::OptionName name, QString cmdShort, QString cmdLong, const char *desc, QString argname)
......@@ -43,20 +44,9 @@ void AkabeiParser::addOption(APM::OptionName name, QString cmdShort, QString cmd
m_acceptedOptions.insert(cmdLong, option);
}
void AkabeiParser::addConnectionWithOption(APM::OptionName name)
{
if (!m_lastOperation.isEmpty()) {
AkabeiOperation &op = m_acceptedOperations[m_lastOperation];
op.addConnectionWithOption(name);
}
}
void AkabeiParser::connectOptionWithEverything(APM::OptionName name)
{
foreach (const QString& cmd, m_acceptedOperations.keys()) {
AkabeiOperation &op = m_acceptedOperations[cmd];
op.addConnectionWithOption(name);
}
generalOptions.push_front(name);
}
void AkabeiParser::parse()
......@@ -128,7 +118,8 @@ void AkabeiParser::doParse()
continue;
}
m_options.insert(option.name(), option);
m_options.insert(option.name(), option);
optionLit.clear();
} else { /* the first normal string is an operation, the next generic arguments */
if (!foundOp) {
......
......@@ -10,23 +10,22 @@
version 2 of the License, or (at your option) any later version.
*/
#ifndef CMDLINEPARSING_H
#define CMDLINEPARSING_H
#ifndef AKABEIPARSER_H
#define AKABEIPARSER_H
#include <QStringList>
#include <QHash>
#include "akabeioptions.h"
#include "parser.h"
#include <akabeioptions.h>
#include <parser.h>
class AkabeiParser : public CommandLineParser
{
public:
AkabeiParser(int, char**, const KAboutData&);
void addOperation(APM::OperationType type, APM::OperationName name, QString command, QString commandLong, const char *desc, bool args);
void addOperation(AkabeiOperation &);
void addOption(APM::OptionName name, QString cmdShort, QString cmdLong, const char *desc, QString argname="");
void addConnectionWithOption(APM::OptionName name);
void connectOptionWithEverything(APM::OptionName name);
void parse();
......@@ -37,7 +36,7 @@ public:
QStringList args();
private:
QString m_lastOperation;
QList<APM::OptionName> generalOptions;
QHash<QString, AkabeiOperation> m_acceptedOperations;
QHash<QString, AkabeiOption> m_acceptedOptions;
......
......@@ -7,16 +7,16 @@
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#include "apm.h"
#include "akabeioptions.h"
#include <apm.h>
#include <akabeioptions.h>
#include "queryoperation.h"
#include "syncoperation.h"
#include "removeoperation.h"
#include "upgradeoperation.h"
#include "performoperation.h"
#include <queryoperation.h>
#include <syncoperation.h>
#include <removeoperation.h>
#include <upgradeoperation.h>
#include <performoperation.h>
#include <akabeiclient/akabeiclientbackend.h>
#include <akabeiclientbackend.h>
#include <kdebug.h>
#include <QStringList>
......
......@@ -7,16 +7,16 @@
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef APM_H
#define APM_H
#include <akabeibackend.h>
#include <akabeienums.h>
#include <QtCore/QObject>
#include <QStringList>
#include "akabeienums.h"
class OperationManager : public QObject
{
Q_OBJECT
......
#include "fieldnames.h"
/* This file is part of the Chakra project
Copyright (C) 2011 Daniele Cocca <daniele.cocca@gmail.com>
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.
*/
#include <fieldnames.h>
#include <klocale.h>
FieldNames::FieldNames()
......
#include <QtCore/QHash>
/* This file is part of the Chakra project
Copyright (C) 2011 Daniele Cocca <daniele.cocca@gmail.com>
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.
*/
#include <QHash>
class FieldNames : public QHash< QString, QString >
{
......
This diff is collapsed.
......@@ -97,12 +97,13 @@ PakabeiParser::PakabeiParser(int argc, char **argv, KAboutData& about)
* This is because acceptedOperations hashes a lot of operations under the same type (of course)
* so after an operation is inserted, retrieving it to modify it in addConnectionWithOption is a bit complex.
*/
void PakabeiParser::addOperation(APM::OperationType type, APM::OperationName name, QString command, QString commandLong, const char *desc, bool args)
void PakabeiParser::addOperation(AkabeiOperation& op)
{
AkabeiOperation op(type, name, command, commandLong, ki18n(desc), args);
foreach (APM::OptionName opt, generalOptions) {
op.addConnectionWithOption(opt);
}
namedOperations.insert(op.name(), op);
lastOperation = op;
acceptedOperations.insertMulti(op.type(), op);
}
void PakabeiParser::addOption(APM::OptionName name, QString cmdShort, QString cmdLong, const char* desc, QString argname)
......@@ -111,27 +112,13 @@ void PakabeiParser::addOption(APM::OptionName name, QString cmdShort, QString cm
acceptedOptions.push_front(opt);
}
void PakabeiParser::addConnectionWithOption(APM::OptionName name)
{
AkabeiOperation& op = namedOperations[lastOperation.name()];
op.addConnectionWithOption(name);
}
void PakabeiParser::connectOptionWithEverything(APM::OptionName option)
{
foreach (APM::OperationName name, namedOperations.keys()) {
AkabeiOperation &op = namedOperations[name];
op.addConnectionWithOption(option);
}
generalOptions.push_front(option);
}
void PakabeiParser::parse()
{
foreach (APM::OperationName name, namedOperations.keys()) {
AkabeiOperation &op = namedOperations[name];
acceptedOperations.insertMulti(op.type(), op);
}
CommandLine commandLine(m_argv);
m_type = APM::NoType;
......@@ -161,14 +148,14 @@ void PakabeiParser::parse()
found << "Q";
}
/*
/*
* We look only for the acceptable operations
* all the others, if present, will be spotted later
*/
foreach (AkabeiOperation op, acceptedOperations.values(m_type)) {
/*
* Saves up the commands for later inspection
*/
/*
* Saves up the commands for later inspection
*/
if (commandLine.isSet(op.commandShort()) || commandLine.isSet(op.commandLong())) {
m_operations.insert(op.name(), op);
found << op.commandShort();
......
......@@ -10,17 +10,17 @@
version 2 of the License, or (at your option) any later version.
*/
#ifndef _CMDLINE_H
#define _CMDLINE_H
#ifndef _PAKABEIPARSER_H
#define _PAKABEIPARSER_H
#include <QString>
#include <QList>
#include <QHash>
#include <QSet>
#include "akabeioptions.h"
#include "akabeienums.h"
#include "parser.h"
#include <akabeioptions.h>
#include <akabeienums.h>
#include <parser.h>
/**
* \class CommandLine
......@@ -74,17 +74,15 @@ private:
QHash<APM::OptionName, AkabeiOption> m_options;
QStringList m_freeArgs;
/* I'm sorry for this, please try to delete it */
QHash<APM::OperationName, AkabeiOperation> namedOperations;
/* This is a very small list to store options that have to be connected to every operation */
QList<APM::OptionName> generalOptions;
/* "Database" */
QMultiHash<APM::OperationType, AkabeiOperation> acceptedOperations;
QList<AkabeiOption> acceptedOptions;
QHash<QString, APM::OperationType> typechars;
AkabeiOperation lastOperation;
/* I'm sorry for this too, but it speeds up one particular stage */
/* I'm sorry for this, but it speeds up one particular stage */
QHash<APM::OperationName, AkabeiOperation> specialOperations;
bool freeArgsRequired();
......@@ -92,9 +90,8 @@ private:
public:
PakabeiParser(int argc, char **argv, KAboutData &);
void addOperation(APM::OperationType type, APM::OperationName name, QString command, QString commandLong, const char *desc, bool args);
void addOperation(AkabeiOperation &);
void addOption(APM::OptionName name, QString cmdShort, QString cmdLong, const char *desc, QString argname="");
void addConnectionWithOption(APM::OptionName name);
void connectOptionWithEverything(APM::OptionName name);
void parse();
......
......@@ -13,9 +13,9 @@
#ifndef PARSER_H
#define PARSER_H
#include "akabeienums.h"
#include "akabeioptions.h"
#include "parsingexceptions.h"
#include <akabeienums.h>
#include <akabeioptions.h>
#include <parsingexceptions.h>
#include <kaboutdata.h>
/**
......@@ -39,14 +39,9 @@ public:
/**
* Add an operation to those supported.
*
* @param type the operation type
* @param name the symbolic name
* @param commandShort the short command
* @param commandLong the long command
* @param desc a description
* @param args whether the operation requires arguments
* @param op the operation object.
*/
virtual void addOperation(APM::OperationType, APM::OperationName, QString, QString, const char *, bool) =0;
virtual void addOperation(AkabeiOperation &) =0;
/**
* Add option to those supported
......@@ -59,14 +54,6 @@ public:
*/
virtual void addOption(APM::OptionName, QString, QString, const char *, QString argname="") =0;
/**
* Add the possibility of use an option with an operation.
* The operation involved is the last one added to the pool.
*
* @param name the symbolic name of the option to add.
*/
virtual void addConnectionWithOption(APM::OptionName) =0;
/**
* Some special options can be associated to every operation.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment