| 1 |
/* |
|---|
| 2 |
|
|---|
| 3 |
This file is from Nitrogen, an X11 background setter. |
|---|
| 4 |
Copyright (C) 2006 Dave Foster & Javeed Shaikh |
|---|
| 5 |
|
|---|
| 6 |
This program is free software; you can redistribute it and/or |
|---|
| 7 |
modify it under the terms of the GNU General Public License |
|---|
| 8 |
as published by the Free Software Foundation; either version 2 |
|---|
| 9 |
of the License, or (at your option) any later version. |
|---|
| 10 |
|
|---|
| 11 |
This program is distributed in the hope that it will be useful, |
|---|
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 |
GNU General Public License for more details. |
|---|
| 15 |
|
|---|
| 16 |
You should have received a copy of the GNU General Public License |
|---|
| 17 |
along with this program; if not, write to the Free Software |
|---|
| 18 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 19 |
|
|---|
| 20 |
*/ |
|---|
| 21 |
|
|---|
| 22 |
#ifndef _ARGPARSER_H_ |
|---|
| 23 |
#define _ARGPARSER_H_ |
|---|
| 24 |
|
|---|
| 25 |
#include <map> |
|---|
| 26 |
#include <string> |
|---|
| 27 |
#include <vector> |
|---|
| 28 |
#include <iostream> |
|---|
| 29 |
|
|---|
| 30 |
class Arg { |
|---|
| 31 |
public: |
|---|
| 32 |
Arg ( std::string desc = "", |
|---|
| 33 |
bool has_arg = false) { |
|---|
| 34 |
set_desc (desc); |
|---|
| 35 |
set_has_arg (has_arg); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
std::string get_desc (void) const { |
|---|
| 39 |
return desc; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
bool get_has_arg (void) const { |
|---|
| 43 |
return has_arg; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
void set_desc (std::string desc) { |
|---|
| 47 |
this->desc = desc; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
void set_has_arg (bool has_arg) { |
|---|
| 51 |
this->has_arg = has_arg; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
~Arg () {}; |
|---|
| 55 |
|
|---|
| 56 |
protected: |
|---|
| 57 |
std::string desc; |
|---|
| 58 |
bool has_arg; |
|---|
| 59 |
}; |
|---|
| 60 |
|
|---|
| 61 |
class ArgParser { |
|---|
| 62 |
public: |
|---|
| 63 |
ArgParser (void) { |
|---|
| 64 |
register_option ("help, -h", "Prints this help text."); |
|---|
| 65 |
}; |
|---|
| 66 |
~ArgParser () {}; |
|---|
| 67 |
|
|---|
| 68 |
// adds a valid option. |
|---|
| 69 |
inline void register_option (std::string name, std::string desc = "", bool has_arg = false) { |
|---|
| 70 |
expected_args[name] = Arg(desc, has_arg); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
// makes two or more options mutually exclusive (only one or the other.) |
|---|
| 74 |
inline void make_exclusive (std::vector<std::string> exclusive_opts) { |
|---|
| 75 |
exclusive.push_back (exclusive_opts); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
// returns the parsed map. |
|---|
| 79 |
inline std::map<std::string, std::string> get_map (void) const { |
|---|
| 80 |
return received_args; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
// returns all errors. |
|---|
| 84 |
inline std::string get_error (void) const { |
|---|
| 85 |
return error_str; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
inline std::string get_extra_args (void) const { |
|---|
| 89 |
return extra_arg_str; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
inline bool has_argument (std::string option) { |
|---|
| 93 |
if (received_args.find (option) != received_args.end ()) { |
|---|
| 94 |
return true; |
|---|
| 95 |
} |
|---|
| 96 |
return false; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
inline std::string get_value (std::string key) { |
|---|
| 100 |
std::map<std::string, std::string>::const_iterator iter = received_args.find (key); |
|---|
| 101 |
if (iter != received_args.end ()) { |
|---|
| 102 |
// key exists. |
|---|
| 103 |
return iter->second; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
// key doesn't exist. |
|---|
| 107 |
return std::string (); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
// parses away. |
|---|
| 112 |
bool parse (int argc, char ** argv); |
|---|
| 113 |
|
|---|
| 114 |
// returns the help text (--help) |
|---|
| 115 |
std::string help_text (void) const; |
|---|
| 116 |
|
|---|
| 117 |
protected: |
|---|
| 118 |
// checks if the supplied key conflicts with an existing key. |
|---|
| 119 |
bool conflicts (std::string key); |
|---|
| 120 |
|
|---|
| 121 |
std::string error_str, extra_arg_str; |
|---|
| 122 |
std::map<std::string, std::string> received_args; |
|---|
| 123 |
std::map<std::string, Arg> expected_args; |
|---|
| 124 |
std::vector< std::vector<std::string> > exclusive; |
|---|
| 125 |
}; |
|---|
| 126 |
|
|---|
| 127 |
#endif |
|---|