1 // Created on: 2015-03-15
2 // Created by: Danila ULYANOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _ViewerTest_CmdParser_HeaderFile
17 #define _ViewerTest_CmdParser_HeaderFile
19 #include <Graphic3d_Vec3.hxx>
28 class Quantity_ColorRGBA;
32 //! A key for a command line option used for a ViewerTest_CmdParser work
33 typedef std::size_t ViewerTest_CommandOptionKey;
35 //! A set of keys for command-line options
36 typedef std::set<ViewerTest_CommandOptionKey> ViewerTest_CommandOptionKeySet;
39 class ViewerTest_CmdParser
42 //! The key of the unnamed command option
43 static const std::size_t THE_UNNAMED_COMMAND_OPTION_KEY;
45 //! The key of the help command option
46 static const std::size_t THE_HELP_COMMAND_OPTION_KEY;
48 //! Initializes help option.
49 //! @param theDescription the description of the command
50 ViewerTest_CmdParser (const std::string& theDescription = std::string());
52 //! Sets description for command.
53 void SetDescription (const std::string& theDescription)
55 myDescription = theDescription;
58 //! Adds option to available option list. Several names may be provided if separated with '|'.
59 //! @param theOptionNames the list of possible option names separated with '|'
60 //! (the first name is main, the other names are aliases)
61 //! @param theOptionDescription the description of the option
62 //! @return an access key of the newly added option
63 ViewerTest_CommandOptionKey AddOption (const std::string& theOptionNames,
64 const std::string& theOptionDescription = std::string());
66 //! Prints help message based on provided command and options descriptions.
67 void PrintHelp() const;
69 //! Parses argument list (skips the command name); assigns local arguments to each option.
70 void Parse (Standard_Integer theArgsNb, const char* const* theArgVec);
72 //! Gets an option name by its access key
73 //! @param theOptionKey the access key of the option which name is to be found
74 //! @retuan a name of the option with the given access key
75 std::string GetOptionNameByKey (ViewerTest_CommandOptionKey theOptionKey) const;
77 //! Gets a set of used options
78 //! @return a set of used options
79 ViewerTest_CommandOptionKeySet GetUsedOptions() const;
81 //! Tests if there were no command line options provided
82 //! @return true if no command line options were provided, or false otherwise
83 bool HasNoOption() const;
85 //! Tests if the unnamed command line option was provided
86 //! @return true if the unnamed command line option was provided, or false otherwise
87 bool HasUnnamedOption() const;
89 //! Tests if only unnamed command line option was provided
90 //! @return true if only unnamed command line option was provided, or false otherwise
91 bool HasOnlyUnnamedOption() const;
93 //! Checks if option was used with given minimal number of arguments.
94 //! Prints error message if isFatal flag was set.
95 //! @param theOptionName the name of the option to be checked
96 //! @param theMandatoryArgsNb the number of mandatory arguments
97 //! @param isFatal the flag that controls printing of an error message
98 //! @return true if an option was set, or false otherwise
99 bool HasOption (const std::string& theOptionName,
100 std::size_t theMandatoryArgsNb = 0,
101 bool isFatal = Standard_False) const;
103 //! Checks if option was used with given minimal number of arguments.
104 //! Prints error message if isFatal flag was set.
105 //! @param theOptionKey the access key of the option to be checked
106 //! @param theMandatoryArgsNb the number of mandatory arguments
107 //! @param isFatal the flag that controls printing of an error message
108 //! @return true if an option was set, or false otherwise
109 bool HasOption (ViewerTest_CommandOptionKey theOptionKey,
110 std::size_t theMandatoryArgsNb = 0,
111 bool isFatal = Standard_False) const;
113 //! Gets a number of option arguments
114 //! @param theOptionName the name of the option
115 //! @return a number of option arguments, or 0 if option was not used
116 Standard_Integer GetNumberOfOptionArguments (const std::string& theOptionName) const;
118 //! Gets a number of option arguments
119 //! @param theOptionKey the access key of the option
120 //! @return a number of option arguments, or 0 if option was not used
121 Standard_Integer GetNumberOfOptionArguments (ViewerTest_CommandOptionKey theOptionKey) const;
123 //! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
124 //! @param theOptionName the name of the option which argument is to be accessed
125 //! @param theArgumentIndex the index of an accessed argument
126 //! @param theOptionArgument an argument of the option with the given name
127 //! @return true if an access was successful, or false otherwise
128 bool Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex, std::string& theOptionArgument) const;
130 //! Accesses a local argument with the index 'theArgumentIndex' of the option with the key 'theOptionKey'.
131 //! @param theOptionKey the access key of the option which argument is to be accessed
132 //! @param theArgumentIndex the index of an accessed argument
133 //! @param theOptionArgument an argument of the option with the given key
134 //! @return true if an access was successful, or false otherwise
135 bool Arg (ViewerTest_CommandOptionKey theOptionKey,
136 Standard_Integer theArgumentIndex,
137 std::string& theOptionArgument) const;
139 //! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
140 //! @param theOptionName the name of the option which argument is to be accessed
141 //! @param theArgumentIndex the index of an accessed argument
142 //! @return an argument of the option with the given name
143 std::string Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex) const;
145 //! Accesses a local argument with the index 'theArgumentIndex' of the option with the key 'theOptionKey'.
146 //! @param theOptionKey the access key of the option which argument is to be accessed
147 //! @param theArgumentIndex the index of an accessed argument
148 //! @return an argument of the option with the given key
149 std::string Arg (ViewerTest_CommandOptionKey theOptionKey, Standard_Integer theArgumentIndex) const;
151 // Interprets arguments of option 'theOptionName' as float vector starting with index 'theArgumentIndex'.
152 Graphic3d_Vec3 ArgVec3f (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
154 // Interprets arguments of option 'theOptionName' as double vector starting with index 'theArgumentIndex'.
155 Graphic3d_Vec3d ArgVec3d (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
157 // Interprets arguments of option 'theOptionName' as gp vector starting with index 'theArgumentIndex'.
158 gp_Vec ArgVec (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
160 // Interprets arguments of option 'theOptionName' as gp vector starting with index 'theArgumentIndex'.
161 gp_Pnt ArgPnt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
163 // Interprets arguments of option 'theOptionName' as double at index 'theArgumentIndex'.
164 Standard_Real ArgDouble (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
166 // Interprets arguments of option 'theOptionName' as float at index 'theArgumentIndex'.
167 Standard_ShortReal ArgFloat (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
169 // Interprets arguments of option 'theOptionName' as integer at index 'theArgumentIndex'.
170 Standard_Integer ArgInt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
172 // Interprets arguments of option 'theOptionName' as boolean at index 'theArgumentIndex'.
173 bool ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
175 //! Interprets arguments of the option 'theOptionName' with the index 'theArgumentIndex' as an RGB(A) color object.
176 //! @tparam theColor the type of a resulting RGB(A) color object
177 //! @param theOptionName the name of the option which arguments are to be interpreted
178 //! @param theArgumentIndex the index of the first argument to be interpreted
179 //! (will be promoted to the next argument after the block of interpreted arguments)
180 //! @param theColor a color that is an interpretation of argument(s) of the option with the given name
181 //! @return true if an interpretation was successful, or false otherwise
182 template <typename TheColor>
183 bool ArgColor (const std::string& theOptionName, Standard_Integer& theArgumentIndex, TheColor& theColor) const;
185 //! Interprets arguments of the option with the key 'theOptionKey' as an RGB(A) color object.
186 //! @tparam theColor the type of a resulting RGB(A) color object
187 //! @param theOptionKey the access key of the option which arguments are to be interpreted
188 //! @param theArgumentIndex the index of the first argument to be interpreted
189 //! (will be promoted to the next argument after the block of interpreted arguments)
190 //! @param theColor a color that is an interpretation of argument(s) of the option with the given name
191 //! @return true if an interpretation was successful, or false otherwise
192 template <typename TheColor>
193 bool ArgColor (ViewerTest_CommandOptionKey theOptionKey,
194 Standard_Integer& theArgumentIndex,
195 TheColor& theColor) const;
198 //! A list of aliases to a command option name
199 typedef std::vector<std::string> OptionAliases;
201 //! A map from all possible option names to option access keys
202 typedef std::map<std::string, ViewerTest_CommandOptionKey> OptionMap;
204 //! A map from keys of used options to their indices in the storage
205 typedef std::map<ViewerTest_CommandOptionKey, std::size_t> UsedOptionMap;
207 //! A list of command option arguments
208 typedef std::vector<std::string> OptionArguments;
210 //! A storage of arguments of different command options
211 typedef std::vector<OptionArguments> OptionArgumentsStorage;
213 //! A full description of a command option
216 std::string Name; //!< A command option name
217 OptionAliases Aliases; //!< A list of aliases to a command option name
218 std::string Description; //!< A text description of a command option
221 // A storage of command options descriptions
222 typedef std::vector<CommandOption> CommandOptionStorage;
224 // A list of raw string arguments
225 typedef std::vector<const char*> RawStringArguments;
227 //! Description of command.
228 std::string myDescription;
230 //! Container which stores option objects.
231 std::vector<CommandOption> myOptionStorage;
233 //! Map from all possible option names to option access keys (that are indices in myOptionStorage)
234 OptionMap myOptionMap;
236 //! Map from keys of used options to their indices in the option arguments storage
237 UsedOptionMap myUsedOptionMap;
239 //! Container which stores the arguments of all used options
240 OptionArgumentsStorage myOptionArgumentStorage;
242 //! Gets an access key of the option
243 //! @param theOptionName the name of the option which key is to be found
244 //! @param theOptionKey an access key of the option with the given name
245 //! @return true if the given option was found, or false otherwise
246 bool findOptionKey (const std::string& theOptionName, ViewerTest_CommandOptionKey& theOptionKey) const;
248 //! Gets an index of an option that was used
249 //! @param theOptionKey the access key of the used option which index is to be found
250 //! @param theUsedOptionIndex an index of the used option with the given access key
251 //! @return true if the given option was not found or not used, or false otherwise
252 bool findUsedOptionIndex (ViewerTest_CommandOptionKey theOptionKey, std::size_t& theUsedOptionIndex) const;
254 //! Gets an index of an option that was used
255 //! @param theOptionName the name of the used option which index is to be found
256 //! @param theUsedOptionIndex an index of the used option with the given name
257 //! @return true if the given option was not found or not used, or false otherwise
258 bool findUsedOptionIndex (const std::string& theOptionName, std::size_t& theUsedOptionIndex) const;
260 //! Adds the option that is used in the passed command line parameters
261 //! @param theNewUsedOptionKey the access key of the adding option
262 //! @return an index of a newly added option
263 std::size_t addUsedOption (ViewerTest_CommandOptionKey theNewUsedOptionKey);
265 //! Gets an index of an option that was used
266 //! @param theOptionName the name of the used option which index is to be found
267 //! @param theUsedOptionIndex an index of the used option with the given name
268 //! @return true if the given option was not found or not used, or false otherwise
269 RawStringArguments getRawStringArguments (std::size_t theUsedOptionIndex) const;
272 #endif // _ViewerTest_CmdParser_HeaderFile