CommandOption.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: CommandOption.hpp 2333 2010-02-25 01:35:45Z yanweignss $"
00002 
00003 
00004 
00010 #ifndef COMMANDOPTION_HPP
00011 #define COMMANDOPTION_HPP
00012 
00013 //============================================================================
00014 //
00015 //  This file is part of GPSTk, the GPS Toolkit.
00016 //
00017 //  The GPSTk is free software; you can redistribute it and/or modify
00018 //  it under the terms of the GNU Lesser General Public License as published
00019 //  by the Free Software Foundation; either version 2.1 of the License, or
00020 //  any later version.
00021 //
00022 //  The GPSTk is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU Lesser General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU Lesser General Public
00028 //  License along with GPSTk; if not, write to the Free Software Foundation,
00029 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030 //  
00031 //  Copyright 2004, The University of Texas at Austin
00032 //
00033 //============================================================================
00034 
00035 //============================================================================
00036 //
00037 //This software developed by Applied Research Laboratories at the University of
00038 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00039 //Department of Defense. The U.S. Government retains all rights to use,
00040 //duplicate, distribute, disclose, or release this software. 
00041 //
00042 //Pursuant to DoD Directive 523024 
00043 //
00044 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00045 //                           release, distribution is unlimited.
00046 //
00047 //=============================================================================
00048 
00049 
00050 
00051 
00052 
00053 
00054 #ifdef __SUNPRO_CC
00055 #include "getopt.h"
00056 #elif defined (_AIX)
00057 #include "getopt.h"
00058 #else
00059 #include "getopt.h"
00060 #endif
00061 
00062 #include <string>
00063 #include <vector>
00064 
00065 namespace gpstk
00066 {
00069 
00070       // forward declaration
00071    class CommandOption;
00072    typedef std::vector<CommandOption*> CommandOptionVec;
00073 
00076    extern CommandOptionVec defaultCommandOptionList;
00077 
00120    class CommandOption
00121    {
00122    public:
00124       friend class CommandOptionParser;
00125 
00128       enum CommandOptionFlag
00129       {
00130          noArgument = 0,         
00131          hasArgument = 1         
00132       };
00133 
00137       enum CommandOptionType
00138       {
00139          trailingType, 
00140          stdType,      
00141          metaType      
00142       };
00143 
00158       CommandOption(const CommandOptionFlag of, 
00159                     const CommandOptionType ot,
00160                     const char shOpt, 
00161                     const std::string& loOpt, 
00162                     const std::string& desc,
00163                     const bool req = false,
00164                     CommandOptionVec& optVectorList =  
00165                        defaultCommandOptionList)
00166             : optFlag(of),  optType(ot),
00167               shortOpt(shOpt), longOpt(loOpt), description(desc),
00168               required(req), count(0), maxCount(0), order(0)
00169          {optVectorList.push_back(this);}
00170 
00173       CommandOption& setMaxCount(const unsigned long l)
00174          {maxCount = l; return *this;}
00175 
00178       virtual std::string getOptionString() const;
00179 
00182       std::string getFullOptionString() const;
00183 
00185       virtual std::string getArgString() const
00186       { return "ARG"; }
00187 
00189       struct option toGetoptLongOption() const;
00191       std::string toGetoptShortOption() const;
00192 
00197       virtual unsigned long getCount() const { return count; }
00198 
00203       std::vector<std::string> getValue() const { return value; }
00204 
00207          //  If it can be repeated, this order represents the order of
00209       unsigned int getOrder() const { return order; }
00210 
00212       std::ostream& dumpValue(std::ostream& out) const;
00213 
00215       std::string getDescription() const;
00216 
00225       virtual std::string checkArguments();
00226 
00228       virtual ~CommandOption() {}
00229 
00230    protected:
00232       CommandOptionFlag optFlag;
00235       CommandOptionType optType;
00237       char shortOpt;
00239       std::string longOpt;
00241       std::string description;
00243       std::vector<std::string> value;
00245       bool required;
00248       unsigned long count;
00251       unsigned long maxCount;
00253       unsigned long order;
00254 
00256       CommandOption() {}
00257    };
00258 
00260    class RequiredOption : public CommandOption
00261    {
00262    public:
00264       RequiredOption(const CommandOptionFlag of,
00265                      const CommandOptionType ot,
00266                      const char shOpt, 
00267                      const std::string& loOpt, 
00268                      const std::string& desc)
00269             : CommandOption(of, ot, shOpt, loOpt, desc, true)
00270          {}
00271 
00273       virtual ~RequiredOption() {}
00274 
00275    protected:
00277       RequiredOption() {}
00278    };
00279 
00281    class CommandOptionNoArg : public CommandOption
00282    {
00283    public:
00285       CommandOptionNoArg(const char shOpt, 
00286                          const std::string& loOpt, 
00287                          const std::string& desc,
00288                          const bool required = false)
00289             : CommandOption(noArgument, stdType, shOpt, loOpt, desc, required)
00290          {}
00291          
00293       virtual ~CommandOptionNoArg() {}
00295       operator bool() const throw() { return (getCount() != 0); }
00296 
00297    protected:
00299       CommandOptionNoArg() {}
00300    };
00301 
00303    class CommandOptionWithArg : public CommandOption
00304    {
00305    public:
00307       CommandOptionWithArg(const CommandOptionType ot,
00308                            const char shOpt, 
00309                            const std::string& loOpt, 
00310                            const std::string& desc,
00311                            const bool required = false)
00312             : CommandOption(hasArgument, ot, shOpt, loOpt, desc, required)
00313          {}
00314 
00316       virtual ~CommandOptionWithArg() {}
00317 
00318    protected:
00320       CommandOptionWithArg() {}
00321    };
00322 
00324    class CommandOptionWithAnyArg : public CommandOptionWithArg
00325    {
00326    public:
00328       CommandOptionWithAnyArg(const char shOpt, 
00329                               const std::string& loOpt, 
00330                               const std::string& desc,
00331                               const bool required = false)
00332             : CommandOptionWithArg(stdType, shOpt, loOpt, desc, required)
00333          {}
00334 
00336       virtual ~CommandOptionWithAnyArg() {}
00337 
00338    protected:
00340       CommandOptionWithAnyArg() {}
00341    };
00342 
00344    class CommandOptionWithStringArg : public CommandOptionWithArg
00345    {
00346    public:
00348       CommandOptionWithStringArg(const char shOpt, 
00349                                  const std::string& loOpt, 
00350                                  const std::string& desc,
00351                                  const bool required = false)
00352             : CommandOptionWithArg(stdType, shOpt, loOpt, desc, required)
00353          {}
00354 
00356       virtual ~CommandOptionWithStringArg() {}
00357 
00358       virtual std::string checkArguments();
00359 
00361       virtual std::string getArgString() const
00362       { return "<alpha>"; }
00363 
00364    protected:
00366       CommandOptionWithStringArg() {}
00367    };
00368 
00370    class CommandOptionWithNumberArg : public CommandOptionWithArg
00371    {
00372    public:
00374       CommandOptionWithNumberArg(const char shOpt, 
00375                                  const std::string& loOpt, 
00376                                  const std::string& desc,
00377                                  const bool required = false)
00378             : CommandOptionWithArg(stdType, shOpt, loOpt, desc, required)
00379          {}
00380 
00382       virtual ~CommandOptionWithNumberArg() {}
00383 
00384       virtual std::string checkArguments();
00385 
00387       virtual std::string getArgString() const
00388       { return "NUM"; }
00389 
00390    protected:
00392       CommandOptionWithNumberArg() {}
00393    };
00394 
00404    class CommandOptionRest : public CommandOptionWithArg
00405    {
00406    public:
00414       CommandOptionRest(const std::string& desc,
00415                         const bool required = false)
00416             : CommandOptionWithArg(trailingType, 0, "", desc, required)
00417       {}
00418       
00420       virtual ~CommandOptionRest() {}
00421       
00422       virtual std::string checkArguments();
00423 
00424    protected:
00426       CommandOptionRest() {}
00427    };
00428 
00438    class CommandOptionOneOf : public CommandOption
00439    {
00440    public:
00445       CommandOptionOneOf()
00446             : CommandOption(noArgument, metaType, 0, "", "")
00447       {}
00448       
00450       virtual ~CommandOptionOneOf() {}
00451 
00452       virtual std::string checkArguments();
00453 
00455       void addOption(CommandOption* opt)
00456       { optionVec.push_back(opt); }
00457 
00459       CommandOption* whichOne() const;
00460       
00461    protected:
00462       CommandOptionVec optionVec;
00463    };
00464 
00474    class CommandOptionAllOf : public CommandOptionOneOf
00475    {
00476    public:
00481       CommandOptionAllOf()
00482       {}
00483       
00485       virtual ~CommandOptionAllOf() {}
00486 
00487       virtual std::string checkArguments();
00488 
00490       virtual unsigned long getCount() const;
00491 
00492    private:
00493          // hide this as it doesn't make sense for this class
00494       CommandOption* whichOne() const;
00495    };
00496 
00506    class CommandOptionMutex : public CommandOptionOneOf
00507    {
00508    public:
00518       CommandOptionMutex(const bool required = false)
00519             : doOneOfChecking(required)
00520       {}
00521       
00523       virtual ~CommandOptionMutex() {}
00524 
00525       virtual std::string checkArguments();
00526 
00527    protected:
00528       bool doOneOfChecking;
00529    };
00530 
00540    class CommandOptionDependent : public CommandOption
00541    {
00542    public:
00552       CommandOptionDependent(const CommandOption* parent,
00553                              const CommandOption* child)
00554             : CommandOption(noArgument, metaType, 0, "", ""),
00555               requiree(parent), requirer(child)
00556       {}
00557 
00559       virtual ~CommandOptionDependent() {}
00560 
00561       virtual std::string checkArguments();
00562 
00563    protected:
00565       CommandOptionDependent() {}
00566 
00567       const CommandOption *requiree, *requirer;
00568    };
00569 
00580    class CommandOptionGroupOr : public CommandOptionOneOf
00581    {
00582    public:
00586       CommandOptionGroupOr()
00587       {}
00588       
00590       virtual ~CommandOptionGroupOr() {}
00591 
00593       virtual std::string checkArguments() { return std::string(); }
00594 
00596       virtual unsigned long getCount() const;
00597 
00599       virtual std::string getOptionString() const;
00600    };
00601 
00612    class CommandOptionGroupAnd : public CommandOptionGroupOr
00613    {
00614    public:
00618       CommandOptionGroupAnd()
00619       {}
00620       
00622       virtual ~CommandOptionGroupAnd() {}
00623 
00625       virtual unsigned long getCount() const;
00626    };
00627 
00629    
00630 } // namespace gpstk
00631 #endif

Generated on Wed Feb 8 03:30:58 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1