5 #include "ArgumentParser.h"
10 template<>ArgumentParser
*Singleton
<ArgumentParser
>::instance
= 0;
12 void ArgumentParser::parse_argv(char *argv
[]) {
14 bool end_found
= false;
15 while(argv
[x
] && argv
[++x
]) {
16 /* Is it an argument? */
17 if(argv
[x
][0] == '-' && !end_found
) {
18 if(argv
[x
][1] == '-') {
20 end_found
= true; continue;
22 /* Try all the long-forms . . . */
23 argument_map_t::iterator i
= argument_map
.begin();
24 for(; i
!= argument_map
.end(); i
++) {
26 switch((*i
).second
->get_type()) {
27 case Argument::BOOLEAN_ARGUMENT
: {
28 BooleanArgument
*ba
= (*i
).second
.to
<BooleanArgument
>();
29 if(ba
->get_enable_long_form() == argv
[x
]) ba
->set_status(true), handled
= true;
30 else if(ba
->get_disable_long_form() == argv
[x
]) ba
->set_status(false), handled
= true;
33 case Argument::STRING_ARGUMENT
: {
34 StringArgument
*sa
= (*i
).second
.to
<StringArgument
>();
35 if(sa
->get_long_form() == argv
[x
]) {
37 throw NoArgumentToArgumentException(argv
[x
]);
39 sa
->set_value(argv
[++x
]), handled
= true;
48 if(i
== argument_map
.end()) {
49 throw UnknownArgumentException(argv
[x
]);
53 std::queue
<char> arguments
;
54 std::string argvstr
= argv
[x
];
55 std::string::iterator si
= argvstr
.begin();
56 for(si
++; si
!= argvstr
.end(); si
++) arguments
.push(*si
);
57 /* It's a bunch of short-form arguments, then. */
58 argument_map_t::iterator i
= argument_map
.begin();
59 while(!arguments
.empty()) {
61 for(; i
!= argument_map
.end(); i
++) {
62 if((*i
).second
->get_type() == Argument::BOOLEAN_ARGUMENT
) {
63 if(arguments
.front() == (*i
).second
.to
<BooleanArgument
>()->get_enable_short_form()) {
64 (*i
).second
.to
<BooleanArgument
>()->set_status(true);
68 else if(arguments
.front() == (*i
).second
.to
<BooleanArgument
>()->get_disable_short_form()) {
69 (*i
).second
.to
<BooleanArgument
>()->set_status(false);
74 else if((*i
).second
->get_type() == Argument::STRING_ARGUMENT
) {
75 if(arguments
.front() == (*i
).second
.to
<StringArgument
>()->get_short_form()) {
77 throw NoArgumentToArgumentException(arguments
.front());
79 (*i
).second
.to
<StringArgument
>()->set_value(argv
[++x
]);
85 throw UnknownArgumentException(arguments
.front());
91 /* Nope, it's a filename . . . */
92 else add_file(new FileArgument(argv
[x
]));
96 ArgumentParser::~ArgumentParser() {
97 argument_map_t::iterator ai
= argument_map
.begin();
98 for(; ai
!= argument_map
.end(); ai
++) {
101 file_argument_vector_t::iterator fi
= file_argument_vector
.begin();
102 for(; fi
!= file_argument_vector
.end(); fi
++) {
108 } // namespace Aesalon