summaryrefslogtreecommitdiffstats
path: root/Source/cmCommandArgumentsHelper.h
blob: f732f8f1d3d42855d347aa7ea885e5b5284ef547 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
#ifndef cmCommandArgumentsHelper_h
#define cmCommandArgumentsHelper_h

#include "cmStandardIncludes.h"

class cmCommandArgumentsHelper;
class cmCommandArgumentGroup;

/* cmCommandArgumentsHelper, cmCommandArgumentGroup and cmCommandArgument (i.e.
its derived classes cmCAXXX can be used to simplify the processing of
arguments to cmake commands. Maybe they can also be used to generate
documentation.

For every argument supported by a command one cmCommandArgument is created
and added to cmCommandArgumentsHelper. cmCommand has a cmCommandArgumentsHelper
as member variable so this should be used.

The order of the arguments is defined using the Follows(arg) method. It says
that this argument follows immediateley the given argument. It can be used
with multiple arguments if the argument can follow after different arguments.

Arguments can be arranged in groups using cmCommandArgumentGroup. Every
member of a group can follow any other member of the group. These groups
can also be used to define the order.

Once all arguments and groups are set up, cmCommandArgumentsHelper::Parse()
is called and afterwards the values of the arguments can be evaluated.

For an example see cmExportCommand.cxx.
*/
class cmCommandArgument
{
  public:
    cmCommandArgument(cmCommandArgumentsHelper* args,
                      const char* key,
                      cmCommandArgumentGroup* group=0);
    virtual ~cmCommandArgument() {}

    /// this argument may follow after arg. 0 means it comes first.
    void Follows(const cmCommandArgument* arg);

    /// this argument may follow after any of the arguments in the given group
    void FollowsGroup(const cmCommandArgumentGroup* group);

    /// Returns true if the argument was found in the argument list
    bool WasFound() const                             {return this->WasActive;}

    // The following methods are only called from
    // cmCommandArgumentsHelper::Parse(), but making this a friend would
    // give it access to everything

    /// Make the current argument the currently active argument
    void Activate();
    /// Consume the current string
    bool Consume(const std::string& arg);

    /// Return true if this argument may follow after the given argument.
    bool MayFollow(const cmCommandArgument* current) const;

    /** Returns true if the given key matches the key for this argument.
    If this argument has an empty key everything matches. */
    bool KeyMatches(const std::string& key) const;

    /// Make this argument follow all members of the own group
    void ApplyOwnGroup();

    /// Reset argument, so it's back to its initial state
    void Reset();
  private:
    const char* Key;
    std::set<const cmCommandArgument*> ArgumentsBefore;
    cmCommandArgumentGroup* Group;
    bool WasActive;
    bool ArgumentsBeforeEmpty;
    unsigned int CurrentIndex;

    virtual bool DoConsume(const std::string& arg, unsigned int index) = 0;
    virtual void DoReset() = 0;
};

/** cmCAStringVector is to be used for arguments which can consist of more
than one string, e.g. the FILES argument in INSTALL(FILES f1 f2 f3 ...). */
class cmCAStringVector : public cmCommandArgument
{
  public:
    cmCAStringVector(cmCommandArgumentsHelper* args,
                     const char* key,
                     cmCommandArgumentGroup* group=0);

    /// Return the vector of strings
    const std::vector<std::string>& GetVector() const    {return this->Vector;}

    /** Is there a keyword which should be skipped in
    the arguments (e.g. ARGS for ADD_CUSTOM_COMMAND) ? */
    void SetIgnore(const char* ignore)                   {this->Ignore=ignore;}
  private:
    std::vector<std::string> Vector;
    unsigned int DataStart;
    const char* Ignore;
    cmCAStringVector();
    virtual bool DoConsume(const std::string& arg, unsigned int index);
    virtual void DoReset();
};

/** cmCAString is to be used for arguments which consist of one value,
e.g. the executable name in ADD_EXECUTABLE(). */
class cmCAString : public cmCommandArgument
{
  public:
    cmCAString(cmCommandArgumentsHelper* args,
               const char* key,
               cmCommandArgumentGroup* group=0);

    /// Return the string
    const std::string& GetString() const                 {return this->String;}
    const char* GetCString() const               {return this->String.c_str();}
  private:
    std::string String;
    unsigned int DataStart;
    virtual bool DoConsume(const std::string& arg, unsigned int index);
    virtual void DoReset();
    cmCAString();
};

/** cmCAEnabler is to be used for options which are off by default and can be
enabled using a special argument, e.g. EXCLUDE_FROM_ALL in ADD_EXECUTABLE(). */
class cmCAEnabler : public cmCommandArgument
{
  public:
    cmCAEnabler(cmCommandArgumentsHelper* args,
                const char* key,
                cmCommandArgumentGroup* group=0);

    /// Has it been enabled ?
    bool IsEnabled() const                              {return this->Enabled;}
  private:
    bool Enabled;
    virtual bool DoConsume(const std::string& arg, unsigned int index);
    virtual void DoReset();
    cmCAEnabler();
};

/** cmCADisable is to be used for options which are on by default and can be
disabled using a special argument.*/
class cmCADisabler : public cmCommandArgument
{
  public:
    cmCADisabler(cmCommandArgumentsHelper* args,
                 const char* key,
                 cmCommandArgumentGroup* group=0);

    /// Is it still enabled ?
    bool IsEnabled() const                              {return this->Enabled;}
  private:
    bool Enabled;
    virtual bool DoConsume(const std::string& arg, unsigned int index);
    virtual void DoReset();
    cmCADisabler();
};


/** Group of arguments, needed for ordering. E.g. WIN32, EXCLUDE_FROM_ALL and
MACSOX_BUNDLE from ADD_EXECUTABLE() are a group.
*/
class cmCommandArgumentGroup
{
  friend class cmCommandArgument;
  public:
    cmCommandArgumentGroup() {}

    /// All members of this group may follow the given argument
    void Follows(const cmCommandArgument* arg);

    /// All members of this group may follow all members of the given group
    void FollowsGroup(const cmCommandArgumentGroup* group);
  private:
    std::vector<cmCommandArgument*> ContainedArguments;
};

class cmCommandArgumentsHelper
{
  public:
    /// Parse the argument list
    void Parse(const std::vector<std::string>* args,
               std::vector<std::string>* unconsumedArgs);
    /// Add an argument.
    void AddArgument(cmCommandArgument* arg);
  private:
    std::vector<cmCommandArgument*> Arguments;
};


#endif