From 7d5b8597565263dd30aaf1d38be1861af3f821a8 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 17 Jan 2018 12:04:01 +0100 Subject: [PATCH] test/run_input: add command-line option parser --- test/run_input.cxx | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/test/run_input.cxx b/test/run_input.cxx index 1c374d3d6..56f33535c 100644 --- a/test/run_input.cxx +++ b/test/run_input.cxx @@ -28,6 +28,8 @@ #include "Log.hxx" #include "fs/io/BufferedOutputStream.hxx" #include "fs/io/StdioOutputStream.hxx" +#include "util/ConstBuffer.hxx" +#include "util/OptionParser.hxx" #ifdef ENABLE_ARCHIVE #include "archive/ArchiveList.hxx" @@ -38,6 +40,27 @@ #include #include +struct CommandLine { + const char *uri = nullptr; +}; + +static CommandLine +ParseCommandLine(int argc, char **argv) +{ + CommandLine c; + + OptionParser option_parser(nullptr, argc, argv); + while (auto o = option_parser.Next()) { + } + + auto args = option_parser.GetRemaining(); + if (args.size != 1) + throw std::runtime_error("Usage: run_input URI"); + + c.uri = args.front(); + return c; +} + class GlobalInit { EventThread io_thread; @@ -107,10 +130,7 @@ dump_input_stream(InputStream *is) int main(int argc, char **argv) try { - if (argc != 2) { - fprintf(stderr, "Usage: run_input URI\n"); - return EXIT_FAILURE; - } + const auto c = ParseCommandLine(argc, argv); /* initialize MPD */ @@ -120,7 +140,7 @@ try { Mutex mutex; Cond cond; - auto is = InputStream::OpenReady(argv[1], mutex, cond); + auto is = InputStream::OpenReady(c.uri, mutex, cond); return dump_input_stream(is.get()); } catch (const std::exception &e) { LogError(e);