Began removal of platform/. The Monitor:: namespace is completely converted.
[aesalon.git] / src / misc / ArgumentParser.cpp
blob468973ab182f2ea4bf7acc89ff252fc4a0c4c8d6
1 #include <cstring>
2 #include <iostream>
3 #include <queue>
5 #include "ArgumentParser.h"
7 namespace Aesalon {
8 namespace Misc {
10 template<>ArgumentParser *Singleton<ArgumentParser>::instance = 0;
12 void ArgumentParser::parse_argv(char *argv[]) {
13 std::size_t x = 0;
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] == '-') {
19 if(argv[x][2] == 0) {
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 ++) {
25 bool handled = false;
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;
31 break;
33 case Argument::STRING_ARGUMENT: {
34 StringArgument *sa = (*i).second.to<StringArgument>();
35 if(sa->get_long_form() == argv[x]) {
36 if(!argv[x+1]) {
37 throw NoArgumentToArgumentException(argv[x]);
39 sa->set_value(argv[++x]), handled = true;
41 break;
45 if(handled) break;
48 if(i == argument_map.end()) {
49 throw UnknownArgumentException(argv[x]);
52 else {
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()) {
60 bool handled = false;
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);
65 handled = true;
66 break;
68 else if(arguments.front() == (*i).second.to<BooleanArgument>()->get_disable_short_form()) {
69 (*i).second.to<BooleanArgument>()->set_status(false);
70 handled = true;
71 break;
74 else if((*i).second->get_type() == Argument::STRING_ARGUMENT) {
75 if(arguments.front() == (*i).second.to<StringArgument>()->get_short_form()) {
76 if(!argv[x+1]) {
77 throw NoArgumentToArgumentException(arguments.front());
79 (*i).second.to<StringArgument>()->set_value(argv[++x]);
80 handled = true;
84 if(!handled) {
85 throw UnknownArgumentException(arguments.front());
87 arguments.pop();
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 ++) {
99 delete (*ai).second;
101 file_argument_vector_t::iterator fi = file_argument_vector.begin();
102 for(; fi != file_argument_vector.end(); fi ++) {
103 delete (*fi);
107 } // namespace Misc
108 } // namespace Aesalon