Fix handling exception 'unrecognised option' in clickhouse-local and client

This commit is contained in:
avogar 2021-10-14 16:34:05 +03:00
parent 9c7bef4c9d
commit a1a4df2501
8 changed files with 29 additions and 25 deletions

View File

@ -996,7 +996,7 @@ void Client::printHelpMessage(const OptionsDescription & options_description)
} }
void Client::addAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments) void Client::addOptions(OptionsDescription & options_description)
{ {
/// Main commandline options related to client functionality and all parameters from Settings. /// Main commandline options related to client functionality and all parameters from Settings.
options_description.main_description->add_options() options_description.main_description->add_options()
@ -1053,14 +1053,6 @@ void Client::addAndCheckOptions(OptionsDescription & options_description, po::va
( (
"types", po::value<std::string>(), "types" "types", po::value<std::string>(), "types"
); );
cmd_settings.addProgramOptions(options_description.main_description.value());
/// Parse main commandline options.
po::parsed_options parsed = po::command_line_parser(arguments).options(options_description.main_description.value()).run();
auto unrecognized_options = po::collect_unrecognized(parsed.options, po::collect_unrecognized_mode::include_positional);
if (unrecognized_options.size() > 1)
throw Exception(ErrorCodes::UNRECOGNIZED_ARGUMENTS, "Unrecognized option '{}'", unrecognized_options[1]);
po::store(parsed, options);
} }

View File

@ -24,7 +24,7 @@ protected:
String getName() const override { return "client"; } String getName() const override { return "client"; }
void printHelpMessage(const OptionsDescription & options_description) override; void printHelpMessage(const OptionsDescription & options_description) override;
void addAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments) override; void addOptions(OptionsDescription & options_description) override;
void processOptions(const OptionsDescription & options_description, const CommandLineOptions & options, void processOptions(const OptionsDescription & options_description, const CommandLineOptions & options,
const std::vector<Arguments> & external_tables_arguments) override; const std::vector<Arguments> & external_tables_arguments) override;
void processConfig() override; void processConfig() override;

View File

@ -17,7 +17,6 @@
#include <Common/Exception.h> #include <Common/Exception.h>
#include <Common/Macros.h> #include <Common/Macros.h>
#include <Common/Config/ConfigProcessor.h> #include <Common/Config/ConfigProcessor.h>
#include <Common/escapeForFileName.h>
#include <Common/ThreadStatus.h> #include <Common/ThreadStatus.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <loggers/Loggers.h> #include <loggers/Loggers.h>
@ -35,7 +34,6 @@
#include <Disks/registerDisks.h> #include <Disks/registerDisks.h>
#include <Formats/registerFormats.h> #include <Formats/registerFormats.h>
#include <boost/program_options/options_description.hpp> #include <boost/program_options/options_description.hpp>
#include <boost/program_options.hpp>
#include <base/argsToConfig.h> #include <base/argsToConfig.h>
#include <Common/randomSeed.h> #include <Common/randomSeed.h>
#include <filesystem> #include <filesystem>
@ -636,7 +634,7 @@ void LocalServer::printHelpMessage(const OptionsDescription & options_descriptio
} }
void LocalServer::addAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments) void LocalServer::addOptions(OptionsDescription & options_description)
{ {
options_description.main_description->add_options() options_description.main_description->add_options()
("database,d", po::value<std::string>(), "database") ("database,d", po::value<std::string>(), "database")
@ -655,10 +653,6 @@ void LocalServer::addAndCheckOptions(OptionsDescription & options_description, p
("no-system-tables", "do not attach system tables (better startup time)") ("no-system-tables", "do not attach system tables (better startup time)")
; ;
cmd_settings.addProgramOptions(options_description.main_description.value());
po::parsed_options parsed = po::command_line_parser(arguments).options(options_description.main_description.value()).run();
po::store(parsed, options);
} }
@ -713,10 +707,11 @@ int mainEntryClickHouseLocal(int argc, char ** argv)
app.init(argc, argv); app.init(argc, argv);
return app.run(); return app.run();
} }
catch (const boost::program_options::error & e) catch (const DB::Exception & e)
{ {
std::cerr << "Bad arguments: " << e.what() << std::endl; std::cerr << DB::getExceptionMessage(e, false) << std::endl;
return DB::ErrorCodes::BAD_ARGUMENTS; auto code = DB::getCurrentExceptionCode();
return code ? code : 1;
} }
catch (...) catch (...)
{ {

View File

@ -40,7 +40,7 @@ protected:
String getQueryTextPrefix() override; String getQueryTextPrefix() override;
void printHelpMessage(const OptionsDescription & options_description) override; void printHelpMessage(const OptionsDescription & options_description) override;
void addAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments) override; void addOptions(OptionsDescription & options_description) override;
void processOptions(const OptionsDescription & options_description, const CommandLineOptions & options, void processOptions(const OptionsDescription & options_description, const CommandLineOptions & options,
const std::vector<Arguments> &) override; const std::vector<Arguments> &) override;
void processConfig() override; void processConfig() override;

View File

@ -72,6 +72,7 @@ namespace ErrorCodes
extern const int UNEXPECTED_PACKET_FROM_SERVER; extern const int UNEXPECTED_PACKET_FROM_SERVER;
extern const int INVALID_USAGE_OF_INPUT; extern const int INVALID_USAGE_OF_INPUT;
extern const int CANNOT_SET_SIGNAL_HANDLER; extern const int CANNOT_SET_SIGNAL_HANDLER;
extern const int UNRECOGNIZED_ARGUMENTS;
} }
} }
@ -1505,6 +1506,19 @@ void ClientBase::readArguments(int argc, char ** argv, Arguments & common_argume
} }
} }
void ClientBase::parseAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments)
{
cmd_settings.addProgramOptions(options_description.main_description.value());
/// Parse main commandline options.
auto parser = po::command_line_parser(arguments).options(options_description.main_description.value());
parser.allow_unregistered();
po::parsed_options parsed = parser.run();
auto unrecognized_options = po::collect_unrecognized(parsed.options, po::collect_unrecognized_mode::include_positional);
if (unrecognized_options.size() > 1)
throw Exception(ErrorCodes::UNRECOGNIZED_ARGUMENTS, "Unrecognized option '{}'", unrecognized_options[1]);
po::store(parsed, options);
}
void ClientBase::init(int argc, char ** argv) void ClientBase::init(int argc, char ** argv)
{ {
@ -1562,7 +1576,8 @@ void ClientBase::init(int argc, char ** argv)
("stacktrace", "print stack traces of exceptions") ("stacktrace", "print stack traces of exceptions")
; ;
addAndCheckOptions(options_description, options, common_arguments); addOptions(options_description);
parseAndCheckOptions(options_description, options, common_arguments);
po::notify(options); po::notify(options);
if (options.count("version") || options.count("V")) if (options.count("version") || options.count("V"))

View File

@ -91,7 +91,7 @@ protected:
}; };
virtual void printHelpMessage(const OptionsDescription & options_description) = 0; virtual void printHelpMessage(const OptionsDescription & options_description) = 0;
virtual void addAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments) = 0; virtual void addOptions(OptionsDescription & options_description) = 0;
virtual void processOptions(const OptionsDescription & options_description, virtual void processOptions(const OptionsDescription & options_description,
const CommandLineOptions & options, const CommandLineOptions & options,
const std::vector<Arguments> & external_tables_arguments) = 0; const std::vector<Arguments> & external_tables_arguments) = 0;
@ -132,6 +132,7 @@ private:
void resetOutput(); void resetOutput();
void outputQueryInfo(bool echo_query_); void outputQueryInfo(bool echo_query_);
void readArguments(int argc, char ** argv, Arguments & common_arguments, std::vector<Arguments> & external_tables_arguments); void readArguments(int argc, char ** argv, Arguments & common_arguments, std::vector<Arguments> & external_tables_arguments);
void parseAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments);
protected: protected:
bool is_interactive = false; /// Use either interactive line editing interface or batch mode. bool is_interactive = false; /// Use either interactive line editing interface or batch mode.

View File

@ -1 +1,2 @@
Bad arguments: unrecognised option '--unknown-option' Code: 552. DB::Exception: Unrecognized option '--unknown-option'. (UNRECOGNIZED_ARGUMENTS)
Code: 552. DB::Exception: Unrecognized option '--unknown-option'. (UNRECOGNIZED_ARGUMENTS)

View File

@ -6,4 +6,4 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. "$CURDIR"/../shell_config.sh . "$CURDIR"/../shell_config.sh
${CLICKHOUSE_LOCAL} --unknown-option 2>&1 echo ${CLICKHOUSE_LOCAL} --unknown-option 2>&1 echo
${CLICKHOUSE_CLIENT} --unknown-option 2>&1 echo