Pretty much converted the ASM namespace to use the StorageManager system.
[aesalon.git] / monitor / src / misc / ArgumentParser.h
blob9f37ff83bbae4ab1234f0fd8aa3392cc61a9a7d6
1 #ifndef AESALON_ARGUMENT_PARSER_H
2 #define AESALON_ARGUMENT_PARSER_H
4 #include <string>
5 #include <vector>
6 #include <map>
8 #include "exception/ArgumentException.h"
10 namespace Misc {
12 class Argument {
13 public:
14 enum argument_type_e {
15 NO_ARGUMENT = 0,
16 REQUIRED_ARGUMENT,
17 OPTIONAL_ARGUMENT
19 private:
20 std::string long_form;
21 char short_form;
22 argument_type_e argument_type;
23 std::string data;
24 bool found;
25 public:
26 Argument(std::string long_form, char short_form,
27 argument_type_e argument_type, std::string default_data) : long_form(long_form),
28 short_form(short_form), argument_type(argument_type), data(default_data), found(false) {}
29 ~Argument() {}
31 std::string get_long_form() const { return long_form; }
32 char get_short_form() const { return short_form; }
33 std::string get_data() const { return data; }
34 void set_data(std::string new_data) { data = new_data; }
35 argument_type_e get_argument_type() const { return argument_type; }
36 bool is_found() const { return found; }
37 void set_found(bool new_found) { found = new_found; }
40 class ArgumentParser {
41 protected:
42 typedef std::map<std::string, Argument *> argument_map_t;
43 typedef std::vector<std::string> postargs_t;
44 private:
45 int argc;
46 char **argv;
47 argument_map_t argument_map;
48 postargs_t postargs;
49 public:
50 ArgumentParser(char **argv);
51 ~ArgumentParser();
53 /** Adds an argument to the argument list.
54 @param argument The argument to add. It is not the responsibility of the caller to free this.
56 void add_argument(Argument *argument) {
57 argument_map[argument->get_long_form()] = argument;
60 Argument *get_argument(std::string long_form) {
61 Argument *arg = argument_map[long_form];
62 if(arg) return arg;
63 throw Exception::ArgumentException(Misc::StreamAsString() << "Attempt to retreieve non-existent argument \"" << long_form << "\"");
66 std::size_t get_postargs() const { return postargs.size(); }
67 std::string get_postarg(std::size_t which) { return postargs[which]; }
69 void parse();
72 } // namespace Misc
74 #endif