namespace command {
/**
* Class responsible for handling commandline arguments.
- * Arguments are required, non-named parameters of program.
- * Accepts
+ * Arguments are non-named parameters of program.
*
* Example:
* ./myprog ARGUMENT
template<typename ArgumentType>
class Argument : public Parameter, public Callable<ArgumentType> {
protected:
- /** Variable indicating if current Argument was already used or not */
- bool used = false;
-
ArgumentType value;
public:
typedef class Argument Type;
* against next value.
*/
virtual bool understand(const std::string & argv) {
- if (!used) {
+ if (!isUsed()) {
std::stringstream ss;
ss << argv;
ss >> value;
if (!ss.fail()) {
- used = true;
+ this->used = true;
return true;
}
}
#include <string>
#include <vector>
+#include <typeinfo>
#include "parameter.h"
releaseMemory();
throw;
}
+ catch(std::logic_error exception) {
+ releaseMemory();
+ throw;
+ }
}
/**
}
}
}
+ for(Parameter *param : parameters) {
+ if (param->isRequired() && !param->isUsed()) {
+ throw std::logic_error(param->describe() + " is required but it was not handled");
+ }
+ }
}
/**
namespace command {
/**
* Class responsible for handling commandline options.
- * Options are non-required, named parameters of program.
+ * Options are named parameters of program.
*
* Example:
* ./myprog OptionName=OptionValue
*/
OptionType value;
- /** Variable indicating if current Option was already used or not */
- bool used = false;
-
public:
/**
* Default constructor.
virtual bool understand(const std::string & argv)
throw(std::invalid_argument) {
- if ((!used) && (argv.find(name) == 0)) {
+ if ((!isUsed()) && (argv.find(name) == 0)) {
std::size_t pos = argv.find("=");
if (pos != name.size()) {
* Current Option name
*/
const OptionName name;
-
- /** Variable indicating if current Option was already used or not */
- bool used = false;
-
public:
/**
* Default constructor.
* used to check against next value.
*/
virtual bool understand(const std::string & argv) {
- if ((!used) &&
+ if ((!isUsed()) &&
(argv == name)) {
used = true;
return true;
* ./myprog ARGUMENT
*/
class Parameter : public Descriptive {
+ protected:
+ /** Variable indicating if current Parameter was already used or not */
+ bool used = false;
+
public:
typedef class Parameter Type;
/**
* parameter.
*/
virtual bool understand(const std::string & ) = 0;
+
+ /**
+ * Indicates if current Parameter is required
+ */
+ virtual bool isRequired() {
+ return false;
+ };
+
+ virtual bool isUsed() {
+ return used;
+ }
};
}
--- /dev/null
+#ifndef __COMMAND_REQUIRED_H
+#define __COMMAND_REQUIRED_H
+
+#include "parameter.h"
+
+namespace command {
+ /**
+ * Required Parameter decorator. Makes passed Parameters treated as required
+ */
+ class Required : public Parameter {
+ protected:
+ /**
+ * Parameter which will be treated as required
+ */
+ Parameter * parameter;
+
+ public:
+ /**
+ * Default constructor.
+ *
+ * @param parameter Parameter which will be treated as required
+ */
+ Required(Parameter * parameter)
+ : Parameter(parameter->describe()), parameter(parameter) {
+ }
+
+ /**
+ * Default destructor. Releases allocated memory
+ */
+ virtual ~Required() {
+ delete parameter;
+ }
+
+ /**
+ * Method used for handling method calls to linked Parameter
+ */
+ virtual void handle() {
+ parameter->handle();
+ }
+
+ /**
+ * Method used for checking if the given user value is understandable by
+ * parameter.
+ *
+ * @param value value from argv to check against
+ */
+ virtual bool understand(const std::string & value) {
+ return parameter->understand(value);
+ }
+
+ /**
+ * Indicates if current Parameter is required
+ */
+ virtual bool isRequired() {
+ return true;
+ };
+
+ /**
+ * Indicates if current Parameter is already used
+ */
+ virtual bool isUsed() {
+ return parameter->isUsed();
+ };
+ };
+}
+
+#endif /* __COMMAND_PARAMETER_H */
#include "option.h"
#include "argument.h"
+#include "required.h"
#include "command.h"
+using namespace command;
+
void argument_function(bool a) {
std::cout << "Argument: " << a << std::endl;
}
}
int main(int argc, char *argv[]) {
- command::Command command(argc, argv, {
-// new command::Argument<std::string>("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }),
- new command::Argument<bool>("File path", argument_function),
- new command::Option<std::string>("f", "Optional file", option_function),
- new command::Option<void>("h", "Help", void_function)
+ Command command(argc, argv, {
+// new Argument<std::string>("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }),
+ new Required(new Argument<bool>("File path", argument_function)),
+ new Option<std::string>("f", "Optional file", option_function),
+ new Option<void>("h", "Help", void_function)
});
return 0;