Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 09:19:33

0001 // Created on: 2015-03-15
0002 // Created by: Danila ULYANOV
0003 // Copyright (c) 2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _ViewerTest_CmdParser_HeaderFile
0017 #define _ViewerTest_CmdParser_HeaderFile
0018 
0019 #include <NCollection_Vec3.hxx>
0020 #include <Standard_TypeDef.hxx>
0021 
0022 #include <map>
0023 #include <vector>
0024 #include <set>
0025 #include <string>
0026 
0027 class gp_Vec;
0028 class gp_Pnt;
0029 
0030 //! A key for a command line option used for a ViewerTest_CmdParser work
0031 typedef std::size_t ViewerTest_CommandOptionKey;
0032 
0033 //! A set of keys for command-line options
0034 typedef std::set<ViewerTest_CommandOptionKey> ViewerTest_CommandOptionKeySet;
0035 
0036 //! Command parser.
0037 class ViewerTest_CmdParser
0038 {
0039 public:
0040   //! The key of the unnamed command option
0041   static const std::size_t THE_UNNAMED_COMMAND_OPTION_KEY;
0042 
0043   //! The key of the help command option
0044   static const std::size_t THE_HELP_COMMAND_OPTION_KEY;
0045 
0046   //! Initializes help option.
0047   //! @param theDescription the description of the command
0048   ViewerTest_CmdParser(const std::string& theDescription = std::string());
0049 
0050   //! Sets description for command.
0051   void SetDescription(const std::string& theDescription) { myDescription = theDescription; }
0052 
0053   //! Adds option to available option list. Several names may be provided if separated with '|'.
0054   //! @param theOptionNames the list of possible option names separated with '|'
0055   //! (the first name is main, the other names are aliases)
0056   //! @param theOptionDescription the description of the option
0057   //! @return an access key of the newly added option
0058   ViewerTest_CommandOptionKey AddOption(const std::string& theOptionNames,
0059                                         const std::string& theOptionDescription = std::string());
0060 
0061   //! Prints help message based on provided command and options descriptions.
0062   void PrintHelp() const;
0063 
0064   //! Parses argument list (skips the command name); assigns local arguments to each option.
0065   void Parse(int theArgsNb, const char* const* theArgVec);
0066 
0067   //! Gets an option name by its access key
0068   //! @param theOptionKey the access key of the option which name is to be found
0069   //! @retuan a name of the option with the given access key
0070   std::string GetOptionNameByKey(ViewerTest_CommandOptionKey theOptionKey) const;
0071 
0072   //! Gets a set of used options
0073   //! @return a set of used options
0074   ViewerTest_CommandOptionKeySet GetUsedOptions() const;
0075 
0076   //! Tests if there were no command line options provided
0077   //! @return true if no command line options were provided, or false otherwise
0078   bool HasNoOption() const;
0079 
0080   //! Tests if the unnamed command line option was provided
0081   //! @return true if the unnamed command line option was provided, or false otherwise
0082   bool HasUnnamedOption() const;
0083 
0084   //! Tests if only unnamed command line option was provided
0085   //! @return true if only unnamed command line option was provided, or false otherwise
0086   bool HasOnlyUnnamedOption() const;
0087 
0088   //! Checks if option was used with given minimal number of arguments.
0089   //! Prints error message if isFatal flag was set.
0090   //! @param theOptionName the name of the option to be checked
0091   //! @param theMandatoryArgsNb the number of mandatory arguments
0092   //! @param isFatal the flag that controls printing of an error message
0093   //! @return true if an option was set, or false otherwise
0094   bool HasOption(const std::string& theOptionName,
0095                  std::size_t        theMandatoryArgsNb = 0,
0096                  bool               isFatal            = false) const;
0097 
0098   //! Checks if option was used with given minimal number of arguments.
0099   //! Prints error message if isFatal flag was set.
0100   //! @param theOptionKey the access key of the option to be checked
0101   //! @param theMandatoryArgsNb the number of mandatory arguments
0102   //! @param isFatal the flag that controls printing of an error message
0103   //! @return true if an option was set, or false otherwise
0104   bool HasOption(ViewerTest_CommandOptionKey theOptionKey,
0105                  std::size_t                 theMandatoryArgsNb = 0,
0106                  bool                        isFatal            = false) const;
0107 
0108   //! Gets a number of option arguments
0109   //! @param theOptionName the name of the option
0110   //! @return a number of option arguments, or 0 if option was not used
0111   int GetNumberOfOptionArguments(const std::string& theOptionName) const;
0112 
0113   //! Gets a number of option arguments
0114   //! @param theOptionKey the access key of the option
0115   //! @return a number of option arguments, or 0 if option was not used
0116   int GetNumberOfOptionArguments(ViewerTest_CommandOptionKey theOptionKey) const;
0117 
0118   //! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
0119   //! @param theOptionName the name of the option which argument is to be accessed
0120   //! @param theArgumentIndex the index of an accessed argument
0121   //! @param theOptionArgument an argument of the option with the given name
0122   //! @return true if an access was successful, or false otherwise
0123   bool Arg(const std::string& theOptionName,
0124            int                theArgumentIndex,
0125            std::string&       theOptionArgument) const;
0126 
0127   //! Accesses a local argument with the index 'theArgumentIndex' of the option with the key
0128   //! 'theOptionKey'.
0129   //! @param theOptionKey the access key of the option which argument is to be accessed
0130   //! @param theArgumentIndex the index of an accessed argument
0131   //! @param theOptionArgument an argument of the option with the given key
0132   //! @return true if an access was successful, or false otherwise
0133   bool Arg(ViewerTest_CommandOptionKey theOptionKey,
0134            int                         theArgumentIndex,
0135            std::string&                theOptionArgument) const;
0136 
0137   //! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
0138   //! @param theOptionName the name of the option which argument is to be accessed
0139   //! @param theArgumentIndex the index of an accessed argument
0140   //! @return an argument of the option with the given name
0141   std::string Arg(const std::string& theOptionName, int theArgumentIndex) const;
0142 
0143   //! Accesses a local argument with the index 'theArgumentIndex' of the option with the key
0144   //! 'theOptionKey'.
0145   //! @param theOptionKey the access key of the option which argument is to be accessed
0146   //! @param theArgumentIndex the index of an accessed argument
0147   //! @return an argument of the option with the given key
0148   std::string Arg(ViewerTest_CommandOptionKey theOptionKey, int theArgumentIndex) const;
0149 
0150   // Interprets arguments of option 'theOptionName' as float vector starting with index
0151   // 'theArgumentIndex'.
0152   NCollection_Vec3<float> ArgVec3f(const std::string& theOptionName,
0153                                    const int          theArgumentIndex = 0) const;
0154 
0155   // Interprets arguments of option 'theOptionName' as double vector starting with index
0156   // 'theArgumentIndex'.
0157   NCollection_Vec3<double> ArgVec3d(const std::string& theOptionName,
0158                                     const int          theArgumentIndex = 0) const;
0159 
0160   // Interprets arguments of option 'theOptionName' as gp vector starting with index
0161   // 'theArgumentIndex'.
0162   gp_Vec ArgVec(const std::string& theOptionName, const int theArgumentIndex = 0) const;
0163 
0164   // Interprets arguments of option 'theOptionName' as gp vector starting with index
0165   // 'theArgumentIndex'.
0166   gp_Pnt ArgPnt(const std::string& theOptionName, const int theArgumentIndex = 0) const;
0167 
0168   // Interprets arguments of option 'theOptionName' as double at index 'theArgumentIndex'.
0169   double ArgDouble(const std::string& theOptionName, const int theArgumentIndex = 0) const;
0170 
0171   // Interprets arguments of option 'theOptionName' as float at index 'theArgumentIndex'.
0172   float ArgFloat(const std::string& theOptionName, const int theArgumentIndex = 0) const;
0173 
0174   // Interprets arguments of option 'theOptionName' as integer at index 'theArgumentIndex'.
0175   int ArgInt(const std::string& theOptionName, const int theArgumentIndex = 0) const;
0176 
0177   // Interprets arguments of option 'theOptionName' as boolean at index 'theArgumentIndex'.
0178   bool ArgBool(const std::string& theOptionName, const int theArgumentIndex = 0) const;
0179 
0180   //! Interprets arguments of the option 'theOptionName' with the index 'theArgumentIndex' as an
0181   //! RGB(A) color object.
0182   //! @tparam theColor the type of a resulting RGB(A) color object
0183   //! @param theOptionName the name of the option which arguments are to be interpreted
0184   //! @param theArgumentIndex the index of the first argument to be interpreted
0185   //! (will be promoted to the next argument after the block of interpreted arguments)
0186   //! @param theColor a color that is an interpretation of argument(s) of the option with the given
0187   //! name
0188   //! @return true if an interpretation was successful, or false otherwise
0189   template <typename TheColor>
0190   bool ArgColor(const std::string& theOptionName, int& theArgumentIndex, TheColor& theColor) const;
0191 
0192   //! Interprets arguments of the option with the key 'theOptionKey' as an RGB(A) color object.
0193   //! @tparam theColor the type of a resulting RGB(A) color object
0194   //! @param theOptionKey the access key of the option which arguments are to be interpreted
0195   //! @param theArgumentIndex the index of the first argument to be interpreted
0196   //! (will be promoted to the next argument after the block of interpreted arguments)
0197   //! @param theColor a color that is an interpretation of argument(s) of the option with the given
0198   //! name
0199   //! @return true if an interpretation was successful, or false otherwise
0200   template <typename TheColor>
0201   bool ArgColor(ViewerTest_CommandOptionKey theOptionKey,
0202                 int&                        theArgumentIndex,
0203                 TheColor&                   theColor) const;
0204 
0205 private:
0206   //! A list of aliases to a command option name
0207   typedef std::vector<std::string> OptionAliases;
0208 
0209   //! A map from all possible option names to option access keys
0210   typedef std::map<std::string, ViewerTest_CommandOptionKey> OptionMap;
0211 
0212   //! A map from keys of used options to their indices in the storage
0213   typedef std::map<ViewerTest_CommandOptionKey, std::size_t> UsedOptionMap;
0214 
0215   //! A list of command option arguments
0216   typedef std::vector<std::string> OptionArguments;
0217 
0218   //! A storage of arguments of different command options
0219   typedef std::vector<OptionArguments> OptionArgumentsStorage;
0220 
0221   //! A full description of a command option
0222   struct CommandOption
0223   {
0224     std::string   Name;        //!< A command option name
0225     OptionAliases Aliases;     //!< A list of aliases to a command option name
0226     std::string   Description; //!< A text description of a command option
0227   };
0228 
0229   // A storage of command options descriptions
0230   typedef std::vector<CommandOption> CommandOptionStorage;
0231 
0232   // A list of raw string arguments
0233   typedef std::vector<const char*> RawStringArguments;
0234 
0235   //! Description of command.
0236   std::string myDescription;
0237 
0238   //! Container which stores option objects.
0239   std::vector<CommandOption> myOptionStorage;
0240 
0241   //! Map from all possible option names to option access keys (that are indices in myOptionStorage)
0242   OptionMap myOptionMap;
0243 
0244   //! Map from keys of used options to their indices in the option arguments storage
0245   UsedOptionMap myUsedOptionMap;
0246 
0247   //! Container which stores the arguments of all used options
0248   OptionArgumentsStorage myOptionArgumentStorage;
0249 
0250   //! Gets an access key of the option
0251   //! @param theOptionName the name of the option which key is to be found
0252   //! @param theOptionKey an access key of the option with the given name
0253   //! @return true if the given option was found, or false otherwise
0254   bool findOptionKey(const std::string&           theOptionName,
0255                      ViewerTest_CommandOptionKey& theOptionKey) const;
0256 
0257   //! Gets an index of an option that was used
0258   //! @param theOptionKey the access key of the used option which index is to be found
0259   //! @param theUsedOptionIndex an index of the used option with the given access key
0260   //! @return true if the given option was not found or not used, or false otherwise
0261   bool findUsedOptionIndex(ViewerTest_CommandOptionKey theOptionKey,
0262                            std::size_t&                theUsedOptionIndex) const;
0263 
0264   //! Gets an index of an option that was used
0265   //! @param theOptionName the name of the used option which index is to be found
0266   //! @param theUsedOptionIndex an index of the used option with the given name
0267   //! @return true if the given option was not found or not used, or false otherwise
0268   bool findUsedOptionIndex(const std::string& theOptionName, std::size_t& theUsedOptionIndex) const;
0269 
0270   //! Adds the option that is used in the passed command line parameters
0271   //! @param theNewUsedOptionKey the access key of the adding option
0272   //! @return an index of a newly added option
0273   std::size_t addUsedOption(ViewerTest_CommandOptionKey theNewUsedOptionKey);
0274 
0275   //! Gets an index of an option that was used
0276   //! @param theOptionName the name of the used option which index is to be found
0277   //! @param theUsedOptionIndex an index of the used option with the given name
0278   //! @return true if the given option was not found or not used, or false otherwise
0279   RawStringArguments getRawStringArguments(std::size_t theUsedOptionIndex) const;
0280 };
0281 
0282 #endif // _ViewerTest_CmdParser_HeaderFile