1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCommandArgumentsHelper.h,v $
6 Date: $Date: 2007/08/23 20:13:15 $
7 Version: $Revision: 1.2 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #ifndef cmCommandArgumentsHelper_h
18 #define cmCommandArgumentsHelper_h
20 #include "cmStandardIncludes.h"
22 class cmCommandArgumentsHelper
;
23 class cmCommandArgumentGroup
;
25 /* cmCommandArgumentsHelper, cmCommandArgumentGroup and cmCommandArgument (i.e.
26 its derived classes cmCAXXX can be used to simplify the processing of
27 arguments to cmake commands. Maybe they can also be used to generate
30 For every argument supported by a command one cmCommandArgument is created
31 and added to cmCommandArgumentsHelper. cmCommand has a cmCommandArgumentsHelper
32 as member variable so this should be used.
34 The order of the arguments is defined using the Follows(arg) method. It says
35 that this argument follows immediateley the given argument. It can be used
36 with multiple arguments if the argument can follow after different arguments.
38 Arguments can be arranged in groups using cmCommandArgumentGroup. Every
39 member of a group can follow any other member of the group. These groups
40 can also be used to define the order.
42 Once all arguments and groups are set up, cmCommandArgumentsHelper::Parse()
43 is called and afterwards the values of the arguments can be evaluated.
45 For an example see cmExportCommand.cxx.
47 class cmCommandArgument
50 cmCommandArgument(cmCommandArgumentsHelper
* args
,
52 cmCommandArgumentGroup
* group
=0);
53 virtual ~cmCommandArgument() {}
55 /// this argument may follow after arg. 0 means it comes first.
56 void Follows(const cmCommandArgument
* arg
);
58 /// this argument may follow after any of the arguments in the given group
59 void FollowsGroup(const cmCommandArgumentGroup
* group
);
61 /// Returns true if the argument was found in the argument list
62 bool WasFound() const {return this->WasActive
;}
64 // The following methods are only called from
65 // cmCommandArgumentsHelper::Parse(), but making this a friend would
66 // give it access to everything
68 /// Make the current argument the currently active argument
70 /// Consume the current string
71 bool Consume(const std::string
& arg
);
73 /// Return true if this argument may follow after the given argument.
74 bool MayFollow(const cmCommandArgument
* current
) const;
76 /** Returns true if the given key matches the key for this argument.
77 If this argument has an empty key everything matches. */
78 bool KeyMatches(const std::string
& key
) const;
80 /// Make this argument follow all members of the own group
83 /// Reset argument, so it's back to its initial state
87 std::set
<const cmCommandArgument
*> ArgumentsBefore
;
88 cmCommandArgumentGroup
* Group
;
90 bool ArgumentsBeforeEmpty
;
91 unsigned int CurrentIndex
;
93 virtual bool DoConsume(const std::string
& arg
, unsigned int index
) = 0;
94 virtual void DoReset() = 0;
97 /** cmCAStringVector is to be used for arguments which can consist of more
98 than one string, e.g. the FILES argument in INSTALL(FILES f1 f2 f3 ...). */
99 class cmCAStringVector
: public cmCommandArgument
102 cmCAStringVector(cmCommandArgumentsHelper
* args
,
104 cmCommandArgumentGroup
* group
=0);
106 /// Return the vector of strings
107 const std::vector
<std::string
>& GetVector() const {return this->Vector
;}
109 /** Is there a keyword which should be skipped in
110 the arguments (e.g. ARGS for ADD_CUSTOM_COMMAND) ? */
111 void SetIgnore(const char* ignore
) {this->Ignore
=ignore
;}
113 std::vector
<std::string
> Vector
;
114 unsigned int DataStart
;
117 virtual bool DoConsume(const std::string
& arg
, unsigned int index
);
118 virtual void DoReset();
121 /** cmCAString is to be used for arguments which consist of one value,
122 e.g. the executable name in ADD_EXECUTABLE(). */
123 class cmCAString
: public cmCommandArgument
126 cmCAString(cmCommandArgumentsHelper
* args
,
128 cmCommandArgumentGroup
* group
=0);
130 /// Return the string
131 const std::string
& GetString() const {return this->String
;}
132 const char* GetCString() const {return this->String
.c_str();}
133 void SetDefaultString(const char* text
)
134 {this->DefaultString
= (text
? text
: "");}
137 std::string DefaultString
;
138 unsigned int DataStart
;
139 virtual bool DoConsume(const std::string
& arg
, unsigned int index
);
140 virtual void DoReset();
144 /** cmCAEnabler is to be used for options which are off by default and can be
145 enabled using a special argument, e.g. EXCLUDE_FROM_ALL in ADD_EXECUTABLE(). */
146 class cmCAEnabler
: public cmCommandArgument
149 cmCAEnabler(cmCommandArgumentsHelper
* args
,
151 cmCommandArgumentGroup
* group
=0);
153 /// Has it been enabled ?
154 bool IsEnabled() const {return this->Enabled
;}
157 virtual bool DoConsume(const std::string
& arg
, unsigned int index
);
158 virtual void DoReset();
162 /** cmCADisable is to be used for options which are on by default and can be
163 disabled using a special argument.*/
164 class cmCADisabler
: public cmCommandArgument
167 cmCADisabler(cmCommandArgumentsHelper
* args
,
169 cmCommandArgumentGroup
* group
=0);
171 /// Is it still enabled ?
172 bool IsEnabled() const {return this->Enabled
;}
175 virtual bool DoConsume(const std::string
& arg
, unsigned int index
);
176 virtual void DoReset();
181 /** Group of arguments, needed for ordering. E.g. WIN32, EXCLUDE_FROM_ALL and
182 MACSOX_BUNDLE from ADD_EXECUTABLE() are a group.
184 class cmCommandArgumentGroup
186 friend class cmCommandArgument
;
188 cmCommandArgumentGroup() {}
190 /// All members of this group may follow the given argument
191 void Follows(const cmCommandArgument
* arg
);
193 /// All members of this group may follow all members of the given group
194 void FollowsGroup(const cmCommandArgumentGroup
* group
);
196 std::vector
<cmCommandArgument
*> ContainedArguments
;
199 class cmCommandArgumentsHelper
202 /// Parse the argument list
203 void Parse(const std::vector
<std::string
>* args
,
204 std::vector
<std::string
>* unconsumedArgs
);
206 void AddArgument(cmCommandArgument
* arg
);
208 std::vector
<cmCommandArgument
*> Arguments
;