Skip to content

Commit a75c8b7

Browse files
committed
Sync all CLI fixes from arcadia
1 parent ad04714 commit a75c8b7

File tree

9 files changed

+57
-21
lines changed

9 files changed

+57
-21
lines changed

ydb/public/lib/ydb_cli/commands/topic_workload/topic_workload_describe.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ NYdb::NTopic::TTopicDescription TCommandWorkloadTopicDescribe::DescribeTopic(con
2626

2727
if (!result.IsSuccess() || result.GetIssues()) {
2828
Cerr << "Error describe topic " << fullTopicName << "\n" << (NYdb::TStatus)result << "\n";
29-
exit(EXIT_FAILURE);
29+
throw yexception() << "Error describe topic " << fullTopicName;
3030
}
3131

3232
NYdb::NTopic::TTopicDescription description = result.GetTopicDescription();
3333
if (description.GetTotalPartitionsCount() == 0) {
3434
Cerr << "Topic " << fullTopicName << " does not have partitions.\n";
35-
exit(EXIT_FAILURE);
35+
throw yexception() << "Topic " << fullTopicName << " does not have partitions.";
3636
}
3737

3838
return description;

ydb/public/lib/ydb_cli/commands/topic_workload/topic_workload_reader.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ void TTopicWorkloadReader::ReaderLoop(TTopicWorkloadReaderParams& params, TInsta
3535

3636
if (!std::any_of(consumers.begin(), consumers.end(), [consumerName](const auto& consumer) { return consumer.GetConsumerName() == consumerName; }))
3737
{
38-
WRITE_LOG(params.Log, ELogPriority::TLOG_EMERG, TStringBuilder() << "Topic '" << params.TopicName << "' doesn't have a consumer '" << consumerName << "'. Run command 'workload init' with parameter '--consumers'.");
39-
exit(EXIT_FAILURE);
38+
throw yexception() << "Topic '" << params.TopicName << "' doesn't have a consumer '" << consumerName << "'. Run command 'workload init' with parameter '--consumers'.";
4039
}
4140
settings.ConsumerName(consumerName).AppendTopics(std::string{params.TopicName});
4241
} else {

ydb/public/lib/ydb_cli/commands/ydb_dynamic_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class TCommandConfigVolatileDrop : public TYdbCommand {
102102
int Run(TConfig& config) override;
103103

104104
private:
105-
ui64 Version;
105+
ui64 Version = 0;
106106
TString Cluster;
107107
THashSet<ui64> Ids;
108108
TString Dir;

ydb/public/lib/ydb_cli/commands/ydb_root_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void TClientCommandRootCommon::ValidateSettings() {
8080
} else {
8181
return;
8282
}
83-
exit(EXIT_FAILURE);
83+
throw yexception() << "Invalid client settings";
8484
}
8585

8686
void TClientCommandRootCommon::FillConfig(TConfig& config) {

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ bool TClientCommand::PROGRESS_REQUESTS = false; // display progress of long requ
1212

1313
using namespace NUtils;
1414

15+
namespace {
16+
static bool HelpPrintedNeedToExit = false;
17+
void PrintUsageAndThrowHelpPrinted(const NLastGetopt::TOptsParser* parser) {
18+
parser->PrintUsage();
19+
HelpPrintedNeedToExit = true;
20+
throw THelpPrintedException();
21+
}
22+
23+
void PrintSvnVersionAndThrowHelpPrinted(const NLastGetopt::TOptsParser* parser) {
24+
parser->PrintUsage();
25+
HelpPrintedNeedToExit = true;
26+
throw THelpPrintedException();
27+
}
28+
}
29+
1530
TClientCommand::TClientCommand(
1631
const TString& name,
1732
const std::initializer_list<TString>& aliases,
@@ -22,11 +37,17 @@ TClientCommand::TClientCommand(
2237
, Description(description)
2338
, Visible(visible)
2439
, Parent(nullptr)
25-
, Opts(NLastGetopt::TOpts::Default())
2640
{
27-
HideOption("svnrevision");
28-
Opts.AddHelpOption('h');
29-
ChangeOptionDescription("help", "Print usage, -hh for detailed help");
41+
Opts.Opts_.clear();
42+
Opts.AddLongOption('V', "svnrevision", "print svn version")
43+
.HasArg(NLastGetopt::EHasArg::NO_ARGUMENT)
44+
.IfPresentDisableCompletion()
45+
.Handler(&PrintSvnVersionAndThrowHelpPrinted)
46+
.Hidden();
47+
Opts.AddLongOption('h', "help", "Print usage, -hh for detailed help")
48+
.HasArg(NLastGetopt::EHasArg::NO_ARGUMENT)
49+
.IfPresentDisableCompletion()
50+
.Handler(&PrintUsageAndThrowHelpPrinted);
3051
auto terminalWidth = GetTerminalWidth();
3152
size_t lineLength = terminalWidth ? *terminalWidth : Max<size_t>();
3253
Opts.SetWrap(Max(Opts.Wrap_, static_cast<ui32>(lineLength)));
@@ -157,9 +178,23 @@ TClientCommand::TOptsParseOneLevelResult::TOptsParseOneLevelResult(TConfig& conf
157178
}
158179
++_argc;
159180
}
181+
if (_argc > config.ArgC) {
182+
// This is possible if the last option should have an argument, but it is not provided
183+
_argc = config.ArgC;
184+
}
160185
Init(config.Opts, _argc, const_cast<const char**>(config.ArgV));
161186
}
162187

188+
void TClientCommand::TCommandOptsParseResult::HandleError() const {
189+
if (HelpPrintedNeedToExit) {
190+
throw THelpPrintedException();
191+
}
192+
if (ThrowOnParseError) {
193+
throw;
194+
}
195+
NLastGetopt::TOptsParseResult::HandleError();
196+
}
197+
163198
void TClientCommand::CheckForExecutableOptions(TConfig& config) {
164199
int argc = 1;
165200

@@ -221,8 +256,13 @@ int TClientCommand::Run(TConfig& config) {
221256
}
222257

223258
int TClientCommand::Process(TConfig& config) {
224-
Prepare(config);
225-
return ValidateAndRun(config);
259+
try {
260+
Prepare(config);
261+
return ValidateAndRun(config);
262+
} catch (const THelpPrintedException& e) {
263+
// Help was printed on demand, return EXIT_SUCCESS
264+
return EXIT_SUCCESS;
265+
}
226266
}
227267

228268
void TClientCommand::SaveParseResult(TConfig& config) {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,7 @@ class TClientCommand {
336336
}
337337
virtual ~TCommandOptsParseResult() = default;
338338

339-
void HandleError() const override {
340-
if (ThrowOnParseError) {
341-
throw;
342-
}
343-
NLastGetopt::TOptsParseResult::HandleError();
344-
}
339+
void HandleError() const override;
345340

346341
protected:
347342
TCommandOptsParseResult(bool throwOnParseError)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ TString InputPassword() {
161161
#elif defined(_win_)
162162
SetConsoleMode(hStdin, mode);
163163
#endif
164-
exit(EXIT_FAILURE);
164+
throw yexception() << "Input interrupted";
165165
} else {
166166
Cerr << '*';
167167
password.push_back(c);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class TMisuseException : public yexception {};
2929
// Print command help
3030
class TMisuseWithHelpException : public TMisuseException {};
3131

32+
// Help was printed, just need to return EXIT_SUCCESS
33+
class THelpPrintedException : public yexception {};
34+
3235
class TProfileConfig {
3336
public:
3437
TProfileConfig(const TString& profileName);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ class TProfileManager : public IProfileManager {
164164
if (TFileStat(configFilePath).Mode & (S_IRGRP | S_IROTH)) {
165165
int chmodResult = Chmod(configFilePath.GetPath().c_str(), S_IRUSR | S_IWUSR);
166166
if (chmodResult) {
167-
Cerr << "Couldn't change permissions for the file \"" << configFilePath.GetPath() << "\"" << Endl;
168-
exit(chmodResult);
167+
throw yexception() << "Couldn't change permissions for the file \"" << configFilePath.GetPath() << "\"";
169168
}
170169
}
171170
TFileOutput resultConfigFile(TFile(configFilePath, CreateAlways | WrOnly | AWUser | ARUser));

0 commit comments

Comments
 (0)