src/CLIParser.h
branchv_0
changeset 2 4b05b16b97e6
parent 0 7b70918c30af
equal deleted inserted replaced
1:23c516259cc5 2:4b05b16b97e6
    34 	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
    34 	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
    35 		if (i < arguments.size()) return arguments[i++];
    35 		if (i < arguments.size()) return arguments[i++];
    36 		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    36 		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    37 	}
    37 	}
    38 
    38 
    39 	/**
       
    40 	 * TODO: use a common method
       
    41 	 */
       
    42 	bool parseBoolean(const relpipe::common::type::StringX& value) {
       
    43 		if (value == L"true") return true;
       
    44 		else if (value == L"false") return false;
       
    45 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    46 	}
       
    47 
       
    48 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
       
    49 		if (currentRelation.relation.size()) {
       
    50 			c.relationConfigurations.push_back(currentRelation);
       
    51 			currentRelation = RelationConfiguration();
       
    52 		}
       
    53 	}
       
    54 
       
    55 	/**
       
    56 	 * TODO: use a common method
       
    57 	 */
       
    58 	relpipe::writer::TypeId parseTypeId(const relpipe::common::type::StringX& value) {
       
    59 		using t = relpipe::writer::TypeId;
       
    60 		if (value == L"string") return t::STRING;
       
    61 		else if (value == L"integer") return t::INTEGER;
       
    62 		else if (value == L"boolean") return t::BOOLEAN;
       
    63 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    64 	}
       
    65 
       
    66 public:
    39 public:
    67 
    40 
    68 	static const relpipe::common::type::StringX OPTION_NAMESPACE;
    41 	static const relpipe::common::type::StringX OPTION_TCP_PORT;
    69 	static const relpipe::common::type::StringX OPTION_RELATION;
       
    70 	static const relpipe::common::type::StringX OPTION_WHERE;
       
    71 	static const relpipe::common::type::StringX OPTION_INPUT_ATTRIBUTES;
       
    72 	static const relpipe::common::type::StringX OPTION_OUTPUT_ATTRIBUTE;
       
    73 	static const relpipe::common::type::StringX OPTION_XML_ATTRIBUTE;
       
    74 
    42 
    75 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    43 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    76 		Configuration c;
    44 		Configuration c;
    77 		RelationConfiguration currentRelation;
       
    78 
    45 
    79 		for (int i = 0; i < arguments.size();) {
    46 		for (int i = 0; i < arguments.size();) {
    80 			relpipe::common::type::StringX option = readNext(arguments, i);
    47 			relpipe::common::type::StringX option = readNext(arguments, i);
    81 
    48 
    82 			if (option == OPTION_NAMESPACE) {
    49 			if (option == OPTION_TCP_PORT) {
    83 				c.namespaceMappings.push_back(readNext(arguments, i));
    50 				c.tcpPort = stoi(readNext(arguments, i));
    84 				c.namespaceMappings.push_back(readNext(arguments, i));
       
    85 			} else if (option == OPTION_RELATION) {
       
    86 				addRelation(c, currentRelation); // previous relation
       
    87 				currentRelation.relation = readNext(arguments, i);
       
    88 			} else if (option == OPTION_WHERE) {
       
    89 				currentRelation.where = readNext(arguments, i);
       
    90 			} else if (option == OPTION_XML_ATTRIBUTE) {
       
    91 				currentRelation.xmlAttributes.push_back(readNext(arguments, i));
       
    92 			} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
       
    93 				AttributeRecipe attribute;
       
    94 				attribute.name = readNext(arguments, i);
       
    95 				attribute.type = parseTypeId(readNext(arguments, i));
       
    96 				attribute.httpd = readNext(arguments, i);
       
    97 				currentRelation.outputAttributes.push_back(attribute);
       
    98 			} else if (option == OPTION_INPUT_ATTRIBUTES) {
       
    99 				relpipe::common::type::StringX policyName = readNext(arguments, i);
       
   100 				InputAttributePolicy policy;
       
   101 				if (policyName == L"append") policy = InputAttributePolicy::Append;
       
   102 				else if (policyName == L"prepend") policy = InputAttributePolicy::Prepend;
       
   103 				else if (policyName == L"auto") policy = InputAttributePolicy::Auto;
       
   104 				else throw relpipe::cli::RelpipeCLIException(L"Unsupported input attributes policy: " + policyName, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
   105 				currentRelation.inputAttributePolicy = policy;
       
   106 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    51 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
   107 		}
    52 		}
   108 		addRelation(c, currentRelation); // last relation
       
   109 
    53 
   110 		return c;
    54 		return c;
   111 	}
    55 	}
   112 
    56 
   113 	virtual ~CLIParser() {
    57 	virtual ~CLIParser() {
   114 	}
    58 	}
   115 };
    59 };
   116 
    60 
   117 const relpipe::common::type::StringX CLIParser::OPTION_NAMESPACE = L"--namespace";
    61 const relpipe::common::type::StringX CLIParser::OPTION_TCP_PORT = L"--tcp-port";
   118 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
       
   119 const relpipe::common::type::StringX CLIParser::OPTION_WHERE = L"--where";
       
   120 const relpipe::common::type::StringX CLIParser::OPTION_INPUT_ATTRIBUTES = L"--input-attributes";
       
   121 const relpipe::common::type::StringX CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
       
   122 const relpipe::common::type::StringX CLIParser::OPTION_XML_ATTRIBUTE = L"--xml-attribute";
       
   123 
    62 
   124 }
    63 }
   125 }
    64 }
   126 }
    65 }