main.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 The Effcee Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <iostream>
  15. #include <sstream>
  16. #include "effcee/effcee.h"
  17. // Checks standard input against the list of checks provided as command line
  18. // arguments.
  19. //
  20. // Example:
  21. // cat <<EOF >sample_data.txt
  22. // Bees
  23. // Make
  24. // Delicious Honey
  25. // EOF
  26. // effcee-example <sample_data.txt "CHECK: Bees" "CHECK-NOT:Sting" "CHECK: Honey"
  27. int main(int argc, char* argv[]) {
  28. // Read the command arguments as a list of check rules.
  29. std::ostringstream checks_stream;
  30. for (int i = 1; i < argc; ++i) {
  31. checks_stream << argv[i] << "\n";
  32. }
  33. // Read stdin as the input to match.
  34. std::stringstream input_stream;
  35. std::cin >> input_stream.rdbuf();
  36. // Attempt to match. The input and checks arguments can be provided as
  37. // std::string or pointer to char.
  38. auto result = effcee::Match(input_stream.str(), checks_stream.str(),
  39. effcee::Options().SetChecksName("checks"));
  40. // Successful match result converts to true.
  41. if (result) {
  42. std::cout << "The input matched your check list!" << std::endl;
  43. } else {
  44. // Otherwise, you can get a status code and a detailed message.
  45. switch (result.status()) {
  46. case effcee::Result::Status::NoRules:
  47. std::cout << "error: Expected check rules as command line arguments\n";
  48. break;
  49. case effcee::Result::Status::Fail:
  50. std::cout << "The input failed to match your check rules:\n";
  51. break;
  52. default:
  53. break;
  54. }
  55. std::cout << result.message() << std::endl;
  56. return 1;
  57. }
  58. return 0;
  59. }