From 817bfee273246f2a90c08e700e67a5f32711f979 Mon Sep 17 00:00:00 2001 From: Niklas Rosenstein Date: Wed, 18 Jan 2017 04:28:20 +0100 Subject: [PATCH] Report unrecognized arguments from BENCHMARK_MAIN() macro (#332) * BENCHMARK_MAIN() now reports unrecognised command-line flags (see google/benchmark#320) * add benchmark::ReportUnrecognizedArguments() Update BENCHMARK_MAIN() to use ReportUnrecognizedArguments() instead of having the reporting code directly in the macro. See issue google/benchmark#320 for reference * let's stick to american english -- fix type in ReportUnrecognizedArguments() * make ReportUnrecognizedArguments() print to stderr * make ReportUnrecognizedArguments() return true if any arguments have been reported (i.e. argc > 1) --- include/benchmark/benchmark_api.h | 5 +++++ src/benchmark.cc | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/include/benchmark/benchmark_api.h b/include/benchmark/benchmark_api.h index 28baa587..f953e597 100644 --- a/include/benchmark/benchmark_api.h +++ b/include/benchmark/benchmark_api.h @@ -168,6 +168,10 @@ class BenchmarkReporter; void Initialize(int* argc, char** argv); +// Report to stdout all arguments in 'argv' as unrecognized except the first. +// Returns true there is at least on unrecognized argument (i.e. 'argc' > 1). +bool ReportUnrecognizedArguments(int argc, char** argv); + // Generate a list of benchmarks matching the specified --benchmark_filter flag // and if --benchmark_list_tests is specified return after printing the name // of each matching benchmark. Otherwise run each matching benchmark and @@ -858,6 +862,7 @@ class Fixture : public internal::Benchmark { #define BENCHMARK_MAIN() \ int main(int argc, char** argv) { \ ::benchmark::Initialize(&argc, argv); \ + if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \ ::benchmark::RunSpecifiedBenchmarks(); \ } diff --git a/src/benchmark.cc b/src/benchmark.cc index 0a5baf86..d37dbd96 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -664,4 +664,11 @@ void Initialize(int* argc, char** argv) { internal::LogLevel() = FLAGS_v; } +bool ReportUnrecognizedArguments(int argc, char** argv) { + for (int i = 1; i < argc; ++i) { + fprintf(stderr, "%s: error: unrecognized command-line flag: %s\n", argv[0], argv[i]); + } + return argc > 1; +} + } // end namespace benchmark