| 1 | #ifndef ARGUMENT_READER_H |
| 2 | #define ARGUMENT_READER_H |
| 3 | |
| 4 | #include <vector> |
| 5 | #include <string> |
| 6 | |
| 7 | using namespace std; |
| 8 | |
| 9 | /*! |
| 10 | *\brief Hachti's even more wonderful configuration manager. |
| 11 | * |
| 12 | * This class is designed to do all the work with the parameters passed to |
| 13 | * the main() routine of a program.\n |
| 14 | * Additionally, it is able to interpret configuration files. |
| 15 | * Usage:\n |
| 16 | * - Create an instance. |
| 17 | * - Add Parameters/switches with add_option_*(). |
| 18 | * - Add Arguments with add_argument(). |
| 19 | * - Call read_args() with your main routine's arguments. |
| 20 | * The vector returned by read_args() contains all error |
| 21 | * messages. When it's empty, everything is fine! |
| 22 | * - Call get_help() if you want nice formatted help output. |
| 23 | * (You want!!!) |
| 24 | * Sould be easy to use.... |
| 25 | */ |
| 26 | class configuration_manager{ |
| 27 | |
| 28 | protected: // Types |
| 29 | /*! |
| 30 | *\brief Container for an option switch |
| 31 | */ |
| 32 | class opt_switch_t{ |
| 33 | public: |
| 34 | opt_switch_t (const string & shortname, |
| 35 | const string & longname, |
| 36 | const string & description, |
| 37 | int * status, |
| 38 | const bool & allow_commandline=true, |
| 39 | const bool & allow_config_file=true); |
| 40 | string shortname; |
| 41 | string longname; |
| 42 | int * status; |
| 43 | string * target; |
| 44 | string description; |
| 45 | string placeholder; |
| 46 | bool allow_conffile; |
| 47 | bool allow_cmdline; |
| 48 | }; |
| 49 | |
| 50 | /*! |
| 51 | *\brief Container for an option value |
| 52 | */ |
| 53 | class opt_value_t{ |
| 54 | public: |
| 55 | opt_value_t (const string & shortname, |
| 56 | const string & longname, |
| 57 | const string & description, |
| 58 | int * status, |
| 59 | string * target, |
| 60 | const string & placeholder=string("<string>"), |
| 61 | const bool & allow_commandline=true, |
| 62 | const bool & allow_config_file=true); |
| 63 | string shortname; |
| 64 | string longname; |
| 65 | int * status; |
| 66 | string * target; |
| 67 | string description; |
| 68 | string placeholder; |
| 69 | bool allow_conffile; |
| 70 | bool allow_cmdline; |
| 71 | }; |
| 72 | |
| 73 | /*! |
| 74 | *\brief Container for a commandline argument |
| 75 | */ |
| 76 | class cmd_arg_t{ |
| 77 | public: |
| 78 | cmd_arg_t (const string & placeholder, |
| 79 | const string & description, |
| 80 | int * status, |
| 81 | string * target); |
| 82 | int * status; |
| 83 | string * target; |
| 84 | string description; |
| 85 | string placeholder; |
| 86 | }; |
| 87 | |
| 88 | public: // Methods |
| 89 | |
| 90 | configuration_manager(string name); |
| 91 | |
| 92 | void add_option_value (const string & shortname, |
| 93 | const string & longname, |
| 94 | const string & description, |
| 95 | int *status, |
| 96 | string *target=NULL, |
| 97 | const string & placeholder=string("<string>"), |
| 98 | const bool & allow_commandline=true, |
| 99 | const bool & allow_config_file=true |
| 100 | ); |
| 101 | |
| 102 | void add_option_switch (const string & shortname, |
| 103 | const string & longname, |
| 104 | const string & description, |
| 105 | int *status, |
| 106 | const bool & allow_commandline=true, |
| 107 | const bool & allow_config_file=true |
| 108 | ); |
| 109 | |
| 110 | void add_argument (const string & description, |
| 111 | int *status, |
| 112 | string *target=NULL, |
| 113 | const string & placeholder=string("<string>") |
| 114 | ); |
| 115 | |
| 116 | vector<string> read_args(int argc, char ** args); |
| 117 | vector<string> read_file(string filename); |
| 118 | |
| 119 | void get_help (vector<string> & target); |
| 120 | vector<string> get_help(); |
| 121 | |
| 122 | protected: // members |
| 123 | vector<opt_switch_t>option_switches; |
| 124 | vector<opt_value_t>option_values; |
| 125 | vector<cmd_arg_t>cmd_args; |
| 126 | string app_name; |
| 127 | |
| 128 | protected: // methods |
| 129 | string analyse_string(const string & line); |
| 130 | bool analyse_bool(const string & data); |
| 131 | bool analyse_bool_false(const string & data); |
| 132 | }; // class configuration_manager |
| 133 | |
| 134 | |
| 135 | #endif |
| 136 | |