yat  0.8.3pre
CommandLine.h
00001 #ifndef _theplu_yat_utility_commandline_
00002 #define _theplu_yat_utility_commandline_
00003 
00004 //$Id: CommandLine.h 2458 2011-04-03 15:14:46Z peter $
00005 
00006 /*
00007   Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
00008   Copyright (C) 2010, 2011 Peter Johansson
00009 
00010   This file is part of the yat library, http://dev.thep.lu.se/yat
00011 
00012   The yat library is free software; you can redistribute it and/or
00013   modify it under the terms of the GNU General Public License as
00014   published by the Free Software Foundation; either version 3 of the
00015   License, or (at your option) any later version.
00016 
00017   The yat library is distributed in the hope that it will be useful,
00018   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00020   General Public License for more details.
00021 
00022   You should have received a copy of the GNU General Public License
00023   along with yat. If not, see <http://www.gnu.org/licenses/>.
00024 */
00025 
00026 #include <algorithm>
00027 #include <cctype>
00028 #include <map>
00029 #include <iosfwd>
00030 #include <sstream>
00031 #include <stdexcept>
00032 #include <string>
00033 #include <typeinfo>
00034 #include <utility>
00035 #include <vector>
00036 
00037 namespace theplu {
00038 namespace yat {
00039 namespace utility {
00040 
00041   class Option;
00042 
00098   class CommandLine
00099   {
00100   public:
00106     CommandLine(std::string str="Available options are:");
00107 
00111     virtual ~CommandLine(void);
00112 
00116     void add(Option&);
00117 
00124     void allow_free_args(size_t n);
00125 
00131     const std::vector<std::string>& free_args(void) const;
00132 
00143     void parse(int argc, char* argv[]);
00144 
00152     bool parsed(void) const;
00153 
00158     std::string program_name(void) const;
00159 
00169     void sort(void);
00170 
00180     template<class Compare>
00181     void sort(Compare compare);
00182 
00187     std::string try_help(void) const;
00188 
00189   private:
00190     friend std::ostream& operator<<(std::ostream& os, const CommandLine& cl);
00191     bool is_long_option(std::string str) const;
00192     bool is_short_option(std::string str) const;
00193     void parse(std::vector<std::string>::iterator& first,
00194                std::vector<std::string>::iterator& last);
00195     void parse_long(std::vector<std::string>::iterator& first,
00196                     std::vector<std::string>::iterator& last);
00197     void parse_short(std::vector<std::string>::iterator& first,
00198                      std::vector<std::string>::iterator& last);
00199     void parse_free_arg(std::vector<std::string>::iterator& first,
00200                         std::vector<std::string>::iterator& last);
00201     std::string description_;
00202     std::vector<std::string> free_arg_;
00203     size_t free_arg_max_;
00204     std::vector<Option*> options_;
00205     std::map<char, Option*> short_options_;
00206     std::map<std::string, Option*> long_options_;
00207     bool parsed_;
00208     std::string program_name_;
00209 
00210     // use cond to make doxygen ignore this privat class
00212     struct OptionCompare
00213     {
00214       bool operator()(const Option*, const Option*) const;
00215     };
00217 
00218   };
00219 
00238   std::ostream& operator<<(std::ostream&, const CommandLine&);
00239 
00240   template<class Compare>
00241   void CommandLine::sort(Compare compare)
00242   {
00243     std::sort(options_.begin(), options_.end(), compare);
00244   }
00245 
00246 }}} // end of namespace utility, yat, and theplu
00247 
00248 #endif

Generated on Thu Dec 20 2012 03:12:58 for yat by  doxygen 1.8.0-20120409