pakabeiparser.h 2.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * This file is part of the Chakra project
 * Parser for the pacman-style command line (header)

   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.
*/

13 14
#ifndef _PAKABEIPARSER_H
#define _PAKABEIPARSER_H
15 16 17 18 19 20

#include <QString>
#include <QList>
#include <QHash>
#include <QSet>

21 22 23
#include <akabeioptions.h>
#include <akabeienums.h>
#include <parser.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

/**
 * \class CommandLine
 * \brief Some facilities to access the current command line
 */
class CommandLine
{
public:
    CommandLine(QStringList &);
    
    /**
     * @param command an option/operation command
     * @returns whether it is present in the line
     */
    bool isSet(const QString &);
    
    /**
     * @returns the option argument, or an empty string if none is available
     */
    QString optArg();
    
    /**
     * @returns all the generic arguments
     */
    QStringList args();
    
private:
    QStringList m_commands; /* the command line list */
    
    int m_index; /* current position in the list */
};

/**
 * \class PakabeiParser
 * \brief Parses a pacman-style command line
 * \extends CommandLineParser
 */
class PakabeiParser : public CommandLineParser
{
private:
    APM::OperationType m_type;
    QMap<APM::OperationName, AkabeiOperation> m_operations;
    QHash<APM::OptionName, AkabeiOption> m_options;
    QStringList m_freeArgs;
    
69 70
    /* This is a very small list to store options that have to be connected to every operation */
    QList<APM::OptionName> generalOptions;
71 72 73 74 75 76
    
    /* "Database" */
    QMultiHash<APM::OperationType, AkabeiOperation> acceptedOperations;
    QList<AkabeiOption> acceptedOptions;
    QHash<QString, APM::OperationType> typechars;
    
77
    /* I'm sorry for this, but it speeds up one particular stage */
78 79 80 81 82 83 84
    QHash<APM::OperationName, AkabeiOperation> specialOperations;
    
    bool freeArgsRequired();
    
public:
    PakabeiParser(int argc, char **argv, KAboutData &);
    
85
    void addOperation(AkabeiOperation &);
86 87 88 89 90 91 92 93 94 95 96 97
    void addOption(APM::OptionName name, QString cmdShort, QString cmdLong, const char *desc, QString argname="");
    void connectOptionWithEverything(APM::OptionName name);

    void parse();
    
    APM::OperationType type();
    QList<APM::OperationName> operations();
    QHash<APM::OptionName, QStringList> options();
    QStringList args();
};

#endif