From 2ef36af68d52aa7c98ab72d1e2b05e5cb43da219 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Oct 2024 22:13:49 +0100 Subject: [PATCH] test/run_filter: add command line parser --- test/meson.build | 1 + test/run_filter.cxx | 66 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/test/meson.build b/test/meson.build index bacd44633..d8e1688eb 100644 --- a/test/meson.build +++ b/test/meson.build @@ -524,6 +524,7 @@ executable( include_directories: inc, dependencies: [ filter_glue_dep, + cmdline_dep, ], ) diff --git a/test/run_filter.cxx b/test/run_filter.cxx index 7b58aa3ba..7d11f4569 100644 --- a/test/run_filter.cxx +++ b/test/run_filter.cxx @@ -3,6 +3,8 @@ #include "ConfigGlue.hxx" #include "ReadFrames.hxx" +#include "cmdline/OptionDef.hxx" +#include "cmdline/OptionParser.hxx" #include "lib/fmt/RuntimeError.hxx" #include "fs/Path.hxx" #include "fs/NarrowPath.hxx" @@ -17,6 +19,7 @@ #include "io/FileDescriptor.hxx" #include "util/StringBuffer.hxx" #include "util/PrintException.hxx" +#include "LogBackend.hxx" #include #include @@ -26,6 +29,51 @@ #include #include +struct CommandLine { + FromNarrowPath config_path; + + const char *filter_name = nullptr; + + AudioFormat audio_format{44100, SampleFormat::S16, 2}; + + bool verbose = false; +}; + +enum Option { + OPTION_VERBOSE, +}; + +static constexpr OptionDef option_defs[] = { + {"verbose", 'v', false, "Verbose logging"}, +}; + +static CommandLine +ParseCommandLine(int argc, char **argv) +{ + CommandLine c; + + OptionParser option_parser(option_defs, argc, argv); + while (auto o = option_parser.Next()) { + switch (Option(o.index)) { + case OPTION_VERBOSE: + c.verbose = true; + break; + } + } + + auto args = option_parser.GetRemaining(); + if (args.size() < 2 || args.size() > 3) + throw std::runtime_error("Usage: run_filter CONFIG NAME [FORMAT] 2) + c.audio_format = ParseAudioFormat(args[2], false); + + return c; +} + static std::unique_ptr LoadFilter(const ConfigData &config, const char *name) { @@ -40,24 +88,14 @@ LoadFilter(const ConfigData &config, const char *name) int main(int argc, char **argv) try { - if (argc < 3 || argc > 4) { - fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] 3) - audio_format = ParseAudioFormat(argv[3], false); + const auto config = AutoLoadConfigFile(c.config_path); + auto audio_format = c.audio_format; const size_t in_frame_size = audio_format.GetFrameSize(); /* initialize the filter */