aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/sqlitestudiocli/clicommandsyntax.h
blob: adf88d2dd3a7412cf4989154973f3414c157868f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef CLICOMMANDSYNTAX_H
#define CLICOMMANDSYNTAX_H

#include <QString>
#include <QStringList>
#include <QHash>

class CliCommandSyntax
{
    public:
        CliCommandSyntax();
        ~CliCommandSyntax();

        void addArgument(int id, const QString& name, bool mandatory = true);
        void addStrictArgument(int id, const QStringList& values, bool mandatory = true);
        void addAlternatedArgument(int id, const QStringList& names, bool mandatory = true);
        void addOptionShort(int id, const QString& shortName);
        void addOptionLong(int id, const QString& longName);
        void addOption(int id, const QString& shortName, const QString& longName);
        void addOptionWithArgShort(int id, const QString& shortName, const QString& argName);
        void addOptionWithArgLong(int id, const QString& longName, const QString& argName);
        void addOptionWithArg(int id, const QString& shortName, const QString& longName, const QString& argName);

        bool parse(const QStringList& args);
        QString getErrorText() const;
        QStringList getStrictArgumentCandidates();
        QList<int> getRegularArgumentCandidates();

        void addAlias(const QString& alias);
        QStringList getAliases() const;

        QString getSyntaxDefinition() const;
        QString getSyntaxDefinition(const QString& usedName) const;

        bool isArgumentSet(int id) const;
        QString getArgument(int id) const;

        bool isOptionSet(int id) const;
        QString getOptionValue(int id) const;

        QString getName() const;
        void setName(const QString& value);

        bool getStrictArgumentCount() const;
        void setStrictArgumentCount(bool value);

    private:
        struct Argument
        {
            enum Type
            {
                REGULAR,
                STRICT,
                ALTERNATED
            };

            int id;
            QStringList names;
            Type type = REGULAR;
            bool mandatory = true;
            bool defined = false;
            QString value;
        };

        struct Option
        {
            int id;
            QString shortName;
            QString longName;
            QString argName;
            bool requested = false;
            QString value;
        };

        bool parseArg(const QString& arg);
        bool parseOpt(const QString& arg, const QStringList& args, int& argIdx);
        int requiredArguments() const;
        void checkNewArgument(bool mandatory);
        Argument* addArgumentInternal(int id, const QStringList& names, bool mandatory, Argument::Type type);
        Option* addOptionInternal(int id, const QString& shortName, const QString& longName, const QString& argName);

        int argPosition = 0;
        QString parsingErrorText;
        bool strictArgumentCount = true;
        bool pastOptions = false;
        QString name;
        QStringList aliases;
        QList<Argument*> arguments;
        QHash<int,Argument*> argumentMap;
        QList<Option*> options;
        Option* lastParsedOption = nullptr;
        QHash<int,Option*> optionMap;
        QHash<QString,Option*> optionsShortNameMap;
        QHash<QString,Option*> optionsLongNameMap;
};

#endif // CLICOMMANDSYNTAX_H