Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:31

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