cgma
GetLongOpt.hpp
Go to the documentation of this file.
00001 //- Class: GetLongOpt
00002 //- Description: GetLongOpt manages the definition and parsing of 
00003 //- long options. Long options can be abbreviated as long as there 
00004 //- is no ambiguity. If an option requires a value, the value should
00005 //- be separated from the option either by whitespace or an "=".
00006 //- 
00007 //- Other features: $
00008 //- o   GetLongOpt can be used to parse options given through environments.$
00009 //- o   GetLongOpt provides a usage function to print usage.$
00010 //- o   Flags & options with optional or mandatory values are supported.$
00011 //- o   The option marker ('-' in Unix) can be customized.$
00012 //- o   Parsing of command line returns optind (see getopt(3)).$
00013 //- o   Descriptive error messages.$
00014 //-
00015 //- Author: S Manoharan. Advanced Computer Research Institute. Lyon. France
00016 //- Owner: Greg Sjaardema
00017 //- Checked By:
00018 //- Version $Id: 
00019 
00020 #ifndef GETLONGOPT_HPP
00021 #define GETLONGOPT_HPP
00022 
00023 #include "CubitDefines.h"
00024 
00025 #include <iostream>
00026 
00027 #include <cstring>
00028 #include "CGMUtilConfigure.h"
00029 #include <vector>
00030 #include <map>
00031 #include "CubitString.hpp"
00032 
00033 class CUBIT_UTIL_EXPORT GetLongOpt {
00034 public:
00035   enum OptType { 
00036      Toggle, Valueless, OptionalValue, MandatoryValue
00037    };
00038 
00039   static const CubitString TOGGLE_ON;
00040   static const CubitString TOGGLE_OFF;
00041 
00042   GetLongOpt(const char optmark = '-'); //- Constructor
00043   //- Constructor for GetLongOpt takes an optional argument: the option
00044   //- marker. If unspecified, this defaults to '-', the standard (?)
00045   //- Unix option marker. 
00046 
00047   ~GetLongOpt();                        //- Destructor
00048   
00049   int parse(const std::vector<CubitString>& args, const CubitString& p = CubitString());
00050   //- A return
00051   //- value < 1 represents a parse error. Appropriate error messages
00052   //- are printed when errors are seen. {GetLongOpt::parse}, in its first
00053   //- form, takes two strings: the first one is the string to be
00054   //- parsed and the second one is a string to be prefixed to the
00055   //- parse errors. In ts second form, {GetLongOpt::parse} returns the
00056   //- the {optind} (see @i{getopt(3)}) if parsing is successful.
00057 
00058   int parse(const CubitString& opt_string, const CubitString &p);
00059 
00060   int enroll(const CubitString& opt, const OptType t,
00061        const CubitString& desc, const CubitString& val);
00062   //- Add an option to the list of valid command options.$
00063   //- {GetLongOpt::enroll} adds option specifications to its internal
00064   //- database. The first argument is the option sting. The second
00065   //- is an enum saying if the option is a flag ({GetLongOpt::Valueless}),
00066   //- if it requires a mandatory value ({GetLongOpt::MandatoryValue}) or
00067   //- if it takes an optional value ({GetLongOpt::OptionalValue}).
00068   //- The third argument is a string giving a brief description of
00069   //- the option. This description will be used by {GetLongOpt::usage}.
00070   //- GetLongOpt, for usage-printing, uses {$val} to represent values
00071   //- needed by the options. {<$val>} is a mandatory value and {[$val]}
00072   //- is an optional value. The final argument to {GetLongOpt::enroll}
00073   //- is the default string to be returned if the option is not
00074   //- specified. For flags (options with {Valueless}), use "" (empty
00075   //- string, or in fact any arbitrary string) for specifying {TRUE}
00076   //- and 0 (null pointer) to specify {FALSE}.
00077   
00078   CubitString retrieve(const CubitString& opt) const;
00079   //- Retrieve value of option$
00080   //- The values of the options that are enrolled in the database
00081   //- can be retrieved using {GetLongOpt::retrieve}. This returns a string
00082   //- and this string should be converted to whatever type you want.
00083   //- See @i{atoi}, @i{atof}, @i{atol} etc.
00084   //- If a "parse" is not done before retrieving all you will get
00085   //- are the default values you gave while enrolling!
00086   //- Ambiguities while retrieving (may happen when options are
00087   //- abbreviated) are resolved by taking the matching option that 
00088   //- was enrolled last. For example, -{v} will expand to {-verify}.$
00089   //- If you try to retrieve something you didn't enroll, you will
00090   //- get a warning message. 
00091 
00092   void options(std::ostream &outfile = std::cout) const;
00093   //- Print command line options only. Called by usage().
00094 
00095   void usage(std::ostream &outfile, const CubitString &p, const CubitString& u = CubitString()) const;
00096   //- Print usage information to {outfile}
00097 
00098 private:
00099   struct Cell {
00100      CubitString option;    //- option name
00101      OptType type;      //- option type
00102      CubitString description;   //- a description of option
00103      CubitString value;         //- value of option (string)
00104      int wasSet;                //- 0 if not set by user, 1 if set
00105      
00106      Cell() { option = description = value = 0; wasSet = 0; }
00107    };
00108 
00109   typedef std::map<CubitString, Cell> Table;
00110   Table mTable;
00111 
00112   char optmarker;       //- option marker
00113   
00114   int setcell(Cell &c, const CubitString &valtoken, const CubitString &nexttoken, const CubitString &name);
00115 };
00116 
00117 #endif // GETLONGOPT_HPP
00118 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines