aboutsummaryrefslogtreecommitdiffstats
path: root/library
diff options
context:
space:
mode:
authorbulatkhr <bulatkhr@yandex-team.com>2022-07-15 10:12:22 +0300
committerbulatkhr <bulatkhr@yandex-team.com>2022-07-15 10:12:22 +0300
commit03cf908feb326c966c4384fde4360f08d9b5b4e1 (patch)
tree29faaf859522f25ba29220000dac674ac364569a /library
parentb63fb26957f120f5cbf2a363d9fef60df2e7c2ae (diff)
downloadydb-03cf908feb326c966c4384fde4360f08d9b5b4e1.tar.gz
Allow skip arg in opt parser
add UnnecessaryArgument
Diffstat (limited to 'library')
-rw-r--r--library/cpp/getopt/small/last_getopt_opt.h18
-rw-r--r--library/cpp/getopt/small/last_getopt_parser.cpp2
-rw-r--r--library/cpp/getopt/ut/last_getopt_ut.cpp34
3 files changed, 53 insertions, 1 deletions
diff --git a/library/cpp/getopt/small/last_getopt_opt.h b/library/cpp/getopt/small/last_getopt_opt.h
index a8dd5adca9..e8567bd4f6 100644
--- a/library/cpp/getopt/small/last_getopt_opt.h
+++ b/library/cpp/getopt/small/last_getopt_opt.h
@@ -62,6 +62,7 @@ namespace NLastGetopt {
EHasArg HasArg_ = DEFAULT_HAS_ARG; // the argument parsing politics
bool Required_ = false; // option existence politics
+ bool EqParseOnly_ = false; // allows option not to read argument
bool AllowMultipleCompletion_ = false; // let the completer know that this option can occur more than once
@@ -286,6 +287,23 @@ namespace NLastGetopt {
}
/**
+ * allow only --option=arg parsing and disable --option arg
+ * @return self
+ */
+ TOpt& DisableSpaceParse() {
+ Y_ASSERT(GetHasArg() == OPTIONAL_ARGUMENT);
+ EqParseOnly_ = true;
+ return *this;
+ }
+
+ /**
+ * @return true if only --option=arg parse allowed
+ */
+ bool IsEqParseOnly() const {
+ return EqParseOnly_;
+ }
+
+ /**
* sets the option to be optional
* @return self
*/
diff --git a/library/cpp/getopt/small/last_getopt_parser.cpp b/library/cpp/getopt/small/last_getopt_parser.cpp
index 7668b12a03..911c76f342 100644
--- a/library/cpp/getopt/small/last_getopt_parser.cpp
+++ b/library/cpp/getopt/small/last_getopt_parser.cpp
@@ -170,7 +170,7 @@ namespace NLastGetopt {
bool TOptsParser::ParseOptParam(const TOpt* opt, size_t pos) {
Y_ASSERT(opt);
- if (opt->GetHasArg() == NO_ARGUMENT) {
+ if (opt->GetHasArg() == NO_ARGUMENT || opt->IsEqParseOnly()) {
return Commit(opt, nullptr, pos, 0);
}
if (pos == Argc_) {
diff --git a/library/cpp/getopt/ut/last_getopt_ut.cpp b/library/cpp/getopt/ut/last_getopt_ut.cpp
index c99a1d053d..6606000ca6 100644
--- a/library/cpp/getopt/ut/last_getopt_ut.cpp
+++ b/library/cpp/getopt/ut/last_getopt_ut.cpp
@@ -415,6 +415,40 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) {
}
#endif
+ Y_UNIT_TEST(TestEqParseOnly) {
+ TOptsParserTester tester;
+
+ tester.Argv_.push_back("cmd");
+ tester.Argv_.push_back("--data=jjhh");
+ tester.Argv_.push_back("-n");
+ tester.Argv_.push_back("11");
+ tester.Argv_.push_back("--optional-number-1=8");
+ tester.Argv_.push_back("--optional-string-1=os1");
+ tester.Argv_.push_back("--optional-number-2");
+ tester.Argv_.push_back("10");
+ tester.Argv_.push_back("--optional-string-2");
+ tester.Argv_.push_back("freearg");
+
+ tester.Opts_.AddLongOption('d', "data");
+ tester.Opts_.AddLongOption('n', "number");
+ tester.Opts_.AddLongOption("optional-string-0");
+ tester.Opts_.AddLongOption("optional-number-0");
+ tester.Opts_.AddLongOption("optional-string-1");
+ tester.Opts_.AddLongOption("optional-number-1");
+ tester.Opts_.AddLongOption("optional-string-2").OptionalArgument().DisableSpaceParse();
+ tester.Opts_.AddLongOption("optional-number-2").OptionalArgument();
+
+ tester.AcceptOptionWithValue("data", "jjhh");
+ tester.AcceptOptionWithValue('n', "11");
+ tester.AcceptOptionWithValue("optional-number-1", "8");
+ tester.AcceptOptionWithValue("optional-string-1", "os1");
+ tester.AcceptOptionWithValue("optional-number-2", "10");
+ tester.AcceptOptionWithoutValue("optional-string-2");
+ tester.AcceptEndOfOptions();
+ tester.AcceptFreeArg("freearg");
+ tester.AcceptEndOfFreeArgs();
+ }
+
Y_UNIT_TEST(TestStoreResult) {
TOptsNoDefault opts;
TString data;