test/run_storage: use the OptionParser class for command-line options
Introducing the option "--verbose".
This commit is contained in:
parent
ca8a2aeb7b
commit
5dd07ac040
|
@ -218,6 +218,7 @@ if enable_database
|
||||||
dependencies: [
|
dependencies: [
|
||||||
event_dep,
|
event_dep,
|
||||||
storage_glue_dep,
|
storage_glue_dep,
|
||||||
|
cmdline_dep,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
// Copyright The Music Player Daemon Project
|
// Copyright The Music Player Daemon Project
|
||||||
|
|
||||||
|
#include "cmdline/OptionDef.hxx"
|
||||||
|
#include "cmdline/OptionParser.hxx"
|
||||||
#include "event/Thread.hxx"
|
#include "event/Thread.hxx"
|
||||||
#include "storage/Registry.hxx"
|
#include "storage/Registry.hxx"
|
||||||
#include "storage/StorageInterface.hxx"
|
#include "storage/StorageInterface.hxx"
|
||||||
|
@ -11,6 +13,8 @@
|
||||||
#include "util/PrintException.hxx"
|
#include "util/PrintException.hxx"
|
||||||
#include "util/StringAPI.hxx"
|
#include "util/StringAPI.hxx"
|
||||||
#include "util/StringBuffer.hxx"
|
#include "util/StringBuffer.hxx"
|
||||||
|
#include "Log.hxx"
|
||||||
|
#include "LogBackend.hxx"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -20,13 +24,55 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static constexpr auto usage_text = R"(Usage: run_storage COMMAND URI ...
|
static constexpr auto usage_text = R"(Usage: run_storage [OPTIONS] COMMAND URI ...
|
||||||
|
|
||||||
|
Options:
|
||||||
|
--verbose
|
||||||
|
|
||||||
Available commands:
|
Available commands:
|
||||||
ls URI PATH
|
ls URI PATH
|
||||||
stat URI PATH
|
stat URI PATH
|
||||||
)";
|
)";
|
||||||
|
|
||||||
|
struct CommandLine {
|
||||||
|
bool verbose = false;
|
||||||
|
|
||||||
|
const char *command;
|
||||||
|
|
||||||
|
std::span<const char *const> args;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class 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 (static_cast<Option>(o.index)) {
|
||||||
|
case Option::VERBOSE:
|
||||||
|
c.verbose = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto args = option_parser.GetRemaining();
|
||||||
|
if (args.empty())
|
||||||
|
throw std::runtime_error{usage_text};
|
||||||
|
|
||||||
|
c.command = args.front();
|
||||||
|
c.args = args.subspan(1);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
class GlobalInit {
|
class GlobalInit {
|
||||||
const ScopeNetInit net_init;
|
const ScopeNetInit net_init;
|
||||||
EventThread io_thread;
|
EventThread io_thread;
|
||||||
|
@ -116,35 +162,32 @@ Stat(Storage &storage, const char *path)
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
try {
|
try {
|
||||||
if (argc < 3) {
|
const auto c = ParseCommandLine(argc, argv);
|
||||||
fputs(usage_text, stderr);
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *const command = argv[1];
|
|
||||||
const char *const storage_uri = argv[2];
|
|
||||||
|
|
||||||
|
SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);
|
||||||
GlobalInit init;
|
GlobalInit init;
|
||||||
|
|
||||||
if (StringIsEqual(command, "ls")) {
|
if (StringIsEqual(c.command, "ls")) {
|
||||||
if (argc != 4) {
|
if (c.args.size() != 2) {
|
||||||
fputs(usage_text, stderr);
|
fputs(usage_text, stderr);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *const path = argv[3];
|
const char *const storage_uri = c.args[0];
|
||||||
|
const char *const path = c.args[1];
|
||||||
|
|
||||||
auto storage = MakeStorage(init.GetEventLoop(),
|
auto storage = MakeStorage(init.GetEventLoop(),
|
||||||
storage_uri);
|
storage_uri);
|
||||||
|
|
||||||
return Ls(*storage, path);
|
return Ls(*storage, path);
|
||||||
} else if (StringIsEqual(command, "stat")) {
|
} else if (StringIsEqual(c.command, "stat")) {
|
||||||
if (argc != 4) {
|
if (c.args.size() != 2) {
|
||||||
fputs(usage_text, stderr);
|
fputs(usage_text, stderr);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *const path = argv[3];
|
const char *const storage_uri = c.args[0];
|
||||||
|
const char *const path = c.args[1];
|
||||||
|
|
||||||
auto storage = MakeStorage(init.GetEventLoop(),
|
auto storage = MakeStorage(init.GetEventLoop(),
|
||||||
storage_uri);
|
storage_uri);
|
||||||
|
|
Loading…
Reference in New Issue