2 #include <ail/arguments.hpp>
3 #include <ail/string.hpp>
4 #include <boost/foreach.hpp>
8 argument_variable::argument_variable(std::string
const & name
, bool & variable
):
10 type(argument_variable_type_bool
),
13 set_common_properties();
16 argument_variable::argument_variable(std::string
const & name
, std::string
& variable
):
18 type(argument_variable_type_string
),
21 set_common_properties();
24 void argument_variable::set_common_properties()
31 bool argument_variable::operator==(std::string
const & input
) const
36 argument_controller::argument_controller(argument_variable
& _variable
):
41 argument_controller
& argument_controller::required()
43 variable
.required
= true;
47 argument_controller
& argument_controller::default_flag(bool value
)
49 variable
.default_value
= new bool(value
);
53 argument_controller
& argument_controller::default_string(std::string
const & value
)
55 variable
.default_value
= new std::string(value
);
59 argument_controller
& argument_parser::flag(std::string
const & name
, bool & variable
)
61 argument_variable
new_variable(name
, variable
);
62 variables
.push_back(new_variable
);
63 return * new argument_controller(*(variables
.end() - 1));
66 argument_controller
& argument_parser::string(std::string
const & name
, std::string
& variable
)
68 argument_variable
new_variable(name
, variable
);
69 variables
.push_back(new_variable
);
70 return * new argument_controller(*(variables
.end() - 1));
73 void argument_parser::parse(std::string
const & input
)
75 string_vector tokens
= tokenise(input
, " ");
79 void argument_parser::parse(int argc
, char ** argv
)
82 for(int i
= 0; i
< argc
; i
++)
83 tokens
.push_back(argv
[i
]);
87 void argument_parser::parse(string_vector
& tokens
)
89 for(std::size_t i
= 0, end
= tokens
.size(); i
< end
; i
++)
91 std::string
const & token
= tokens
[i
];
97 std::string name
= token
.substr(1);
98 variable_vector::iterator iterator
= std::find(variables
.begin(), variables
.end(), name
);
99 if(iterator
== variables
.end())
101 argument_variable
& variable
= *iterator
;
104 throw exception("Variable \"" + name
+ "\" is lacking an argument");
105 std::string argument
= tokens
[i
];
106 switch(variable
.type
)
108 case argument_variable_type_bool
:
111 if(!string_to_bool(argument
, value
))
112 throw exception("Invalid boolean value specified for variable \"" + name
+ "\"");
113 *reinterpret_cast<bool *>(variable
.address
) = value
;
117 case argument_variable_type_string
:
118 *reinterpret_cast<std::string
*>(variable
.address
) = argument
;
122 throw exception("Unknown variable type encountered in argument parser");
125 variable
.handled
= true;
129 BOOST_FOREACH(argument_variable
& variable
, variables
)
131 if(!variable
.handled
)
133 if(variable
.required
)
134 throw exception("Required variable \"" + variable
.name
+ "\" has not been specified");
136 else if(variable
.default_value
)
138 switch(variable
.type
)
140 case argument_variable_type_bool
:
141 variable
.set_default_value
<bool>();
144 case argument_variable_type_string
:
145 variable
.set_default_value
<std::string
>();