src/CLIParser.h
branchv_0
changeset 2 f031a4dc7c52
parent 0 16c7fa9b7c49
child 5 d70ea23682aa
equal deleted inserted replaced
1:3876a9c56a66 2:f031a4dc7c52
    35 	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
    35 	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& 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 	/**
       
    41 	 * TODO: use a common method
       
    42 	 */
       
    43 	bool parseBoolean(const relpipe::writer::string_t& value) {
       
    44 		if (value == L"true") return true;
       
    45 		else if (value == L"false") return false;
       
    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);
       
    47 	}
       
    48 
    40 public:
    49 public:
    41 
    50 
    42 	static const relpipe::writer::string_t OPTION_RELATION;
    51 	static const relpipe::writer::string_t OPTION_RELATION;
       
    52 	static const relpipe::writer::string_t OPTION_ENABLE_SECTIONS;
       
    53 	static const relpipe::writer::string_t OPTION_ENABLE_SUBKEYS;
       
    54 	static const relpipe::writer::string_t OPTION_ENABLE_COMMENTS;
       
    55 	static const relpipe::writer::string_t OPTION_ENABLE_LINE_NUMBERS;
       
    56 	static const relpipe::writer::string_t OPTION_ENABLE_EVENT_NUMBERS;
    43 
    57 
    44 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
    58 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
    45 		Configuration c;
    59 		Configuration c;
    46 
    60 
    47 		for (int i = 0; i < arguments.size();) {
    61 		for (int i = 0; i < arguments.size();) {
    48 			relpipe::writer::string_t option = readNext(arguments, i);
    62 			relpipe::writer::string_t option = readNext(arguments, i);
    49 
    63 
    50 			if (option == OPTION_RELATION) {
    64 			if (option == OPTION_RELATION) c.relation = readNext(arguments, i);
    51 				c.relation = readNext(arguments, i);
    65 			else if (option == OPTION_ENABLE_SECTIONS) c.enableSections = parseBoolean(readNext(arguments, i));
    52 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    66 			else if (option == OPTION_ENABLE_SUBKEYS) c.enableSubkeys = parseBoolean(readNext(arguments, i));
       
    67 			else if (option == OPTION_ENABLE_COMMENTS) c.enableComments = parseBoolean(readNext(arguments, i));
       
    68 			else if (option == OPTION_ENABLE_LINE_NUMBERS) c.enableLineNumbers = parseBoolean(readNext(arguments, i));
       
    69 			else if (option == OPTION_ENABLE_EVENT_NUMBERS) c.enableEventNumbers = parseBoolean(readNext(arguments, i));
       
    70 			else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    53 		}
    71 		}
    54 
    72 
    55 		return c;
    73 		return c;
    56 	}
    74 	}
    57 
    75 
    58 	virtual ~CLIParser() {
    76 	virtual ~CLIParser() {
    59 	}
    77 	}
    60 };
    78 };
    61 
    79 
    62 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
    80 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
       
    81 const relpipe::writer::string_t CLIParser::OPTION_ENABLE_SECTIONS = L"--enable-sections";
       
    82 const relpipe::writer::string_t CLIParser::OPTION_ENABLE_SUBKEYS = L"--enable-subkeys";
       
    83 const relpipe::writer::string_t CLIParser::OPTION_ENABLE_COMMENTS = L"--enable-comments";
       
    84 const relpipe::writer::string_t CLIParser::OPTION_ENABLE_LINE_NUMBERS = L"--enable-line-numbers";
       
    85 const relpipe::writer::string_t CLIParser::OPTION_ENABLE_EVENT_NUMBERS = L"--enable-event-numbers";
    63 
    86 
    64 }
    87 }
    65 }
    88 }
    66 }
    89 }