test/run_filter: add command line parser

This commit is contained in:
Max Kellermann 2024-10-30 22:13:49 +01:00
parent d7b4634e5e
commit 2ef36af68d
2 changed files with 53 additions and 14 deletions

View File

@ -524,6 +524,7 @@ executable(
include_directories: inc,
dependencies: [
filter_glue_dep,
cmdline_dep,
],
)

View File

@ -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 <cassert>
#include <memory>
@ -26,6 +29,51 @@
#include <stdlib.h>
#include <stdio.h>
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] <IN");
c.config_path = args[0];
c.filter_name = args[1];
if (args.size() > 2)
c.audio_format = ParseAudioFormat(args[2], false);
return c;
}
static std::unique_ptr<PreparedFilter>
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] <IN\n");
return EXIT_FAILURE;
}
const FromNarrowPath config_path = argv[1];
AudioFormat audio_format(44100, SampleFormat::S16, 2);
const auto c = ParseCommandLine(argc, argv);
SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);
/* read configuration file (mpd.conf) */
const auto config = AutoLoadConfigFile(config_path);
/* parse the audio format */
if (argc > 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 */