aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/small
diff options
context:
space:
mode:
authornga <nga@yandex-team.ru>2022-02-10 16:48:09 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:48:09 +0300
commit1f553f46fb4f3c5eec631352cdd900a0709016af (patch)
treea231fba2c03b440becaea6c86a2702d0bfb0336e /library/cpp/getopt/small
parentc4de7efdedc25b49cbea74bd589eecb61b55b60a (diff)
downloadydb-1f553f46fb4f3c5eec631352cdd900a0709016af.tar.gz
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/getopt/small')
-rw-r--r--library/cpp/getopt/small/last_getopt.cpp2
-rw-r--r--library/cpp/getopt/small/last_getopt.h20
-rw-r--r--library/cpp/getopt/small/last_getopt_support.h24
-rw-r--r--library/cpp/getopt/small/opt.cpp120
-rw-r--r--library/cpp/getopt/small/opt.h170
-rw-r--r--library/cpp/getopt/small/opt2.h6
-rw-r--r--library/cpp/getopt/small/posix_getopt.cpp110
-rw-r--r--library/cpp/getopt/small/posix_getopt.h56
8 files changed, 254 insertions, 254 deletions
diff --git a/library/cpp/getopt/small/last_getopt.cpp b/library/cpp/getopt/small/last_getopt.cpp
index 30669b2c5a..d76420629d 100644
--- a/library/cpp/getopt/small/last_getopt.cpp
+++ b/library/cpp/getopt/small/last_getopt.cpp
@@ -6,4 +6,4 @@ namespace NLastGetopt {
exit(0);
}
-}
+}
diff --git a/library/cpp/getopt/small/last_getopt.h b/library/cpp/getopt/small/last_getopt.h
index 07687bc914..4377591fa8 100644
--- a/library/cpp/getopt/small/last_getopt.h
+++ b/library/cpp/getopt/small/last_getopt.h
@@ -1,21 +1,21 @@
#pragma once
-
+
#include "last_getopt_opts.h"
#include "last_getopt_easy_setup.h"
#include "last_getopt_parse_result.h"
#include <util/generic/function.h>
#include <util/string/split.h>
-
+
/// see some documentation in
/// https://wiki.yandex-team.ru/development/poisk/arcadia/util/lastgetopt/
/// https://wiki.yandex-team.ru/development/poisk/arcadia/library/getopt/
/// see examples in library/cpp/getopt/last_getopt_demo
-
+
//TODO: in most cases this include is unnecessary, but needed THandlerFunctor1<TpFunc, TpArg>::HandleOpt
#include "last_getopt_parser.h"
-namespace NLastGetopt {
+namespace NLastGetopt {
/// Handler to split option value by delimiter into a target container and allow ranges.
template <class Container>
struct TOptRangeSplitHandler: public IOptHandler {
@@ -35,21 +35,21 @@ namespace NLastGetopt {
if (curval.IsInited()) {
StringSplitter(curval).Split(ElementsDelim).Consume([&](const TStringBuf& val) {
TStringBuf mutableValue = val;
-
+
TValue first = NPrivate::OptFromString<TValue>(mutableValue.NextTok(RangesDelim), parser->CurOpt());
TValue last = mutableValue ? NPrivate::OptFromString<TValue>(mutableValue, parser->CurOpt()) : first;
-
+
if (last < first) {
throw TUsageException() << "failed to parse opt " << NPrivate::OptToString(parser->CurOpt()) << " value " << TString(val).Quote() << ": the second argument is less than the first one";
}
-
+
for (++last; first < last; ++first) {
Target->insert(Target->end(), first);
}
});
}
}
-
+
private:
TContainer* Target;
char ElementsDelim;
@@ -126,7 +126,7 @@ namespace NLastGetopt {
<< " value " << TString(curval).Quote() << ": " << CurrentExceptionMessage();
}
}
-
- }
+
+ }
}
diff --git a/library/cpp/getopt/small/last_getopt_support.h b/library/cpp/getopt/small/last_getopt_support.h
index 17bed3e614..3ea05733ff 100644
--- a/library/cpp/getopt/small/last_getopt_support.h
+++ b/library/cpp/getopt/small/last_getopt_support.h
@@ -1,36 +1,36 @@
#pragma once
-
+
#include <util/string/cast.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
-#include <util/generic/utility.h>
+#include <util/generic/utility.h>
#include <util/generic/yexception.h>
-
-namespace NLastGetopt {
+
+namespace NLastGetopt {
class TOpt;
class TOpts;
class TOptsParser;
class TOptsParseResult;
-
+
/// base of all getopt exceptions
class TException: public yexception {
};
-
+
/// TOpts configuration is incorrect
class TConfException: public TException {
};
-
+
/// User passed incorrect arguments, parsing failed
/// Note: use `throw TUsageException()` instead of `ythrow TUsageException()` to prevent appearence of stacktrace
/// and location of the `ythrow` statment in error messages.
class TUsageException: public TException {
};
-
+
struct IOptHandler {
virtual void HandleOpt(const TOptsParser* parser) = 0;
virtual ~IOptHandler() = default;
};
-
+
namespace NPrivate {
template <typename TpFunc>
class THandlerFunctor0
@@ -94,7 +94,7 @@ namespace NLastGetopt {
class TStoreResultFunctor {
private:
T* Target_;
-
+
public:
TStoreResultFunctor(T* target)
: Target_(target)
@@ -128,7 +128,7 @@ namespace NLastGetopt {
class TStoreValueFunctor {
T* Target;
const TpVal Value;
-
+
public:
template <typename TpArg>
TStoreValueFunctor(T* target, const TpArg& value)
@@ -155,7 +155,7 @@ namespace NLastGetopt {
inline TStringBuf OptFromStringImpl<TStringBuf>(const TStringBuf& value) {
return value;
}
-
+
template <>
inline const char* OptFromStringImpl<const char*>(const TStringBuf& value) {
return value.data();
diff --git a/library/cpp/getopt/small/opt.cpp b/library/cpp/getopt/small/opt.cpp
index 744501765c..2752987a55 100644
--- a/library/cpp/getopt/small/opt.cpp
+++ b/library/cpp/getopt/small/opt.cpp
@@ -1,105 +1,105 @@
#include "opt.h"
-
+
#include <util/system/progname.h>
-#include <ctype.h>
+#include <ctype.h>
-using namespace NLastGetopt;
+using namespace NLastGetopt;
-namespace {
+namespace {
struct TOptsNoDefault: public TOpts {
TOptsNoDefault(const TStringBuf& optstring = TStringBuf())
: TOpts(optstring)
{
}
- };
-
-}
-
-void Opt::Init(int argc, char* argv[], const char* optString, const Ion* longOptions, bool longOnly, bool isOpen) {
- Ions_ = longOptions;
- Err = true;
- GotError_ = false;
+ };
+
+}
+
+void Opt::Init(int argc, char* argv[], const char* optString, const Ion* longOptions, bool longOnly, bool isOpen) {
+ Ions_ = longOptions;
+ Err = true;
+ GotError_ = false;
Ind = argc;
-
- Opts_.Reset(new TOptsNoDefault(optString));
+
+ Opts_.Reset(new TOptsNoDefault(optString));
for (const Ion* o = longOptions; o != nullptr && o->name != nullptr; ++o) {
- TOpt* opt;
+ TOpt* opt;
if ((unsigned)o->val < 0x80 && isalnum(o->val)) {
- opt = &Opts_->CharOption(char(o->val));
+ opt = &Opts_->CharOption(char(o->val));
opt->AddLongName(o->name);
- } else {
- Opts_->AddLongOption(o->name);
- opt = const_cast<TOpt*>(&Opts_->GetLongOption(o->name));
- }
- opt->HasArg_ = EHasArg(o->has_arg);
+ } else {
+ Opts_->AddLongOption(o->name);
+ opt = const_cast<TOpt*>(&Opts_->GetLongOption(o->name));
+ }
+ opt->HasArg_ = EHasArg(o->has_arg);
opt->UserValue(o);
}
- Opts_->AllowSingleDashForLong_ = longOnly;
- Opts_->AllowPlusForLong_ = true;
- Opts_->AllowUnknownCharOptions_ = isOpen;
- Opts_->AllowUnknownLongOptions_ = false;
-
- OptsParser_.Reset(new TOptsParser(Opts_.Get(), argc, argv));
-}
+ Opts_->AllowSingleDashForLong_ = longOnly;
+ Opts_->AllowPlusForLong_ = true;
+ Opts_->AllowUnknownCharOptions_ = isOpen;
+ Opts_->AllowUnknownLongOptions_ = false;
-Opt::Opt(int argc, char* argv[], const char* optString, const Ion* longOptions, bool longOnly, bool isOpen) {
- Init(argc, argv, optString, longOptions, longOnly, isOpen);
+ OptsParser_.Reset(new TOptsParser(Opts_.Get(), argc, argv));
}
-Opt::Opt(int argc, const char* argv[], const char* optString, const Ion* longOptions, bool longOnly, bool isOpen) {
+Opt::Opt(int argc, char* argv[], const char* optString, const Ion* longOptions, bool longOnly, bool isOpen) {
+ Init(argc, argv, optString, longOptions, longOnly, isOpen);
+}
+
+Opt::Opt(int argc, const char* argv[], const char* optString, const Ion* longOptions, bool longOnly, bool isOpen) {
Init(argc, (char**)argv, optString, longOptions, longOnly, isOpen);
-}
-
-int Opt::Get() {
+}
+
+int Opt::Get() {
return Get(nullptr);
}
-int Opt::Get(int* longOptionIndex) {
- if (GotError_)
- return EOF;
+int Opt::Get(int* longOptionIndex) {
+ if (GotError_)
+ return EOF;
Arg = nullptr;
- try {
- bool r = OptsParser_->Next();
+ try {
+ bool r = OptsParser_->Next();
Ind = (int)OptsParser_->Pos_;
- if (!r) {
+ if (!r) {
return EOF;
- } else {
+ } else {
Arg = (char*)OptsParser_->CurVal();
if (!OptsParser_->CurOpt()) {
- // possible if RETURN_IN_ORDER
- return 1;
- } else {
+ // possible if RETURN_IN_ORDER
+ return 1;
+ } else {
const Ion* ion = (const Ion*)OptsParser_->CurOpt()->UserValue();
if (longOptionIndex) {
*longOptionIndex = int(ion - Ions_);
- }
+ }
char c = OptsParser_->CurOpt()->GetCharOr0();
- return c != 0 ? c : ion->val;
+ return c != 0 ? c : ion->val;
}
}
- } catch (const NLastGetopt::TException&) {
- GotError_ = true;
- if (Err)
- Cerr << CurrentExceptionMessage() << Endl;
- return '?';
+ } catch (const NLastGetopt::TException&) {
+ GotError_ = true;
+ if (Err)
+ Cerr << CurrentExceptionMessage() << Endl;
+ return '?';
}
}
void Opt::DummyHelp(IOutputStream& os) {
Opts_->PrintUsage(GetProgramName(), os);
-}
-
-int Opt::GetArgC() const {
+}
+
+int Opt::GetArgC() const {
return (int)OptsParser_->Argc_;
-}
-
-const char** Opt::GetArgV() const {
- return OptsParser_->Argv_;
-}
-
+}
+
+const char** Opt::GetArgV() const {
+ return OptsParser_->Argv_;
+}
+
int opt_get_number(int& argc, char* argv[]) {
int num = -1;
for (int a = 1; a < argc; a++) {
diff --git a/library/cpp/getopt/small/opt.h b/library/cpp/getopt/small/opt.h
index ecb57439bc..80701aa7e9 100644
--- a/library/cpp/getopt/small/opt.h
+++ b/library/cpp/getopt/small/opt.h
@@ -1,118 +1,118 @@
#pragma once
-
+
#include "last_getopt.h"
#include <util/generic/ptr.h>
-#include <util/generic/noncopyable.h>
-
-// implementation of Opt class using last getopt
-
-/*
- short-options syntax:
-
- opt-letter ::=
- [^: ]
-
- opt-string ::=
- '+'|'-'?({opt-letter}':'{0,2})*
-
- example: "AbCx:y:z::"
- {A,b,C} options without argument
- {x,y} options with argument
- {z} option with optional argument
-
- 1. shortopts begins with '-' :=> RETURN_IN_ORDER
- == non-option forces getopt to return 1 and to place non-option into optarg
-
- 2. shortopts begins with '+' :=> REQUIRE_ORDER
+#include <util/generic/noncopyable.h>
+
+// implementation of Opt class using last getopt
+
+/*
+ short-options syntax:
+
+ opt-letter ::=
+ [^: ]
+
+ opt-string ::=
+ '+'|'-'?({opt-letter}':'{0,2})*
+
+ example: "AbCx:y:z::"
+ {A,b,C} options without argument
+ {x,y} options with argument
+ {z} option with optional argument
+
+ 1. shortopts begins with '-' :=> RETURN_IN_ORDER
+ == non-option forces getopt to return 1 and to place non-option into optarg
+
+ 2. shortopts begins with '+' :=> REQUIRE_ORDER
GetEnv(_POSIX_OPTION_ORDER) :=> REQUIRE_ORDER
- == 1st non-option forces getopt to return EOF
-
- 3. default :=> PERMUTE
- == exchange options with non-options and place all options first
-
- 4. '--' command line argument forces getopt to stop parsing and to return EOF
- in any case
-
- long options should begin by '+' sign
- or when (_getopt_long_only = 1) by '-' sign
-
- struct option {
- char *name : option name
- int has_arg: 0 | 1 | 2 = without | with | optional argument
- int *flag : if (flag != 0) then getopt returns 0 and stores val into *flag
- int val : if (flag == 0) then getopt returns val
- }
-
- Example:
-
- struct option my_opts[] = {
- { "delete", 0, &deletion_flag, DEL }, -- returns 0, deletion_flag := DEL
- { "add", 1, NULL, 'a' }, -- returns 'a', argument in optarg
- { NULL }
- }
-*/
-
-#define OPT_RETURN_IN_ORDER "-"
+ == 1st non-option forces getopt to return EOF
+
+ 3. default :=> PERMUTE
+ == exchange options with non-options and place all options first
+
+ 4. '--' command line argument forces getopt to stop parsing and to return EOF
+ in any case
+
+ long options should begin by '+' sign
+ or when (_getopt_long_only = 1) by '-' sign
+
+ struct option {
+ char *name : option name
+ int has_arg: 0 | 1 | 2 = without | with | optional argument
+ int *flag : if (flag != 0) then getopt returns 0 and stores val into *flag
+ int val : if (flag == 0) then getopt returns val
+ }
+
+ Example:
+
+ struct option my_opts[] = {
+ { "delete", 0, &deletion_flag, DEL }, -- returns 0, deletion_flag := DEL
+ { "add", 1, NULL, 'a' }, -- returns 'a', argument in optarg
+ { NULL }
+ }
+*/
+
+#define OPT_RETURN_IN_ORDER "-"
#define OPT_REQUIRE_ORDER "+"
#define OPT_DONT_STORE_ARG ((void*)0)
-
+
class Opt : TNonCopyable {
-public:
+public:
enum HasArg { WithoutArg,
WithArg,
PossibleArg };
-
- struct Ion {
+
+ struct Ion {
const char* name;
HasArg has_arg;
int* flag;
int val;
- };
-
-private:
- THolder<NLastGetopt::TOpts> Opts_;
- THolder<NLastGetopt::TOptsParser> OptsParser_;
- const Ion* Ions_;
- bool GotError_;
+ };
+
+private:
+ THolder<NLastGetopt::TOpts> Opts_;
+ THolder<NLastGetopt::TOptsParser> OptsParser_;
+ const Ion* Ions_;
+ bool GotError_;
void Init(int argc, char* argv[], const char* optString, const Ion* longOptions = nullptr, bool longOnly = false, bool isOpen = false);
-
-public:
+
+public:
Opt(int argc, char* argv[], const char* optString, const Ion* longOptions = nullptr, bool longOnly = false, bool isOpen = false);
Opt(int argc, const char* argv[], const char* optString, const Ion* longOptions = nullptr, bool longOnly = false, bool isOpen = false);
-
- // Get() means next
- int Get();
- int Get(int* longOptionIndex);
+
+ // Get() means next
+ int Get();
+ int Get(int* longOptionIndex);
int operator()() {
- return Get();
- }
-
+ return Get();
+ }
+
const char* GetArg() const {
return Arg;
}
-
+
TVector<TString> GetFreeArgs() const {
return NLastGetopt::TOptsParseResult(&*Opts_, GetArgC(), GetArgV()).GetFreeArgs();
}
- // obsolete, use GetArg() instead
+ // obsolete, use GetArg() instead
char* Arg; /* option argument if any or NULL */
-
+
int Ind; /* command line index */
bool Err; /* flag to print error messages */
-
- int GetArgC() const;
- const char** GetArgV() const;
-
+
+ int GetArgC() const;
+ const char** GetArgV() const;
+
void DummyHelp(IOutputStream& os = Cerr);
-};
-
-// call before getopt. returns non-negative int, removing it from arguments (not found: -1)
-// Example: returns 11 for "progname -11abc", -1 for "progname -a11"
+};
+
+// call before getopt. returns non-negative int, removing it from arguments (not found: -1)
+// Example: returns 11 for "progname -11abc", -1 for "progname -a11"
int opt_get_number(int& argc, char* argv[]);
-
+
#define OPTION_HANDLING_PROLOG \
{ \
int optlet; \
@@ -128,12 +128,12 @@ int opt_get_number(int& argc, char* argv[]);
#define OPTION_HANDLE_END \
} \
break;
-
+
#define OPTION_HANDLE(opt, handle) \
OPTION_HANDLE_BEGIN(opt) \
handle; \
OPTION_HANDLE_END
-
+
#define OPTION_HANDLING_EPILOG \
default: \
ythrow yexception() << "unknown optlet"; \
diff --git a/library/cpp/getopt/small/opt2.h b/library/cpp/getopt/small/opt2.h
index 4d9d943237..c81cf345a4 100644
--- a/library/cpp/getopt/small/opt2.h
+++ b/library/cpp/getopt/small/opt2.h
@@ -111,11 +111,11 @@ public:
void AddError(const char* message = nullptr);
public:
- // non-option args
+ // non-option args
TVector<char*> Pos;
bool HasErrors;
-
-private:
+
+private:
bool BadPosCount;
char UnknownOption;
char* UnknownLongOption;
diff --git a/library/cpp/getopt/small/posix_getopt.cpp b/library/cpp/getopt/small/posix_getopt.cpp
index bd06f3499f..cae224133a 100644
--- a/library/cpp/getopt/small/posix_getopt.cpp
+++ b/library/cpp/getopt/small/posix_getopt.cpp
@@ -2,76 +2,76 @@
#include <util/generic/ptr.h>
-#include <ctype.h>
-
-namespace NLastGetopt {
- char* optarg;
- int optind;
- int optopt;
- int opterr;
- int optreset;
-
- static THolder<TOpts> Opts;
- static THolder<TOptsParser> OptsParser;
-
+#include <ctype.h>
+
+namespace NLastGetopt {
+ char* optarg;
+ int optind;
+ int optopt;
+ int opterr;
+ int optreset;
+
+ static THolder<TOpts> Opts;
+ static THolder<TOptsParser> OptsParser;
+
int getopt_long_impl(int argc, char* const* argv, const char* optstring,
const struct option* longopts, int* longindex, bool long_only) {
- if (!Opts || optreset == 1) {
+ if (!Opts || optreset == 1) {
optarg = nullptr;
- optind = 1;
- opterr = 1;
- optreset = 0;
- Opts.Reset(new TOpts(TOpts::Default(optstring)));
-
- Opts->AllowSingleDashForLong_ = long_only;
-
+ optind = 1;
+ opterr = 1;
+ optreset = 0;
+ Opts.Reset(new TOpts(TOpts::Default(optstring)));
+
+ Opts->AllowSingleDashForLong_ = long_only;
+
for (const struct option* o = longopts; o != nullptr && o->name != nullptr; ++o) {
- TOpt* opt;
+ TOpt* opt;
if ((unsigned)o->val < 0x80 && isalnum(o->val)) {
- opt = &Opts->CharOption(char(o->val));
+ opt = &Opts->CharOption(char(o->val));
opt->AddLongName(o->name);
- } else {
- Opts->AddLongOption(o->name);
- opt = const_cast<TOpt*>(&Opts->GetLongOption(o->name));
- }
- opt->HasArg_ = EHasArg(o->has_arg);
+ } else {
+ Opts->AddLongOption(o->name);
+ opt = const_cast<TOpt*>(&Opts->GetLongOption(o->name));
+ }
+ opt->HasArg_ = EHasArg(o->has_arg);
opt->UserValue(o->flag);
- }
-
+ }
+
OptsParser.Reset(new TOptsParser(&*Opts, argc, (const char**)argv));
- }
-
+ }
+
optarg = nullptr;
-
- try {
- if (!OptsParser->Next()) {
- return -1;
- } else {
+
+ try {
+ if (!OptsParser->Next()) {
+ return -1;
+ } else {
optarg = (char*)OptsParser->CurVal();
optind = (int)OptsParser->Pos_;
if (longindex && OptsParser->CurOpt())
*longindex = (int)Opts->IndexOf(OptsParser->CurOpt());
return OptsParser->CurOpt() ? OptsParser->CurOpt()->GetCharOr0() : 1;
- }
- } catch (const NLastGetopt::TException&) {
- return '?';
- }
- }
-
+ }
+ } catch (const NLastGetopt::TException&) {
+ return '?';
+ }
+ }
+
int getopt_long(int argc, char* const* argv, const char* optstring,
const struct option* longopts, int* longindex) {
- return getopt_long_impl(argc, argv, optstring, longopts, longindex, false);
- }
-
- int getopt_long_only(int argc, char* const* argv, const char* optstring,
+ return getopt_long_impl(argc, argv, optstring, longopts, longindex, false);
+ }
+
+ int getopt_long_only(int argc, char* const* argv, const char* optstring,
const struct option* longopts, int* longindex) {
- return getopt_long_impl(argc, argv, optstring, longopts, longindex, true);
- }
-
- // XXX: leading colon is not supported
- // XXX: updating optind by client is not supported
- int getopt(int argc, char* const* argv, const char* optstring) {
+ return getopt_long_impl(argc, argv, optstring, longopts, longindex, true);
+ }
+
+ // XXX: leading colon is not supported
+ // XXX: updating optind by client is not supported
+ int getopt(int argc, char* const* argv, const char* optstring) {
return getopt_long(argc, argv, optstring, nullptr, nullptr);
- }
-
-}
+ }
+
+}
diff --git a/library/cpp/getopt/small/posix_getopt.h b/library/cpp/getopt/small/posix_getopt.h
index e6af1e0284..d26fa96641 100644
--- a/library/cpp/getopt/small/posix_getopt.h
+++ b/library/cpp/getopt/small/posix_getopt.h
@@ -1,32 +1,32 @@
#pragma once
-
-// implementation of posix getopt using last getopt for demonstration purposes
-
-#include "last_getopt.h"
-
-namespace NLastGetopt {
- extern char* optarg;
- extern int optind;
- extern int optopt;
- extern int opterr;
- extern int optreset;
-
- enum {
- no_argument = NO_ARGUMENT,
- required_argument = REQUIRED_ARGUMENT,
- optional_argument = OPTIONAL_ARGUMENT,
- };
-
- struct option {
- const char* name;
- int has_arg;
- int* flag;
- int val;
- };
-
- int getopt(int argc, char* const* argv, const char* optstring);
+
+// implementation of posix getopt using last getopt for demonstration purposes
+
+#include "last_getopt.h"
+
+namespace NLastGetopt {
+ extern char* optarg;
+ extern int optind;
+ extern int optopt;
+ extern int opterr;
+ extern int optreset;
+
+ enum {
+ no_argument = NO_ARGUMENT,
+ required_argument = REQUIRED_ARGUMENT,
+ optional_argument = OPTIONAL_ARGUMENT,
+ };
+
+ struct option {
+ const char* name;
+ int has_arg;
+ int* flag;
+ int val;
+ };
+
+ int getopt(int argc, char* const* argv, const char* optstring);
int getopt_long(int argc, char* const* argv, const char* optstring,
const struct option* longopts, int* longindex);
- int getopt_long_only(int argc, char* const* argv, const char* optstring,
+ int getopt_long_only(int argc, char* const* argv, const char* optstring,
const struct option* longopts, int* longindex);
-}
+}