Skip to content

Commit dfed0b4

Browse files
authored
25-1: TOptionsParseResult: throwing an exception in case of options parsing errors (ydb-platform#16710)
1 parent b1c1f5a commit dfed0b4

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

ydb/public/lib/ydb_cli/common/command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ int TClientCommand::Process(TConfig& config) {
204204
}
205205

206206
void TClientCommand::SaveParseResult(TConfig& config) {
207-
ParseResult = std::make_shared<NLastGetopt::TOptsParseResult>(config.Opts, config.ArgC, config.ArgV);
207+
ParseResult = std::make_shared<TCommandOptsParseResult>(config.Opts, config.ArgC, config.ArgV);
208208
}
209209

210210
void TClientCommand::Prepare(TConfig& config) {

ydb/public/lib/ydb_cli/common/command.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,36 @@ class TClientCommand {
326326
}
327327
};
328328

329-
class TOptsParseOneLevelResult : public NLastGetopt::TOptsParseResult {
329+
class TCommandOptsParseResult: public NLastGetopt::TOptsParseResult {
330+
public:
331+
TCommandOptsParseResult(const NLastGetopt::TOpts* options, int argc, const char* argv[]) {
332+
Init(options, argc, argv);
333+
}
334+
TCommandOptsParseResult(const NLastGetopt::TOpts* options, int argc, char* argv[]) {
335+
Init(options, argc, const_cast<const char**>(argv));
336+
}
337+
virtual ~TCommandOptsParseResult() = default;
338+
339+
void HandleError() const override {
340+
if (ThrowOnParseError) {
341+
throw;
342+
}
343+
NLastGetopt::TOptsParseResult::HandleError();
344+
}
345+
346+
protected:
347+
TCommandOptsParseResult() = default;
348+
349+
void Init(const NLastGetopt::TOpts* options, int argc, const char* argv[]) {
350+
ThrowOnParseError = options->HasLongOption("throw-on-parse-error");
351+
NLastGetopt::TOptsParseResult::Init(options, argc, argv);
352+
}
353+
354+
private:
355+
bool ThrowOnParseError = false;
356+
};
357+
358+
class TOptsParseOneLevelResult : public TCommandOptsParseResult {
330359
public:
331360
TOptsParseOneLevelResult(TConfig& config);
332361
};

0 commit comments

Comments
 (0)