From edf74d1cb191bcfe7307e14e229f2e10c0e8c4a6 Mon Sep 17 00:00:00 2001 From: Peter Dillinger Date: Tue, 9 Jun 2020 10:01:12 -0700 Subject: [PATCH] Add --version and --help to ldb and sst_dump (#6951) Summary: as title Pull Request resolved: https://github.com/facebook/rocksdb/pull/6951 Test Plan: tests included + manual Reviewed By: ajkr Differential Revision: D21918540 Pulled By: pdillinger fbshipit-source-id: 79d4991f2a831214fc7e477a839ec19dbbace6c5 --- include/rocksdb/sst_dump_tool.h | 2 +- include/rocksdb/utilities/ldb_cmd.h | 8 +++++--- tools/ldb_cmd.cc | 2 +- tools/ldb_cmd_test.cc | 12 ++++++++++++ tools/ldb_tool.cc | 22 ++++++++++++++++------ tools/sst_dump_test.cc | 14 ++++++++++++++ tools/sst_dump_tool.cc | 19 +++++++++++++------ 7 files changed, 62 insertions(+), 17 deletions(-) diff --git a/include/rocksdb/sst_dump_tool.h b/include/rocksdb/sst_dump_tool.h index ecb692e31..9261ba47d 100644 --- a/include/rocksdb/sst_dump_tool.h +++ b/include/rocksdb/sst_dump_tool.h @@ -11,7 +11,7 @@ namespace ROCKSDB_NAMESPACE { class SSTDumpTool { public: - int Run(int argc, char** argv, Options options = Options()); + int Run(int argc, char const* const* argv, Options options = Options()); }; } // namespace ROCKSDB_NAMESPACE diff --git a/include/rocksdb/utilities/ldb_cmd.h b/include/rocksdb/utilities/ldb_cmd.h index 874d36e18..66da199e5 100644 --- a/include/rocksdb/utilities/ldb_cmd.h +++ b/include/rocksdb/utilities/ldb_cmd.h @@ -78,7 +78,7 @@ class LDBCommand { SelectCommand); static LDBCommand* InitFromCmdLineArgs( - int argc, char** argv, const Options& options, + int argc, char const* const* argv, const Options& options, const LDBOptions& ldb_options, const std::vector* column_families); @@ -269,11 +269,13 @@ class LDBCommand { class LDBCommandRunner { public: - static void PrintHelp(const LDBOptions& ldb_options, const char* exec_name); + static void PrintHelp(const LDBOptions& ldb_options, const char* exec_name, + bool to_stderr = true); // Returns the status code to return. 0 is no error. static int RunCommand( - int argc, char** argv, Options options, const LDBOptions& ldb_options, + int argc, char const* const* argv, Options options, + const LDBOptions& ldb_options, const std::vector* column_families); }; diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index 8e63eee03..0859f7d15 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -97,7 +97,7 @@ void DumpSstFile(Options options, std::string filename, bool output_hex, }; LDBCommand* LDBCommand::InitFromCmdLineArgs( - int argc, char** argv, const Options& options, + int argc, char const* const* argv, const Options& options, const LDBOptions& ldb_options, const std::vector* column_families) { std::vector args; diff --git a/tools/ldb_cmd_test.cc b/tools/ldb_cmd_test.cc index 549e31514..e485ab551 100644 --- a/tools/ldb_cmd_test.cc +++ b/tools/ldb_cmd_test.cc @@ -41,6 +41,18 @@ class LdbCmdTest : public testing::Test { std::shared_ptr env_guard_; }; +TEST_F(LdbCmdTest, HelpAndVersion) { + Options o; + o.env = TryLoadCustomOrDefaultEnv(); + LDBOptions lo; + static const char* help[] = {"./ldb", "--help"}; + ASSERT_EQ(0, LDBCommandRunner::RunCommand(2, help, o, lo, nullptr)); + static const char* version[] = {"./ldb", "--version"}; + ASSERT_EQ(0, LDBCommandRunner::RunCommand(2, version, o, lo, nullptr)); + static const char* bad[] = {"./ldb", "--not_an_option"}; + ASSERT_NE(0, LDBCommandRunner::RunCommand(2, bad, o, lo, nullptr)); +} + TEST_F(LdbCmdTest, HexToString) { // map input to expected outputs. // odd number of "hex" half bytes doesn't make sense diff --git a/tools/ldb_tool.cc b/tools/ldb_tool.cc index f9374955a..c4fa97cc6 100644 --- a/tools/ldb_tool.cc +++ b/tools/ldb_tool.cc @@ -13,7 +13,7 @@ namespace ROCKSDB_NAMESPACE { LDBOptions::LDBOptions() {} void LDBCommandRunner::PrintHelp(const LDBOptions& ldb_options, - const char* /*exec_name*/) { + const char* /*exec_name*/, bool to_stderr) { std::string ret; ret.append(ldb_options.print_help_header); @@ -99,22 +99,32 @@ void LDBCommandRunner::PrintHelp(const LDBOptions& ldb_options, WriteExternalSstFilesCommand::Help(ret); IngestExternalSstFilesCommand::Help(ret); - fprintf(stderr, "%s\n", ret.c_str()); + fprintf(to_stderr ? stderr : stdout, "%s\n", ret.c_str()); } int LDBCommandRunner::RunCommand( - int argc, char** argv, Options options, const LDBOptions& ldb_options, + int argc, char const* const* argv, Options options, + const LDBOptions& ldb_options, const std::vector* column_families) { if (argc <= 2) { - PrintHelp(ldb_options, argv[0]); - return 1; + if (std::string(argv[1]) == "--version") { + printf("ldb from RocksDB %d.%d.%d\n", ROCKSDB_MAJOR, ROCKSDB_MINOR, + ROCKSDB_PATCH); + return 0; + } else if (std::string(argv[1]) == "--help") { + PrintHelp(ldb_options, argv[0], /*to_stderr*/ false); + return 0; + } else { + PrintHelp(ldb_options, argv[0], /*to_stderr*/ true); + return 1; + } } LDBCommand* cmdObj = LDBCommand::InitFromCmdLineArgs( argc, argv, options, ldb_options, column_families); if (cmdObj == nullptr) { fprintf(stderr, "Unknown command\n"); - PrintHelp(ldb_options, argv[0]); + PrintHelp(ldb_options, argv[0], /*to_stderr*/ true); return 1; } diff --git a/tools/sst_dump_test.cc b/tools/sst_dump_test.cc index e3eeb68f7..b49c26806 100644 --- a/tools/sst_dump_test.cc +++ b/tools/sst_dump_test.cc @@ -129,6 +129,20 @@ class SSTDumpToolTest : public testing::Test { } }; +TEST_F(SSTDumpToolTest, HelpAndVersion) { + Options opts; + opts.env = env(); + + ROCKSDB_NAMESPACE::SSTDumpTool tool; + + static const char* help[] = {"./sst_dump", "--help"}; + ASSERT_TRUE(!tool.Run(2, help, opts)); + static const char* version[] = {"./sst_dump", "--version"}; + ASSERT_TRUE(!tool.Run(2, version, opts)); + static const char* bad[] = {"./sst_dump", "--not_an_option"}; + ASSERT_TRUE(tool.Run(2, bad, opts)); +} + TEST_F(SSTDumpToolTest, EmptyFilter) { Options opts; opts.env = env(); diff --git a/tools/sst_dump_tool.cc b/tools/sst_dump_tool.cc index b848496b2..cff5d0552 100644 --- a/tools/sst_dump_tool.cc +++ b/tools/sst_dump_tool.cc @@ -481,9 +481,9 @@ Status SstFileDumper::ReadTableProperties( namespace { -void print_help() { +void print_help(bool to_stderr) { fprintf( - stderr, + to_stderr ? stderr : stdout, R"(sst_dump --file= [--command=check|scan|raw|recompress|identify] --file= Path to SST file or directory containing SST files @@ -572,7 +572,7 @@ bool ParseIntArg(const char* arg, const std::string arg_name, } } // namespace -int SSTDumpTool::Run(int argc, char** argv, Options options) { +int SSTDumpTool::Run(int argc, char const* const* argv, Options options) { const char* env_uri = nullptr; const char* dir_or_file = nullptr; uint64_t read_num = std::numeric_limits::max(); @@ -696,10 +696,17 @@ int SSTDumpTool::Run(int argc, char** argv, Options options) { "compression_level_to must be numeric", &tmp_val)) { has_compression_level_to = true; compress_level_to = static_cast(tmp_val); + } else if (strcmp(argv[i], "--help") == 0) { + print_help(/*to_stderr*/ false); + return 0; + } else if (strcmp(argv[i], "--version") == 0) { + printf("sst_dump from RocksDB %d.%d.%d\n", ROCKSDB_MAJOR, ROCKSDB_MINOR, + ROCKSDB_PATCH); + return 0; } else { fprintf(stderr, "Unrecognized argument '%s'\n\n", argv[i]); - print_help(); - exit(1); + print_help(/*to_stderr*/ true); + return 1; } } @@ -730,7 +737,7 @@ int SSTDumpTool::Run(int argc, char** argv, Options options) { if (dir_or_file == nullptr) { fprintf(stderr, "file or directory must be specified.\n\n"); - print_help(); + print_help(/*to_stderr*/ true); exit(1); }