1 #ifndef AESALON_ARGUMENT_PARSER_H
2 #define AESALON_ARGUMENT_PARSER_H
8 #include "exception/ArgumentException.h"
14 enum argument_type_e
{
20 std::string long_form
;
22 argument_type_e argument_type
;
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) {}
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
{
42 typedef std::map
<std::string
, Argument
*> argument_map_t
;
43 typedef std::vector
<std::string
> postargs_t
;
47 argument_map_t argument_map
;
50 ArgumentParser(char **argv
);
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
];
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
]; }