src/CLIParser.h
branchv_0
changeset 1 3bbf848b3565
parent 0 3493d6e7016e
child 6 1b17b8cdbfc3
equal deleted inserted replaced
0:3493d6e7016e 1:3bbf848b3565
    35 	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
    35 	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
    36 		if (i < arguments.size()) return arguments[i++];
    36 		if (i < arguments.size()) return arguments[i++];
    37 		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 		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);
    38 	}
    38 	}
    39 
    39 
    40 	void readNextRecord(const std::vector<relpipe::common::type::StringX>& arguments, int& i, RelationConfiguration& currentRelation) {
       
    41 		for (const auto& a : currentRelation.attributes) currentRelation.values.emplace_back(readNext(arguments, i));
       
    42 	}
       
    43 
       
    44 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
       
    45 		if (currentRelation.relation.size()) {
       
    46 			c.relationConfigurations.push_back(currentRelation);
       
    47 			currentRelation = RelationConfiguration();
       
    48 		}
       
    49 	}
       
    50 
       
    51 	/**
    40 	/**
    52 	 * TODO: use a common method
    41 	 * TODO: use a common method
    53 	 */
    42 	 */
    54 	bool parseBoolean(const relpipe::common::type::StringX& value) {
    43 	bool parseBoolean(const relpipe::common::type::StringX& value) {
    55 		if (value == L"true") return true;
    44 		if (value == L"true") return true;
    56 		else if (value == L"false") return false;
    45 		else if (value == L"false") return false;
    57 		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 		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);
    58 	}
    47 	}
    59 
    48 
    60 	/**
       
    61 	 * TODO: use a common method
       
    62 	 */
       
    63 	relpipe::writer::TypeId parseTypeId(const relpipe::common::type::StringX& value) {
       
    64 		using t = relpipe::writer::TypeId;
       
    65 		if (value == L"string") return t::STRING;
       
    66 		else if (value == L"integer") return t::INTEGER;
       
    67 		else if (value == L"boolean") return t::BOOLEAN;
       
    68 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    69 	}
       
    70 
       
    71 public:
    49 public:
    72 
    50 
    73 	static const relpipe::common::type::StringX OPTION_RELATION;
    51 	static const relpipe::common::type::StringX OPTION_LIST_INPUT_DEVICES;
    74 	static const relpipe::common::type::StringX OPTION_WRITE_HEADER;
    52 	static const relpipe::common::type::StringX OPTION_LIST_INPUT_EVENTS;
    75 	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
       
    76 	static const relpipe::common::type::StringX OPTION_RECORD;
       
    77 	static const relpipe::common::type::StringX OPTION_RECORDS;
       
    78 	static const relpipe::common::type::StringX OPTION_RECORDS_STDIN;
       
    79 
    53 
    80 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    54 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    81 		Configuration c;
    55 		Configuration c;
    82 		RelationConfiguration currentRelation;
       
    83 
    56 
    84 		for (int i = 0; i < arguments.size();) {
    57 		for (int i = 0; i < arguments.size();) {
    85 			relpipe::common::type::StringX option = readNext(arguments, i);
    58 			relpipe::common::type::StringX option = readNext(arguments, i);
    86 
    59 
    87 			if (option == OPTION_RELATION) {
    60 			if (option == OPTION_LIST_INPUT_DEVICES) {
    88 				addRelation(c, currentRelation); // previous relation
    61 				c.listInputDevices = parseBoolean(readNext(arguments, i));
    89 				currentRelation.relation = readNext(arguments, i);
    62 			} else if (option == OPTION_LIST_INPUT_EVENTS) {
    90 			} else if (option == OPTION_WRITE_HEADER) {
    63 				c.listInputEvents = parseBoolean(readNext(arguments, i));
    91 				currentRelation.writeHeader = parseBoolean(readNext(arguments, i));
       
    92 			} else if (option == OPTION_ATTRIBUTE) {
       
    93 				AttributeRecipe attribute;
       
    94 				attribute.name = readNext(arguments, i);
       
    95 				attribute.type = parseTypeId(readNext(arguments, i));
       
    96 				currentRelation.attributes.push_back(attribute);
       
    97 			} else if (option == OPTION_RECORD) {
       
    98 				readNextRecord(arguments, i, currentRelation);
       
    99 			} else if (option == OPTION_RECORDS) {
       
   100 				while (i < arguments.size()) readNextRecord(arguments, i, currentRelation);
       
   101 			} else if (option == OPTION_RECORDS_STDIN) {
       
   102 				if (parseBoolean(readNext(arguments, i))) {
       
   103 					for (RelationConfiguration r : c.relationConfigurations) if (r.valueStream) throw relpipe::cli::RelpipeCLIException(L"Only one relation can read data from STDIN.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
   104 					currentRelation.valueStream = &std::cin;
       
   105 				}
       
   106 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    64 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
   107 		}
    65 		}
   108 		addRelation(c, currentRelation); // last relation
       
   109 
       
   110 		if (c.relationConfigurations.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one relation. Use the " + OPTION_RELATION + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
   111 		for (auto& rc : c.relationConfigurations) if (rc.attributes.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one attribute. Use the " + OPTION_ATTRIBUTE + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
   112 
    66 
   113 		return c;
    67 		return c;
   114 	}
    68 	}
   115 
    69 
   116 	virtual ~CLIParser() {
    70 	virtual ~CLIParser() {
   117 	}
    71 	}
   118 };
    72 };
   119 
    73 
   120 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
    74 const relpipe::common::type::StringX CLIParser::OPTION_LIST_INPUT_DEVICES = L"--list-input-devices";
   121 const relpipe::common::type::StringX CLIParser::OPTION_WRITE_HEADER = L"--write-header";
    75 const relpipe::common::type::StringX CLIParser::OPTION_LIST_INPUT_EVENTS = L"--list-input-events";
   122 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
       
   123 const relpipe::common::type::StringX CLIParser::OPTION_RECORD = L"--record";
       
   124 const relpipe::common::type::StringX CLIParser::OPTION_RECORDS = L"--records";
       
   125 const relpipe::common::type::StringX CLIParser::OPTION_RECORDS_STDIN = L"--records-on-stdin";
       
   126 
    76 
   127 }
    77 }
   128 }
    78 }
   129 }
    79 }