Shellp

shellcmdarg.h

00001 /*
00002     
00003   This file is a part of Shellp, a 'shell helper' library.
00004   
00005   Copyright (C) 2011-2012 Jori Liesenborgs
00006   
00007   Contact: jori.liesenborgs@gmail.com
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00022   USA
00023 
00024 */
00025 
00026 #ifndef SHELLP_SHELLCMDARG_H
00027 
00028 #define SHELLP_SHELLCMDARG_H
00029 
00030 #include "shellconfig.h"
00031 #include <errut/errorbase.h>
00032 #include <string>
00033 #include <vector>
00034 
00035 namespace shellp
00036 {
00037 
00038 class SHELLP_IMPORTEXPORT ShellCmdArg : public errut::ErrorBase
00039 {
00040 public:
00041         enum ArgType { String, Integer, Real, Bool, Choice };
00042 protected:
00043         ShellCmdArg(ArgType t, const std::string &name)                         { m_argType = t; m_argName = name; m_gotDefaultVal = false; m_expand = true; }
00044         ShellCmdArg(ArgType t, const std::string &name, 
00045                                const std::string &defaultVal)                   { m_argType = t; m_argName = name; m_gotDefaultVal = true; m_defaultValue = defaultVal; m_expand = true; }
00046 public:
00047         virtual ~ShellCmdArg()                                                  { }
00048 
00049         ArgType getArgType() const                                              { return m_argType; }
00050         std::string getName() const                                             { return m_argName; }
00051         std::string getDescription() const                                      { return m_description; }
00052         std::string getDefault() const                                          { return m_defaultValue; }
00053         bool hasDefault() const                                                 { return m_gotDefaultVal; }
00054 
00055         void setExpand(bool f)                                                  { m_expand = f; }
00056         bool shouldExpand() const                                               { return m_expand; }
00057 
00058         void setDescription(const std::string &d)                               { m_description = d; }
00059         void setDefault(const std::string &v)                                   { m_gotDefaultVal = true; m_defaultValue = v; }
00060         void clearDefault()                                                     { m_gotDefaultVal = false; }
00061 
00062         virtual bool parse(const std::string &arg) = 0;
00063         static bool parseArgumentList(std::vector<ShellCmdArg *> &args, 
00064                                       const std::vector<std::string> &argStrings,
00065                                       std::string &errStr);
00066 
00067         static std::string getArgumentNames(const std::vector<ShellCmdArg *> &args);
00068         static const char *getValidPreprocessorNameCharacters()                 { return m_pValidChars; }
00069 private:
00070         static bool expandArgument(const std::string &arg, std::string &expandedArg, std::string &errStr, int iteration);
00071 
00072         ArgType m_argType;
00073         std::string m_argName, m_defaultValue, m_description;
00074         bool m_gotDefaultVal, m_expand;
00075 
00076         static const char m_pValidChars[];
00077 };
00078 
00079 } // end namespace
00080 
00081 #endif // SHELLP_SHELLCMDARG_H
00082