Skip to content

Commit 26f8e58

Browse files
committed
Refactor short option handling (#15614)
1 parent cea6a08 commit 26f8e58

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

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

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,24 @@ TClientCommand::TOptsParseOneLevelResult::TOptsParseOneLevelResult(TConfig& conf
133133
optName = optName.substr(0, eqPos);
134134
if (optName.StartsWith("--")) {
135135
opt = config.Opts->FindLongOption(optName.substr(2));
136-
} else {
137-
opt = config.Opts->FindCharOption(optName[1]);
138-
}
139-
if (opt != nullptr && opt->GetHasArg() != NLastGetopt::NO_ARGUMENT) {
140-
if (eqPos == TStringBuf::npos) {
136+
if (opt != nullptr && opt->GetHasArg() != NLastGetopt::NO_ARGUMENT && eqPos == TStringBuf::npos) {
141137
++_argc;
142138
}
139+
} else {
140+
if (optName.length() > 2) {
141+
// Char option list
142+
if (eqPos != TStringBuf::npos) {
143+
throw yexception() << "Char option list " << optName << " can not be followed by \"=\" sign";
144+
}
145+
} else if (optName.length() == 2) {
146+
// Single char option
147+
opt = config.Opts->FindCharOption(optName[1]);
148+
if (opt != nullptr && opt->GetHasArg() != NLastGetopt::NO_ARGUMENT && eqPos == TStringBuf::npos) {
149+
++_argc;
150+
}
151+
} else {
152+
throw yexception() << "Wrong CLI argument \"" << optName << "\"";
153+
}
143154
}
144155
} else {
145156
--levels;
@@ -149,31 +160,6 @@ TClientCommand::TOptsParseOneLevelResult::TOptsParseOneLevelResult(TConfig& conf
149160
Init(config.Opts, _argc, const_cast<const char**>(config.ArgV));
150161
}
151162

152-
void TClientCommand::CheckForExecutableOptions(TConfig& config) {
153-
int argc = 1;
154-
155-
while (argc < config.ArgC && config.ArgV[argc][0] == '-') {
156-
const NLastGetopt::TOpt* opt = nullptr;
157-
TStringBuf optName(config.ArgV[argc]);
158-
auto eqPos = optName.find('=');
159-
optName = optName.substr(0, eqPos);
160-
if (optName.StartsWith("--")) {
161-
opt = config.Opts->FindLongOption(optName.substr(2));
162-
} else {
163-
opt = config.Opts->FindCharOption(optName[1]);
164-
}
165-
if (config.ExecutableOptions.find(optName) != config.ExecutableOptions.end()) {
166-
config.HasExecutableOptions = true;
167-
}
168-
if (opt != nullptr && opt->GetHasArg() != NLastGetopt::NO_ARGUMENT) {
169-
if (eqPos == TStringBuf::npos) {
170-
++argc;
171-
}
172-
}
173-
++argc;
174-
}
175-
}
176-
177163
void TClientCommand::Config(TConfig& config) {
178164
config.Opts = &Opts;
179165
config.OnlyExplicitProfile = OnlyExplicitProfile;
@@ -212,7 +198,6 @@ void TClientCommand::Prepare(TConfig& config) {
212198
config.ArgsSettings = TConfig::TArgSettings();
213199
config.Opts = &Opts;
214200
Config(config);
215-
CheckForExecutableOptions(config);
216201
config.CheckParamsCount();
217202
SetCustomUsage(config);
218203
SaveParseResult(config);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ class TClientCommand {
102102
TVector<TString> Tokens;
103103
TString SecurityToken;
104104
TList<TCommandInfo> ParentCommands;
105-
THashSet<TString> ExecutableOptions;
106-
bool HasExecutableOptions = false;
107105
TString Path;
108106
TArgSettings ArgsSettings;
109107
TString Address;
@@ -225,7 +223,7 @@ class TClientCommand {
225223

226224
void CheckParamsCount() {
227225
size_t count = GetParamsCount();
228-
if (HasHelpCommand() || HasExecutableOptions) {
226+
if (HasHelpCommand()) {
229227
return;
230228
}
231229
bool minSet = ArgsSettings.Min.GetIsSet();
@@ -407,7 +405,6 @@ class TClientCommand {
407405
private:
408406
void HideOption(const TString& name);
409407
void ChangeOptionDescription(const TString& name, const TString& description);
410-
void CheckForExecutableOptions(TConfig& config);
411408

412409
constexpr static int DESCRIPTION_ALIGNMENT = 28;
413410
};

0 commit comments

Comments
 (0)