• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/configuration/CommandLine.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _arguments_h
00018 #define _arguments_h
00019 
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 #include <spl/String.h>
00023 #include <spl/collection/Hashtable.h>
00024 #include <spl/Memory.h>
00025 #include <spl/RefCountPtr.h>
00026 #include <spl/collection/Vector.h>
00027 
00033 class CommandLine;
00034 typedef RefCountPtr<CommandLine> CommandLinePtr;
00035 
00043 class CommandLine : public IMemoryValidate
00044 {
00045 protected:
00046         Vector<String> m_switches;
00047         Hashtable<String, String> m_switchIdx;
00048         Vector<String> m_args;
00049 
00050         String ParseValue( const String& str, const bool unixStyle );
00051 
00052 public:
00053         CommandLine();
00054         CommandLine(const CommandLine& cl);
00055         CommandLine(const int argc, const char **argv);
00056         virtual ~CommandLine();
00057 
00058         CommandLine& operator =(const CommandLine& cl);
00059 
00060         bool Parse(const int argc, const char **argv);
00061         void Clear();
00062 
00063         inline bool HasSwitch(const String& argNoDashOrSlash) const { return m_switchIdx.ContainsKey(argNoDashOrSlash); }
00064         inline String& GetSwitch(const String argNoDashOrSlash) const { return m_switchIdx.GetRef(argNoDashOrSlash); }
00065         String& GetSwitch(const String& argNoDashOrSlash, const String& defValue);
00066         inline String& GetSwitch(const int idx) const { return m_switches.ElementAtRef(idx); }
00067         inline String& GetArg(int idx) const { ASSERT(idx < m_args.Count()); return m_args.ElementAtRef(idx); }
00068         inline int GetArgCount() const { return m_args.Count(); }
00069         inline int GetSwitchCount() const { return m_switchIdx.Count(); }
00070         void SetSwitch(const String& key, const String& value);
00071 
00072 #ifdef DEBUG
00073         void ValidateMem() const;
00074         void CheckMem() const;
00075 #endif
00076 };
00077 
00078 REGISTER_TYPEOF( 408, CommandLine );
00079 
00082 #endif