00001 /*!@file Component/CmdlineOptionManager.H OptionManager implementation for command-line parsing */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Component/CmdlineOptionManager.H $ 00035 // $Id: CmdlineOptionManager.H 8417 2007-05-22 20:25:39Z rjpeters $ 00036 // 00037 00038 #ifndef COMPONENT_CMDLINEOPTIONMANAGER_H_DEFINED 00039 #define COMPONENT_CMDLINEOPTIONMANAGER_H_DEFINED 00040 00041 #include "Component/OptionManager.H" 00042 00043 //! OptionManager implementation for command-line parsing 00044 class CmdlineOptionManager : public OptionManager 00045 { 00046 public: 00047 CmdlineOptionManager(); 00048 00049 virtual ~CmdlineOptionManager(); 00050 00051 // ###################################################################### 00052 /*! @name Model option database */ 00053 //@{ 00054 00055 //! Only allow ModelOptionDef's that match the given mask 00056 /*! This mask is used to filter requestOption() calls -- if the 00057 ModelOptionDef's filterFlag isn't part of ModelManager's current 00058 mask, then the requestOption() is ignored. */ 00059 void allowOptions(const int mask); 00060 00061 //! ModelComponent objects may request a command-line option here 00062 /*! @param p the ModelParam data member of c corresponding to the 00063 option 00064 00065 @param useMyVal if true, we will do a setOptionValString() using 00066 the current value of the parameter, so that this value 00067 will be propagated to all OptionedModelParam objects that 00068 have requested this option and will become the new 00069 command-line default value. If false, the ModelParam p 00070 will be updated to the current value of the option. So, 00071 use useMyVal=true if your component wishes to push its 00072 current parameter value as the default for everyone using 00073 this option, and use false if your component wishes to 00074 use the current default instead. */ 00075 virtual void requestOption(OptionedModelParam& p, 00076 const bool useMyVal = false); 00077 00078 //! Request the removal of a param from the command-line options 00079 /*! @param p the ModelParam data member of c corresponding to the option */ 00080 virtual void unRequestOption(OptionedModelParam& p); 00081 00082 //! Users may request model option aliases 00083 /*! @param name the name of the option alias, from ModelOptionDefs.C */ 00084 virtual void requestOptionAlias(const ModelOptionDef* def); 00085 00086 //! Set an option value 00087 /*! All ModelParam objects associated this ModelOptionDef will be 00088 updated with the new value. Will throw a fatal error if the 00089 model has been started (see ModelComponent::start()). */ 00090 virtual void setOptionValString(const ModelOptionDef* def, 00091 const std::string& val); 00092 00093 //! Get an option value 00094 /*! The value returned here is the value currently held by the 00095 ModelManager. This value is updated in three cases: 1) when a 00096 new component requests the option using requestOption() with 00097 useMyVal true; 2) when setOptionValString() is called, and 3) 00098 when the command-line arguments are parsed by ParseCommandLine, 00099 if a value for this option has been specified in the command 00100 line. If ModelComponent objects modify their local ModelParam 00101 values for their local parameter with this name, the value 00102 returned here will not be affected. */ 00103 virtual std::string getOptionValString(const ModelOptionDef* def); 00104 00105 //! Check if anybody has requested the given option. 00106 virtual bool isOptionRequested(const ModelOptionDef* def) const; 00107 00108 //@} 00109 00110 00111 // ###################################################################### 00112 /*! @name Alternative access to internal options 00113 00114 These functions may be useful for alternative methods of 00115 displaying the help for command-line options (e.g. in the Qt 00116 ModelManagerWizard). 00117 */ 00118 //@{ 00119 00120 //! Get the number of ModelOptionDef objects in our list. 00121 virtual uint numOptionDefs() const; 00122 00123 //! Get ModelOptionDef objects from our list, up to the size of the given array. 00124 /*! Returns the number of array entries that were actually filled 00125 with valid ModelOptionDef pointers. */ 00126 virtual uint getOptionDefs(const ModelOptionDef** arr, uint narr); 00127 00128 //! Get the ModelOptionDef for the given name. 00129 /*! Aborts if there is no such ModelOptionDef. */ 00130 virtual const ModelOptionDef* findOptionDef(const char* name) const; 00131 00132 //! Query if the given ModelOptionDef is used in this program. 00133 virtual bool isOptionDefUsed(const ModelOptionDef* def) const; 00134 00135 //@} 00136 00137 00138 // ###################################################################### 00139 /*! @name Command-line parsing */ 00140 //@{ 00141 00142 //! Parse all command-line options and set our params 00143 /*! This function will create a set of command-line options from our 00144 current collection of OptionedModelParam objects. It will then 00145 parse the command-line arguments and set the various option 00146 values accordingly, using a call to setOptionValString(). Any 00147 left-over args (i.e., not formatted as options) will be 00148 available through getExtraArg(), and if the number of such extra 00149 args is not within the specified minarg/maxarg bounds, a usage 00150 message and optionlist will be printed out (also printed out if 00151 a '--help' option is encountered). To decide which options 00152 should be exported by your various ModelComponent objects, 00153 typically you will call exportOptions() on the manager, which 00154 will propagate the call to all registered components. Here we 00155 have an additional default behavior that if exportOptions() has 00156 not been called on us, we will call it with an order to export 00157 all options, just before we start parsing the command-line. So, 00158 if you want to export all options, you don't have to do 00159 anything; if you want to only export special classes of options, 00160 then you will need to call exportOptions() manually some time 00161 before you parse the command-line. Returns true upon fully 00162 successful parse. 00163 00164 @param argc the number of command-line args 00165 @param argv the command-line args 00166 @param usage a textual description of the expected usage format, 00167 describing only what the non-option arguments should be (e.g., 00168 "<input.ppm> <output.ppm>") 00169 @param minarg the minimum number of non-optional arguments 00170 @param maxarg the maximum number of non-optional args, or -1 if 00171 unbounded */ 00172 bool parseCommandLine(const int argc, const char **argv, 00173 const char *usage, 00174 const int minarg, const int maxarg); 00175 00176 //! The core of parseCommandLine(). 00177 /*! This may be called recursively to handle command-line 00178 aliases. Note: beware that the first element of argv will be 00179 ignored (as it usually is the program name). */ 00180 bool parseCommandLineCore(const int argc, const char** argv, 00181 const std::string& context = std::string()); 00182 00183 bool parseCommandLineCore(const char* args, 00184 const std::string& context = std::string()); 00185 00186 00187 //! Get the total number of command-line arguments 00188 /*! Includes argv[0] as well as both option and non-option args. */ 00189 uint numArgs() const; 00190 00191 //! Get a command-line argument, by number 00192 /*! Includes argv[0] as well as both option and non-option args. */ 00193 std::string getArg(const uint num) const; 00194 00195 //! Get the number of available extra (non-option) command-line args 00196 uint numExtraArgs() const; 00197 00198 //! Get an extra command-line arg, by number 00199 std::string getExtraArg(const uint num) const; 00200 00201 //! Clear our list of leftover command-line arguments 00202 void clearExtraArgs(); 00203 00204 //@} 00205 00206 private: 00207 CmdlineOptionManager(const CmdlineOptionManager&); // not implemented 00208 CmdlineOptionManager& operator=(const CmdlineOptionManager&); // not implemented 00209 00210 struct Impl; 00211 Impl* const rep; 00212 }; 00213 00214 // ###################################################################### 00215 /* So things look consistent in everyone's emacs... */ 00216 /* Local Variables: */ 00217 /* mode: c++ */ 00218 /* indent-tabs-mode: nil */ 00219 /* End: */ 00220 00221 #endif // COMPONENT_CMDLINEOPTIONMANAGER_H_DEFINED