diff options
author | steplg <steplg@yandex-team.ru> | 2022-02-10 16:50:54 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:54 +0300 |
commit | d385578a0e719dc19758dbdef4ca862726ce22a7 (patch) | |
tree | 1f85cebcfcf4cccd1d54c4f39899285edd454262 /library/cpp | |
parent | 78c0028dbb79b03bcb0d700133245433b0dac3d1 (diff) | |
download | ydb-d385578a0e719dc19758dbdef4ca862726ce22a7.tar.gz |
Restoring authorship annotation for <steplg@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/getopt/small/last_getopt.h | 2 | ||||
-rw-r--r-- | library/cpp/getopt/small/modchooser.cpp | 128 | ||||
-rw-r--r-- | library/cpp/getopt/small/modchooser.h | 104 | ||||
-rw-r--r-- | library/cpp/getopt/ut/last_getopt_ut.cpp | 10 |
4 files changed, 122 insertions, 122 deletions
diff --git a/library/cpp/getopt/small/last_getopt.h b/library/cpp/getopt/small/last_getopt.h index 07687bc914..8ebb5e8d08 100644 --- a/library/cpp/getopt/small/last_getopt.h +++ b/library/cpp/getopt/small/last_getopt.h @@ -61,7 +61,7 @@ namespace NLastGetopt { public: using TContainer = Container; using TValue = typename TContainer::value_type; - + explicit TOptSplitHandler(TContainer* target, const char delim) : Target(target) , Delim(delim) diff --git a/library/cpp/getopt/small/modchooser.cpp b/library/cpp/getopt/small/modchooser.cpp index 2fa5cfd070..1cfa122778 100644 --- a/library/cpp/getopt/small/modchooser.cpp +++ b/library/cpp/getopt/small/modchooser.cpp @@ -2,73 +2,73 @@ #include "completer_command.h" #include "completion_generator.h" #include "last_getopt.h" -#include "modchooser.h" - +#include "modchooser.h" + #include <library/cpp/colorizer/colors.h> #include <util/stream/output.h> #include <util/stream/format.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/generic/ptr.h> #include <util/string/builder.h> #include <util/string/join.h> - + class PtrWrapper: public TMainClass { -public: - explicit PtrWrapper(const TMainFunctionPtr main) +public: + explicit PtrWrapper(const TMainFunctionPtr main) : Main(main) { } - + int operator()(const int argc, const char** argv) override { return Main(argc, argv); - } - -private: - TMainFunctionPtr Main; -}; - + } + +private: + TMainFunctionPtr Main; +}; + class PtrvWrapper: public TMainClass { -public: - explicit PtrvWrapper(const TMainFunctionPtrV main) +public: + explicit PtrvWrapper(const TMainFunctionPtrV main) : Main(main) { } - + int operator()(const int argc, const char** argv) override { TVector<TString> nargv(argv, argv + argc); return Main(nargv); - } - -private: - TMainFunctionPtrV Main; -}; - + } + +private: + TMainFunctionPtrV Main; +}; + class ClassWrapper: public TMainClass { -public: +public: explicit ClassWrapper(TMainClassV* main) : Main(main) { } - + int operator()(const int argc, const char** argv) override { TVector<TString> nargv(argv, argv + argc); return (*Main)(nargv); - } - -private: + } + +private: TMainClassV* Main; -}; - +}; + TModChooser::TMode::TMode(const TString& name, TMainClass* main, const TString& descr, bool hidden, bool noCompletion) : Name(name) , Main(main) , Description(descr) , Hidden(hidden) , NoCompletion(noCompletion) -{ +{ } - + TModChooser::TModChooser() : ModesHelpOption("-?") // Default help option in last_getopt , VersionHandler(nullptr) @@ -79,7 +79,7 @@ TModChooser::TModChooser() } TModChooser::~TModChooser() = default; - + void TModChooser::AddMode(const TString& mode, const TMainFunctionRawPtr func, const TString& description, bool hidden, bool noCompletion) { AddMode(mode, TMainFunctionPtr(func), description, hidden, noCompletion); } @@ -91,21 +91,21 @@ void TModChooser::AddMode(const TString& mode, const TMainFunctionRawPtrV func, void TModChooser::AddMode(const TString& mode, const TMainFunctionPtr func, const TString& description, bool hidden, bool noCompletion) { Wrappers.push_back(MakeHolder<PtrWrapper>(func)); AddMode(mode, Wrappers.back().Get(), description, hidden, noCompletion); -} - +} + void TModChooser::AddMode(const TString& mode, const TMainFunctionPtrV func, const TString& description, bool hidden, bool noCompletion) { Wrappers.push_back(MakeHolder<PtrvWrapper>(func)); AddMode(mode, Wrappers.back().Get(), description, hidden, noCompletion); -} - +} + void TModChooser::AddMode(const TString& mode, TMainClass* func, const TString& description, bool hidden, bool noCompletion) { if (Modes.FindPtr(mode)) { - ythrow yexception() << "TMode '" << mode << "' already exists in TModChooser."; - } - + ythrow yexception() << "TMode '" << mode << "' already exists in TModChooser."; + } + Modes[mode] = UnsortedModes.emplace_back(MakeHolder<TMode>(mode, func, description, hidden, noCompletion)).Get(); -} - +} + void TModChooser::AddMode(const TString& mode, TMainClassV* func, const TString& description, bool hidden, bool noCompletion) { Wrappers.push_back(MakeHolder<ClassWrapper>(func)); AddMode(mode, Wrappers.back().Get(), description, hidden, noCompletion); @@ -129,9 +129,9 @@ void TModChooser::AddAlias(const TString& alias, const TString& mode) { } void TModChooser::SetDescription(const TString& descr) { - Description = descr; -} - + Description = descr; +} + void TModChooser::SetModesHelpOption(const TString& helpOption) { ModesHelpOption = helpOption; } @@ -161,11 +161,11 @@ void TModChooser::AddCompletions(TString progName, const TString& name, bool hid CompletionsGenerator = NLastGetopt::MakeCompletionMod(this, std::move(progName), name); AddMode(name, CompletionsGenerator.Get(), "generate autocompletion files", hidden, noCompletion); } -} - +} + int TModChooser::Run(const int argc, const char** argv) const { Y_ENSURE(argc, "Can't run TModChooser with empty list of arguments."); - + bool shiftArgs = true; TString modeName; if (argc == 1) { @@ -178,12 +178,12 @@ int TModChooser::Run(const int argc, const char** argv) const { } } else { modeName = argv[1]; - } - - if (modeName == "-h" || modeName == "--help" || modeName == "-?") { + } + + if (modeName == "-h" || modeName == "--help" || modeName == "-?") { PrintHelp(argv[0]); - return 0; - } + return 0; + } if (VersionHandler && (modeName == "-v" || modeName == "--version")) { VersionHandler(); return 0; @@ -191,19 +191,19 @@ int TModChooser::Run(const int argc, const char** argv) const { if (!SvnRevisionOptionDisabled && modeName == "--svnrevision") { NLastGetopt::PrintVersionAndExit(nullptr); } - + auto modeIter = Modes.find(modeName); if (modeIter == Modes.end() && !DefaultMode.empty()) { modeIter = Modes.find(DefaultMode); shiftArgs = false; } - if (modeIter == Modes.end()) { + if (modeIter == Modes.end()) { Cerr << "Unknown mode " << modeName.Quote() << "." << Endl; PrintHelp(argv[0]); - return 1; - } - + return 1; + } + if (shiftArgs) { TString firstArg; TVector<const char*> nargv(Reserve(argc)); @@ -213,7 +213,7 @@ int TModChooser::Run(const int argc, const char** argv) const { } else { firstArg = argv[0] + TString(" ") + modeIter->second->Name; } - + nargv.push_back(firstArg.data()); for (int i = 2; i < argc; ++i) { @@ -226,9 +226,9 @@ int TModChooser::Run(const int argc, const char** argv) const { return (*modeIter->second->Main)(nargv.size() - 1, nargv.data()); } else { return (*modeIter->second->Main)(argc, argv); - } -} - + } +} + int TModChooser::Run(const TVector<TString>& argv) const { TVector<const char*> nargv(Reserve(argv.size() + 1)); for (auto& arg : argv) { @@ -288,7 +288,7 @@ void TModChooser::PrintHelp(const TString& progName) const { continue; // this is an alias maxModeLen = Max(maxModeLen, mode->CalculateFullNameLen()); } - + if (ShowSeparated) { for (const auto& unsortedMode : UnsortedModes) if (!unsortedMode->Hidden) { @@ -317,8 +317,8 @@ void TModChooser::PrintHelp(const TString& progName) const { if (!SvnRevisionOptionDisabled) { Cerr << "To print svn revision type --svnrevision" << Endl; } - return; -} + return; +} TVersionHandlerPtr TModChooser::GetVersionHandler() const { return VersionHandler; diff --git a/library/cpp/getopt/small/modchooser.h b/library/cpp/getopt/small/modchooser.h index 0a8de6d50b..8abdd531d0 100644 --- a/library/cpp/getopt/small/modchooser.h +++ b/library/cpp/getopt/small/modchooser.h @@ -1,59 +1,59 @@ -#pragma once - +#pragma once + #include "last_getopt_opts.h" #include <util/generic/map.h> #include <util/generic/string.h> -#include <util/generic/vector.h> - +#include <util/generic/vector.h> + #include <functional> -//! Mode function with vector of cli arguments. +//! Mode function with vector of cli arguments. using TMainFunctionPtrV = std::function<int(const TVector<TString>&)> ; using TMainFunctionRawPtrV = int (*)(const TVector<TString>& argv); - -//! Mode function with classic argc and argv arguments. + +//! Mode function with classic argc and argv arguments. using TMainFunctionPtr = std::function<int(int, const char**)> ; using TMainFunctionRawPtr = int (*)(const int argc, const char** argv); - -//! Mode class with vector of cli arguments. -class TMainClassV { -public: + +//! Mode class with vector of cli arguments. +class TMainClassV { +public: virtual int operator()(const TVector<TString>& argv) = 0; virtual ~TMainClassV() = default; -}; - -//! Mode class with classic argc and argv arguments. -class TMainClass { -public: +}; + +//! Mode class with classic argc and argv arguments. +class TMainClass { +public: virtual int operator()(int argc, const char** argv) = 0; virtual ~TMainClass() = default; -}; - +}; + //! Function to handle '--version' parameter typedef void (*TVersionHandlerPtr)(); -/*! Main class for handling different modes in single tool. +/*! Main class for handling different modes in single tool. * - * You can add modes for this class, use autogenerated help with - * list of modes and automaticly call necessary mode in run(). - * - * In first argv element mode get joined by space tool name and - * current mode name. - */ -class TModChooser { -public: + * You can add modes for this class, use autogenerated help with + * list of modes and automaticly call necessary mode in run(). + * + * In first argv element mode get joined by space tool name and + * current mode name. + */ +class TModChooser { +public: TModChooser(); - ~TModChooser(); + ~TModChooser(); -public: +public: void AddMode(const TString& mode, TMainFunctionRawPtr func, const TString& description, bool hidden = false, bool noCompletion = false); void AddMode(const TString& mode, TMainFunctionRawPtrV func, const TString& description, bool hidden = false, bool noCompletion = false); void AddMode(const TString& mode, TMainFunctionPtr func, const TString& description, bool hidden = false, bool noCompletion = false); void AddMode(const TString& mode, TMainFunctionPtrV func, const TString& description, bool hidden = false, bool noCompletion = false); void AddMode(const TString& mode, TMainClass* func, const TString& description, bool hidden = false, bool noCompletion = false); void AddMode(const TString& mode, TMainClassV* func, const TString& description, bool hidden = false, bool noCompletion = false); - + //! Hidden groups won't be displayed in 'help' block void AddGroupModeDescription(const TString& description, bool hidden = false, bool noCompletion = false); @@ -62,9 +62,9 @@ public: void AddAlias(const TString& alias, const TString& mode); - //! Set main program description. + //! Set main program description. void SetDescription(const TString& descr); - + //! Set modes help option name (-? is by default) void SetModesHelpOption(const TString& helpOption); @@ -84,32 +84,32 @@ public: void AddCompletions(TString progName, const TString& name = "completion", bool hidden = false, bool noCompletion = false); - /*! Run appropriate mode. - * + /*! Run appropriate mode. + * * In this method following things happen: - * 1) If first argument is -h/--help/-? then print short description of - * all modes and exit with zero code. + * 1) If first argument is -h/--help/-? then print short description of + * all modes and exit with zero code. * 2) If first argument is -v/--version and version handler is specified, * then call it and exit with zero code. * 3) Find mode with the same name as first argument. If it's found then * call it and return its return code. * 4) If appropriate mode is not found - return non-zero code. - */ + */ int Run(int argc, const char** argv) const; - - //! Run appropriate mode. Same as Run(const int, const char**) + + //! Run appropriate mode. Same as Run(const int, const char**) int Run(const TVector<TString>& argv) const; - + void PrintHelp(const TString& progName) const; - - struct TMode { + + struct TMode { TString Name; TMainClass* Main; TString Description; bool Hidden; bool NoCompletion; TVector<TString> Aliases; - + TMode() : Main(nullptr) { @@ -120,8 +120,8 @@ public: // Full name includes primary name and aliases. Also, will add ANSI colors. size_t CalculateFullNameLen() const; TString FormatFullName(size_t pad) const; - }; - + }; + TVector<const TMode*> GetUnsortedModes() const { auto ret = TVector<const TMode*>(Reserve(UnsortedModes.size())); for (auto& mode : UnsortedModes) { @@ -129,22 +129,22 @@ public: } return ret; } - + TVersionHandlerPtr GetVersionHandler() const; bool IsSvnRevisionOptionDisabled() const; -private: - //! Main program description. +private: + //! Main program description. TString Description; - + //! Help option for modes. TString ModesHelpOption; //! Wrappers around all modes. TVector<THolder<TMainClass>> Wrappers; - - //! Modes + + //! Modes TMap<TString, TMode*> Modes; TString DefaultMode; @@ -169,7 +169,7 @@ private: //! Mode that generates completions THolder<TMainClass> CompletionsGenerator; -}; +}; //! Mode class that allows introspecting its console arguments. class TMainClassArgs: public TMainClass { diff --git a/library/cpp/getopt/ut/last_getopt_ut.cpp b/library/cpp/getopt/ut/last_getopt_ut.cpp index c99a1d053d..2ecbbf2949 100644 --- a/library/cpp/getopt/ut/last_getopt_ut.cpp +++ b/library/cpp/getopt/ut/last_getopt_ut.cpp @@ -487,11 +487,11 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { opts.AddLongOption('s', "split").SplitHandler(&vals, ','); TOptsParseResultTestWrapper r(&opts, V({"prog", "--split=a,b,c"})); UNIT_ASSERT_EQUAL(vals.size(), 3); - UNIT_ASSERT_EQUAL(vals[0], "a"); - UNIT_ASSERT_EQUAL(vals[1], "b"); - UNIT_ASSERT_EQUAL(vals[2], "c"); - } - + UNIT_ASSERT_EQUAL(vals[0], "a"); + UNIT_ASSERT_EQUAL(vals[1], "b"); + UNIT_ASSERT_EQUAL(vals[2], "c"); + } + Y_UNIT_TEST(TestRangeSplitValue) { TOptsNoDefault opts; TVector<ui32> vals; |