00001 #ifndef _theplu_yat_utility_commandline_
00002 #define _theplu_yat_utility_commandline_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00194 std::string description_;
00195 std::vector<std::string> free_arg_;
00196 size_t free_arg_max_;
00197 std::vector<Option*> options_;
00198 std::map<char, Option*> short_options_;
00199 std::map<std::string, Option*> long_options_;
00200 bool parsed_;
00201 std::string program_name_;
00202
00203
00205 struct OptionCompare
00206 {
00207 bool operator()(const Option*, const Option*) const;
00208 };
00210
00211 };
00212
00231 std::ostream& operator<<(std::ostream&, const CommandLine&);
00232
00233 template<class Compare>
00234 void CommandLine::sort(Compare compare)
00235 {
00236 std::sort(options_.begin(), options_.end(), compare);
00237 }
00238
00239 }}}
00240
00241 #endif