Skip to content

Commit c8482a7

Browse files
author
tldr
committed
[arc][getopt]fix enforcment of RequiredArgument().DisableSpaceParse()
commit_hash:9c2ada5b95be3f810d2750406d74d839bd2598ff
1 parent cafe0d9 commit c8482a7

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

library/cpp/getopt/small/last_getopt_parse_result.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ namespace NLastGetopt {
192192
bool Has(const TOpt* opt, bool includeDefault = false) const;
193193

194194
/**
195-
* @return nil terminated string on the last fetched argument of givne option
195+
* @return nil terminated string on the last fetched argument of given option
196196
*
197197
* @param opt ptr on required object
198198
* @param includeDefault search in results obtained from default values
199199
*/
200200
const char* Get(const TOpt* opt, bool includeDefault = true) const;
201201

202202
/**
203-
* @return nil terminated string on the last fetched argument of givne option
203+
* @return nil terminated string on the last fetched argument of given option
204204
* if option haven't been fetched, given defaultValue will be returned
205205
*
206206
* @param opt ptr on required object
@@ -218,15 +218,15 @@ namespace NLastGetopt {
218218
bool Has(const TString& name, bool includeDefault = false) const;
219219

220220
/**
221-
* @return nil terminated string on the last fetched argument of givne option
221+
* @return nil terminated string on the last fetched argument of given option
222222
*
223223
* @param name long name of required object
224224
* @param includeDefault search in results obtained from default values
225225
*/
226226
const char* Get(const TString& name, bool includeDefault = true) const;
227227

228228
/**
229-
* @return nil terminated string on the last fetched argument of givne option
229+
* @return nil terminated string on the last fetched argument of given option
230230
* if option haven't been fetched, given defaultValue will be returned
231231
*
232232
* @param name long name of required object
@@ -244,15 +244,15 @@ namespace NLastGetopt {
244244
bool Has(char name, bool includeDefault = false) const;
245245

246246
/**
247-
* @return nil terminated string on the last fetched argument of givne option
247+
* @return nil terminated string on the last fetched argument of given option
248248
*
249249
* @param c short name of required object
250250
* @param includeDefault search in results obtained from default values
251251
*/
252252
const char* Get(char name, bool includeDefault = true) const;
253253

254254
/**
255-
* @return nil terminated string on the last fetched argument of givne option
255+
* @return nil terminated string on the last fetched argument of given option
256256
* if option haven't been fetched, given defaultValue will be returned
257257
*
258258
* @param c short name of required object
@@ -261,7 +261,7 @@ namespace NLastGetopt {
261261
const char* GetOrElse(char name, const char* defaultValue) const;
262262

263263
/**
264-
* for givne option return parsed value of the last fetched argument
264+
* for given option return parsed value of the last fetched argument
265265
* if option haven't been fetched, HandleError action is called
266266
*
267267
* @param opt required option (one of: ptr, short name, long name).
@@ -280,7 +280,7 @@ namespace NLastGetopt {
280280
}
281281

282282
/**
283-
* for givne option return parsed value of the last fetched argument
283+
* for given option return parsed value of the last fetched argument
284284
* if option haven't been fetched, given defaultValue will be returned
285285
*
286286
* @param opt required option (one of: ptr, short name, long name).

library/cpp/getopt/small/last_getopt_parser.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,14 @@ namespace NLastGetopt {
170170

171171
bool TOptsParser::ParseOptParam(const TOpt* opt, size_t pos) {
172172
Y_ASSERT(opt);
173-
if (opt->GetHasArg() == NO_ARGUMENT || opt->IsEqParseOnly()) {
173+
if (opt->GetHasArg() == NO_ARGUMENT ||
174+
opt->GetHasArg() == OPTIONAL_ARGUMENT && opt->IsEqParseOnly()) {
174175
return Commit(opt, nullptr, pos, 0);
175176
}
177+
if (opt->IsEqParseOnly()) {
178+
Y_ASSERT(opt->GetHasArg() == REQUIRED_ARGUMENT);
179+
throw TUsageException() << "option " << opt->ToShortString() << " requires an argument but only accepts it over =";
180+
}
176181
if (pos == Argc_) {
177182
if (opt->GetHasArg() == REQUIRED_ARGUMENT)
178183
throw TUsageException() << "option " << opt->ToShortString() << " must have arg";

library/cpp/getopt/ut/last_getopt_ut.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,19 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) {
449449
tester.AcceptEndOfFreeArgs();
450450
}
451451

452+
Y_UNIT_TEST(TestEqParseOnlyRequiredArgument) {
453+
TOptsNoDefault opts;
454+
455+
opts.AddLongOption("eq-only").RequiredArgument().DisableSpaceParse();
456+
457+
TOptsParseResultTestWrapper res(&opts, V({"cmd", "--eq-only=value"}));
458+
UNIT_ASSERT_EQUAL(res.Get("eq-only"), "value"sv);
459+
460+
UNIT_ASSERT_EXCEPTION(
461+
TOptsParseResultTestWrapper(&opts, V({"cmd", "--eq-only", "value"})),
462+
TUsageException);
463+
}
464+
452465
Y_UNIT_TEST(TestStoreResult) {
453466
TOptsNoDefault opts;
454467
TString data;

0 commit comments

Comments
 (0)