yat  0.14.5pre
CommandLine.h
1 #ifndef _theplu_yat_utility_commandline_
2 #define _theplu_yat_utility_commandline_
3 
4 //$Id: CommandLine.h 3188 2014-03-25 10:24:29Z peter $
5 
6 /*
7  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010, 2011, 2012, 2013, 2014 Peter Johansson
9 
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11 
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16 
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include <algorithm>
27 #include <cctype>
28 #include <map>
29 #include <iosfwd>
30 #include <sstream>
31 #include <stdexcept>
32 #include <string>
33 #include <typeinfo>
34 #include <utility>
35 #include <vector>
36 
37 namespace theplu {
38 namespace yat {
39 namespace utility {
40 
41  class Option;
42 
99  {
100  public:
106  CommandLine(std::string str="Available options are:");
107 
111  virtual ~CommandLine(void);
112 
116  void add(Option&);
117 
124  void allow_free_args(size_t n);
125 
131  const std::vector<std::string>& free_args(void) const;
132 
143  void parse(int argc, char* argv[]);
144 
152  bool parsed(void) const;
153 
158  std::string program_name(void) const;
159 
169  void sort(void);
170 
180  template<class Compare>
181  void sort(Compare compare);
182 
187  std::string try_help(void) const;
188 
189  private:
190  friend std::ostream& operator<<(std::ostream& os, const CommandLine& cl);
191  bool is_long_option(std::string str) const;
192  bool is_short_option(std::string str) const;
193  void parse(std::vector<std::string>::iterator& first,
194  std::vector<std::string>::iterator& last);
195  void parse_long(std::vector<std::string>::iterator& first,
196  std::vector<std::string>::iterator& last);
197  void parse_short(std::vector<std::string>::iterator& first,
198  std::vector<std::string>::iterator& last);
199  void parse_free_arg(std::vector<std::string>::iterator& first,
200  std::vector<std::string>::iterator& last);
201  std::string description_;
202  std::vector<std::string> free_arg_;
203  size_t free_arg_max_;
204  std::vector<Option*> options_;
205  std::map<char, Option*> short_options_;
206  std::map<std::string, Option*> long_options_;
207  bool parsed_;
208  std::string program_name_;
209 
210  // use cond to make doxygen ignore this privat class
212  struct OptionCompare
213  {
214  bool operator()(const Option*, const Option*) const;
215  };
217 
218  };
219 
238  std::ostream& operator<<(std::ostream&, const CommandLine&);
239 
240  template<class Compare>
241  void CommandLine::sort(Compare compare)
242  {
243  std::sort(options_.begin(), options_.end(), compare);
244  }
245 
246 }}} // end of namespace utility, yat, and theplu
247 
248 #endif
std::string try_help(void) const
void allow_free_args(size_t n)
Allow at most n free arguments.
const std::vector< std::string > & free_args(void) const
Arguments not associated with an Option.
void add(Option &)
Function to add an option.
CommandLine(std::string str="Available options are:")
default constructor
bool parsed(void) const
has the commandline been parsed already
void parse(int argc, char *argv[])
parse the commandline
Class for parsing the command line.
Definition: CommandLine.h:98
void sort(void)
Sort Options how they will appear in (help) output.
std::string program_name(void) const
virtual ~CommandLine(void)
Destructor.
Container of variables for an option.
Definition: Option.h:37

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5