diff options
author | Ilnur Khuziev <ilnur.khuziev@yandex.ru> | 2022-02-10 16:46:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:14 +0300 |
commit | 60040c91ffe701a84689b2c6310ff845e65cff42 (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /library/cpp/getopt | |
parent | 736dcd8ca259457a136f2f9f9168c44643914323 (diff) | |
download | ydb-60040c91ffe701a84689b2c6310ff845e65cff42.tar.gz |
Restoring authorship annotation for Ilnur Khuziev <ilnur.khuziev@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/getopt')
34 files changed, 563 insertions, 563 deletions
diff --git a/library/cpp/getopt/last_getopt.h b/library/cpp/getopt/last_getopt.h index 3a50270b44..d14f05cc5b 100644 --- a/library/cpp/getopt/last_getopt.h +++ b/library/cpp/getopt/last_getopt.h @@ -1,3 +1,3 @@ #pragma once -#include <library/cpp/getopt/small/last_getopt.h> +#include <library/cpp/getopt/small/last_getopt.h> diff --git a/library/cpp/getopt/last_getopt_demo/demo.cpp b/library/cpp/getopt/last_getopt_demo/demo.cpp index dfa98829a6..79426a9cc9 100644 --- a/library/cpp/getopt/last_getopt_demo/demo.cpp +++ b/library/cpp/getopt/last_getopt_demo/demo.cpp @@ -1,5 +1,5 @@ -#include <library/cpp/getopt/last_getopt.h> -#include <library/cpp/getopt/modchooser.h> +#include <library/cpp/getopt/last_getopt.h> +#include <library/cpp/getopt/modchooser.h> #include <library/cpp/colorizer/colors.h> // For the sake of this example, let's implement Wget @@ -158,7 +158,7 @@ protected: opts.AddSection( "Description", - "LastGetoptDemo is a showcase of library/cpp/getopt capabilities. It mimics interface of Wget " + "LastGetoptDemo is a showcase of library/cpp/getopt capabilities. It mimics interface of Wget " "but doesn't actually do anything." "\n\n" "GNU Wget, on the other hand, is a free utility for non-interactive download of files from the Web." diff --git a/library/cpp/getopt/last_getopt_demo/ya.make b/library/cpp/getopt/last_getopt_demo/ya.make index a0a6bb7f3e..53f1cfc122 100644 --- a/library/cpp/getopt/last_getopt_demo/ya.make +++ b/library/cpp/getopt/last_getopt_demo/ya.make @@ -3,7 +3,7 @@ PROGRAM(last_getopt_demo) OWNER(amatanhead) PEERDIR( - library/cpp/getopt + library/cpp/getopt ) SRCS( diff --git a/library/cpp/getopt/last_getopt_support.h b/library/cpp/getopt/last_getopt_support.h index 6a2fd0ba86..b71c7045b1 100644 --- a/library/cpp/getopt/last_getopt_support.h +++ b/library/cpp/getopt/last_getopt_support.h @@ -1,3 +1,3 @@ #pragma once -#include <library/cpp/getopt/small/last_getopt_support.h> +#include <library/cpp/getopt/small/last_getopt_support.h> diff --git a/library/cpp/getopt/modchooser.h b/library/cpp/getopt/modchooser.h index 956d4c7fd9..9bf73daf13 100644 --- a/library/cpp/getopt/modchooser.h +++ b/library/cpp/getopt/modchooser.h @@ -1,3 +1,3 @@ #pragma once -#include <library/cpp/getopt/small/modchooser.h> +#include <library/cpp/getopt/small/modchooser.h> diff --git a/library/cpp/getopt/opt.h b/library/cpp/getopt/opt.h index a0c6fdee64..d2a85075bd 100644 --- a/library/cpp/getopt/opt.h +++ b/library/cpp/getopt/opt.h @@ -1,3 +1,3 @@ #pragma once -#include <library/cpp/getopt/small/opt.h> +#include <library/cpp/getopt/small/opt.h> diff --git a/library/cpp/getopt/opt2.h b/library/cpp/getopt/opt2.h index 17373ce273..2d35bc7b29 100644 --- a/library/cpp/getopt/opt2.h +++ b/library/cpp/getopt/opt2.h @@ -1,3 +1,3 @@ #pragma once -#include <library/cpp/getopt/small/opt2.h> +#include <library/cpp/getopt/small/opt2.h> diff --git a/library/cpp/getopt/posix_getopt.h b/library/cpp/getopt/posix_getopt.h index 38e1650c89..8cb7ece624 100644 --- a/library/cpp/getopt/posix_getopt.h +++ b/library/cpp/getopt/posix_getopt.h @@ -1,3 +1,3 @@ #pragma once -#include <library/cpp/getopt/small/posix_getopt.h> +#include <library/cpp/getopt/small/posix_getopt.h> diff --git a/library/cpp/getopt/print.cpp b/library/cpp/getopt/print.cpp index 0aadd22eba..8cf1c62e4d 100644 --- a/library/cpp/getopt/print.cpp +++ b/library/cpp/getopt/print.cpp @@ -9,7 +9,7 @@ #include <library/cpp/svnversion/svnversion.h> #include <library/cpp/build_info/build_info.h> -namespace NLastGetoptPrivate { +namespace NLastGetoptPrivate { TString InitVersionString() { TString ts = GetProgramSvnVersion(); ts += "\n"; @@ -21,7 +21,7 @@ namespace NLastGetoptPrivate { } return ts; } - + TString InitShortVersionString() { TString ts = GetProgramShortVersionData(); return ts; @@ -29,12 +29,12 @@ namespace NLastGetoptPrivate { TString& VersionString(); TString& ShortVersionString(); - - struct TInit { + + struct TInit { TInit() { - VersionString() = InitVersionString(); + VersionString() = InitVersionString(); ShortVersionString() = InitShortVersionString(); - } - } Init; - + } + } Init; + } diff --git a/library/cpp/getopt/small/last_getopt.h b/library/cpp/getopt/small/last_getopt.h index e0479cc433..07687bc914 100644 --- a/library/cpp/getopt/small/last_getopt.h +++ b/library/cpp/getopt/small/last_getopt.h @@ -1,38 +1,38 @@ #pragma once -#include "last_getopt_opts.h" -#include "last_getopt_easy_setup.h" -#include "last_getopt_parse_result.h" - +#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/ +/// 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 +/// 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" -//TODO: in most cases this include is unnecessary, but needed THandlerFunctor1<TpFunc, TpArg>::HandleOpt -#include "last_getopt_parser.h" - namespace NLastGetopt { - /// Handler to split option value by delimiter into a target container and allow ranges. + /// Handler to split option value by delimiter into a target container and allow ranges. template <class Container> struct TOptRangeSplitHandler: public IOptHandler { - public: - using TContainer = Container; - using TValue = typename TContainer::value_type; + public: + using TContainer = Container; + using TValue = typename TContainer::value_type; - explicit TOptRangeSplitHandler(TContainer* target, const char elementsDelim, const char rangesDelim) - : Target(target) - , ElementsDelim(elementsDelim) + explicit TOptRangeSplitHandler(TContainer* target, const char elementsDelim, const char rangesDelim) + : Target(target) + , ElementsDelim(elementsDelim) , RangesDelim(rangesDelim) { } void HandleOpt(const TOptsParser* parser) override { - const TStringBuf curval(parser->CurValOrDef()); - if (curval.IsInited()) { + const TStringBuf curval(parser->CurValOrDef()); + if (curval.IsInited()) { StringSplitter(curval).Split(ElementsDelim).Consume([&](const TStringBuf& val) { TStringBuf mutableValue = val; @@ -47,39 +47,39 @@ namespace NLastGetopt { Target->insert(Target->end(), first); } }); - } + } } private: TContainer* Target; - char ElementsDelim; - char RangesDelim; + char ElementsDelim; + char RangesDelim; }; template <class Container> struct TOptSplitHandler: public IOptHandler { - public: - using TContainer = Container; - using TValue = typename TContainer::value_type; + public: + using TContainer = Container; + using TValue = typename TContainer::value_type; - explicit TOptSplitHandler(TContainer* target, const char delim) - : Target(target) + explicit TOptSplitHandler(TContainer* target, const char delim) + : Target(target) , Delim(delim) { } void HandleOpt(const TOptsParser* parser) override { - const TStringBuf curval(parser->CurValOrDef()); - if (curval.IsInited()) { + const TStringBuf curval(parser->CurValOrDef()); + if (curval.IsInited()) { StringSplitter(curval).Split(Delim).Consume([&](const TStringBuf& val) { Target->insert(Target->end(), NPrivate::OptFromString<TValue>(val, parser->CurOpt())); }); - } + } } - private: - TContainer* Target; - char Delim; + private: + TContainer* Target; + char Delim; }; template <class TpFunc> @@ -129,4 +129,4 @@ namespace NLastGetopt { } -} +} diff --git a/library/cpp/getopt/small/last_getopt_easy_setup.cpp b/library/cpp/getopt/small/last_getopt_easy_setup.cpp index dea45dbb83..c87dedf95e 100644 --- a/library/cpp/getopt/small/last_getopt_easy_setup.cpp +++ b/library/cpp/getopt/small/last_getopt_easy_setup.cpp @@ -1,12 +1,12 @@ -#include "last_getopt_easy_setup.h" - -namespace NLastGetopt { +#include "last_getopt_easy_setup.h" + +namespace NLastGetopt { TEasySetup::TEasySetup(const TStringBuf& optstring) : TOpts(optstring) { AddHelpOption(); } - + TOpt& TEasySetup::AdjustParam(const char* longName, const char* help, const char* argName, bool required) { Y_ASSERT(longName); TOpt& o = AddLongOption(longName); @@ -23,25 +23,25 @@ namespace NLastGetopt { } return o; } - + TEasySetup& TEasySetup::operator()(char shortName, const char* longName, const char* help, bool required) { AdjustParam(longName, help, nullptr, required).AddShortName(shortName); return *this; - } + } TEasySetup& TEasySetup::operator()(char shortName, const char* longName, const char* argName, const char* help, bool required) { AdjustParam(longName, help, argName, required).AddShortName(shortName); return *this; - } + } TEasySetup& TEasySetup::operator()(const char* longName, const char* help, bool required) { AdjustParam(longName, help, nullptr, required); return *this; - } - + } + TEasySetup& TEasySetup::operator()(const char* longName, const char* argName, const char* help, bool required) { AdjustParam(longName, help, argName, required); return *this; } - -} + +} diff --git a/library/cpp/getopt/small/last_getopt_easy_setup.h b/library/cpp/getopt/small/last_getopt_easy_setup.h index 8baa889a87..60dddda225 100644 --- a/library/cpp/getopt/small/last_getopt_easy_setup.h +++ b/library/cpp/getopt/small/last_getopt_easy_setup.h @@ -1,8 +1,8 @@ -#pragma once - -#include "last_getopt_opts.h" - -namespace NLastGetopt { +#pragma once + +#include "last_getopt_opts.h" + +namespace NLastGetopt { /** * Wrapper for TOpts class to make the life a bit easier. * Usual usage: @@ -21,25 +21,25 @@ namespace NLastGetopt { TEasySetup(const TStringBuf& optstring = TStringBuf()); TEasySetup& operator()(char shortName, const char* longName, const char* help, bool required = false); TEasySetup& operator()(char shortName, const char* longName, const char* argName, const char* help, bool required = false); - + template <class TpFunc> TEasySetup& operator()(char shortName, const char* longName, TpFunc handler, const char* help, bool required = false) { AdjustParam(longName, help, nullptr, handler, required).AddShortName(shortName); return *this; } - + TEasySetup& operator()(const char* longName, const char* help, bool required = false); TEasySetup& operator()(const char* longName, const char* argName, const char* help, bool required = false); - + template <class TpFunc> TEasySetup& operator()(const char* longName, TpFunc handler, const char* help, bool required = false) { AdjustParam(longName, help, nullptr, handler, required); return *this; } - + private: TOpt& AdjustParam(const char* longName, const char* help, const char* argName, bool required); - + template <class TpFunc> TOpt& AdjustParam(const char* longName, const char* help, const char* argName, TpFunc handler, bool required) { TOpt& o = AdjustParam(longName, help, argName, required); @@ -47,5 +47,5 @@ namespace NLastGetopt { return o; } }; - -} + +} diff --git a/library/cpp/getopt/small/last_getopt_handlers.h b/library/cpp/getopt/small/last_getopt_handlers.h index db6e21ded3..d35456ef34 100644 --- a/library/cpp/getopt/small/last_getopt_handlers.h +++ b/library/cpp/getopt/small/last_getopt_handlers.h @@ -1,19 +1,19 @@ -#pragma once - -#include "last_getopt_support.h" - -#include <util/string/split.h> +#pragma once + +#include "last_getopt_support.h" + +#include <util/string/split.h> #include <util/system/compiler.h> - -namespace NLastGetopt { - /// Handler to split option value by delimiter into a target container. + +namespace NLastGetopt { + /// Handler to split option value by delimiter into a target container. template <class Container> - struct TOptSplitHandler; - - /// Handler to split option value by delimiter into a target container and allow ranges. + struct TOptSplitHandler; + + /// Handler to split option value by delimiter into a target container and allow ranges. template <class Container> - struct TOptRangeSplitHandler; - + struct TOptRangeSplitHandler; + /// Handler to parse key-value pairs (default delimiter is '=') and apply user-supplied handler to each pair template <class TpFunc> struct TOptKVHandler; @@ -21,4 +21,4 @@ namespace NLastGetopt { [[noreturn]] void PrintUsageAndExit(const TOptsParser* parser); [[noreturn]] void PrintVersionAndExit(const TOptsParser* parser); [[noreturn]] void PrintShortVersionAndExit(const TString& appName); -} +} diff --git a/library/cpp/getopt/small/last_getopt_opt.cpp b/library/cpp/getopt/small/last_getopt_opt.cpp index 7f63053afe..9a99437f4b 100644 --- a/library/cpp/getopt/small/last_getopt_opt.cpp +++ b/library/cpp/getopt/small/last_getopt_opt.cpp @@ -1,16 +1,16 @@ -#include "last_getopt_opt.h" - -#include <util/stream/format.h> -#include <util/string/escape.h> -#include <util/generic/ylimits.h> -#include <util/generic/utility.h> -#include <util/generic/algorithm.h> -#include <ctype.h> - -namespace NLastGetopt { +#include "last_getopt_opt.h" + +#include <util/stream/format.h> +#include <util/string/escape.h> +#include <util/generic/ylimits.h> +#include <util/generic/utility.h> +#include <util/generic/algorithm.h> +#include <ctype.h> + +namespace NLastGetopt { static const TStringBuf ExcludedShortNameChars = "= -\t\n"; static const TStringBuf ExcludedLongNameChars = "= \t\n"; - + bool TOpt::NameIs(const TString& name) const { for (const auto& next : LongNames_) { if (next == name) @@ -18,7 +18,7 @@ namespace NLastGetopt { } return false; } - + bool TOpt::CharIs(char c) const { for (auto next : Chars_) { if (next == c) @@ -26,31 +26,31 @@ namespace NLastGetopt { } return false; } - + char TOpt::GetChar() const { ; if (Chars_.empty()) ythrow TConfException() << "no char for option " << this->ToShortString(); return Chars_.at(0); - } - + } + char TOpt::GetCharOr0() const { if (Chars_.empty()) return 0; return GetChar(); - } - + } + TString TOpt::GetName() const { ; if (LongNames_.empty()) ythrow TConfException() << "no name for option " << this->ToShortString(); return LongNames_.at(0); } - + bool TOpt::IsAllowedShortName(unsigned char c) { return isprint(c) && TStringBuf::npos == ExcludedShortNameChars.find(c); } - + TOpt& TOpt::AddShortName(unsigned char c) { ; if (!IsAllowedShortName(c)) @@ -58,7 +58,7 @@ namespace NLastGetopt { Chars_.push_back(c); return *this; } - + bool TOpt::IsAllowedLongName(const TString& name, unsigned char* out) { for (size_t i = 0; i != name.size(); ++i) { const unsigned char c = name[i]; @@ -67,10 +67,10 @@ namespace NLastGetopt { *out = c; return false; } - } + } return true; - } - + } + TOpt& TOpt::AddLongName(const TString& name) { ; unsigned char c = 0; @@ -80,13 +80,13 @@ namespace NLastGetopt { LongNames_.push_back(name); return *this; } - + namespace NPrivate { TString OptToString(char c); - + TString OptToString(const TString& longOption); } - + TString TOpt::ToShortString() const { ; if (!LongNames_.empty()) @@ -95,13 +95,13 @@ namespace NLastGetopt { return NPrivate::OptToString(Chars_.front()); return "?"; } - + void TOpt::FireHandlers(const TOptsParser* parser) const { for (const auto& handler : Handlers_) { handler->HandleOpt(parser); } - } - + } + TOpt& TOpt::IfPresentDisableCompletionFor(const TOpt& opt) { if (opt.GetLongNames()) { IfPresentDisableCompletionFor(opt.GetName()); @@ -110,4 +110,4 @@ namespace NLastGetopt { } return *this; } -} +} diff --git a/library/cpp/getopt/small/last_getopt_opt.h b/library/cpp/getopt/small/last_getopt_opt.h index 3a39357365..a8dd5adca9 100644 --- a/library/cpp/getopt/small/last_getopt_opt.h +++ b/library/cpp/getopt/small/last_getopt_opt.h @@ -1,25 +1,25 @@ -#pragma once - +#pragma once + #include "completer.h" -#include "last_getopt_handlers.h" - -#include <util/string/split.h> -#include <util/generic/ptr.h> +#include "last_getopt_handlers.h" + +#include <util/string/split.h> +#include <util/generic/ptr.h> #include <util/generic/string.h> -#include <util/generic/maybe.h> -#include <util/generic/vector.h> +#include <util/generic/maybe.h> +#include <util/generic/vector.h> #include <util/string/cast.h> - -#include <stdarg.h> - -namespace NLastGetopt { + +#include <stdarg.h> + +namespace NLastGetopt { enum EHasArg { NO_ARGUMENT, REQUIRED_ARGUMENT, OPTIONAL_ARGUMENT, DEFAULT_HAS_ARG = REQUIRED_ARGUMENT }; - + /** * NLastGetopt::TOpt is a storage of data about exactly one program option. * The data is: parse politics and help information. @@ -44,25 +44,25 @@ namespace NLastGetopt { public: typedef TVector<char> TShortNames; typedef TVector<TString> TLongNames; - + protected: TShortNames Chars_; TLongNames LongNames_; - + private: typedef TMaybe<TString> TdOptVal; typedef TVector<TSimpleSharedPtr<IOptHandler>> TOptHandlers; - + public: bool Hidden_ = false; // is visible in help TString ArgTitle_; // the name of argument in help output TString Help_; // the help string TString CompletionHelp_; // the help string that's used in completion script, a shorter version of Help_ TString CompletionArgHelp_; // the description of argument in completion script - + EHasArg HasArg_ = DEFAULT_HAS_ARG; // the argument parsing politics bool Required_ = false; // option existence politics - + bool AllowMultipleCompletion_ = false; // let the completer know that this option can occur more than once bool DisableCompletionForOptions_ = false; @@ -78,48 +78,48 @@ namespace NLastGetopt { TdOptVal OptionalValue_; TdOptVal DefaultValue_; TOptHandlers Handlers_; - + public: /** * Checks if given char can be a short name * @param c char to check */ static bool IsAllowedShortName(unsigned char c); - + /** * Checks if given string can be a long name * @param name string to check * @param c if given, the first bad charecter will be saved in c */ static bool IsAllowedLongName(const TString& name, unsigned char* c = nullptr); - + /** * @return one of the expected representations of the option. * If the option has short names, will return "-<char>" * Otherwise will return "--<long name>" */ TString ToShortString() const; - + /** * check if given string is one of the long names * * @param name string to check */ bool NameIs(const TString& name) const; - + /** * check if given char is one of the short names * * @param c char to check */ bool CharIs(char c) const; - + /** * If string has long names - will return one of them * Otherwise will throw */ TString GetName() const; - + /** * adds short alias for the option * @@ -128,14 +128,14 @@ namespace NLastGetopt { * @return self */ TOpt& AddShortName(unsigned char c); - + /** * return all short names of the option */ const TShortNames& GetShortNames() const { return Chars_; } - + /** * adds long alias for the option * @@ -144,31 +144,31 @@ namespace NLastGetopt { * @return self */ TOpt& AddLongName(const TString& name); - + /** * return all long names of the option */ const TLongNames& GetLongNames() const { return LongNames_; } - + /** * @return one of short names of the opt. If there is no short names exception is raised. */ char GetChar() const; - + /** * @return one of short names of the opt. If there is no short names '\0' returned. */ char GetCharOr0() const; - + /** * @returns argument parsing politics */ const EHasArg& GetHasArg() const { return HasArg_; } - + /** * sets argument parsing politics * @@ -181,14 +181,14 @@ namespace NLastGetopt { HasArg_ = hasArg; return *this; } - + /** * @returns argument title */ TString GetArgTitle() const { return ArgTitle_; } - + /** * sets argument parsing politics into REQUIRED_ARGUMENT * @@ -199,7 +199,7 @@ namespace NLastGetopt { ArgTitle_ = title; return HasArg(REQUIRED_ARGUMENT); } - + /** * sets argument parsing politics into NO_ARGUMENT * @@ -208,7 +208,7 @@ namespace NLastGetopt { TOpt& NoArgument() { return HasArg(NO_ARGUMENT); } - + /** * sets argument parsing politics into OPTIONAL_ARGUMENT * for details see NLastGetopt::TOpt @@ -220,7 +220,7 @@ namespace NLastGetopt { ArgTitle_ = title; return HasArg(OPTIONAL_ARGUMENT); } - + /** * sets argument parsing politics into OPTIONAL_ARGUMENT * sets the <optional value> into given @@ -235,14 +235,14 @@ namespace NLastGetopt { OptionalValue_ = val; return OptionalArgument(title); } - + /** * checks if "argument parsing politics" is OPTIONAL_ARGUMENT and the <optional value> is set. */ bool HasOptionalValue() const { return OPTIONAL_ARGUMENT == HasArg_ && OptionalValue_; } - + /** * @return optional value * throws exception if optional value wasn't set @@ -250,7 +250,7 @@ namespace NLastGetopt { const TString& GetOptionalValue() const { return *OptionalValue_; } - + /** * sets <default value> * @return self @@ -260,14 +260,14 @@ namespace NLastGetopt { DefaultValue_ = ToString(val); return *this; } - + /** * checks if default value is set. */ bool HasDefaultValue() const { return DefaultValue_.Defined(); } - + /** * @return default value * throws exception if <default value> wasn't set @@ -275,7 +275,7 @@ namespace NLastGetopt { const TString& GetDefaultValue() const { return *DefaultValue_; } - + /** * sets the option to be required * @return self @@ -284,7 +284,7 @@ namespace NLastGetopt { Required_ = true; return *this; } - + /** * sets the option to be optional * @return self @@ -293,14 +293,14 @@ namespace NLastGetopt { Required_ = false; return *this; } - + /** * @return true if the option is required */ bool IsRequired() const { return Required_; } - + /** * sets the option to be hidden (invisible in help) * @return self @@ -309,14 +309,14 @@ namespace NLastGetopt { Hidden_ = true; return *this; } - + /** * @return true if the option is hidden */ bool IsHidden() const { return Hidden_; } - + /** * sets the <user value> * @return self @@ -326,14 +326,14 @@ namespace NLastGetopt { UserValue_ = userval; return *this; } - + /** * @return user value */ const void* UserValue() const { return UserValue_; } - + /** * Set help string that appears with `--help`. Unless `CompletionHelp` is given, this message will also be used * in completion script. In this case, don't make it too long, don't start it with a capital letter and don't @@ -371,14 +371,14 @@ namespace NLastGetopt { Help_ = help; return *this; } - + /** * Get help string. */ const TString& GetHelp() const { return Help_; } - + /** * Set help string that appears when argument completer lists available options. * @@ -546,19 +546,19 @@ namespace NLastGetopt { * Run handlers for this option. */ void FireHandlers(const TOptsParser* parser) const; - + private: TOpt& HandlerImpl(IOptHandler* handler) { Handlers_.push_back(handler); return *this; } - + public: template <typename TpFunc> TOpt& Handler0(TpFunc func) { // functor taking no parameters return HandlerImpl(new NPrivate::THandlerFunctor0<TpFunc>(func)); } - + template <typename TpFunc> TOpt& Handler1(TpFunc func) { // functor taking one parameter return HandlerImpl(new NPrivate::THandlerFunctor1<TpFunc>(func)); @@ -575,35 +575,35 @@ namespace NLastGetopt { TOpt& Handler1T2(const TpArg2& def, TpFunc func) { return HandlerImpl(new NPrivate::THandlerFunctor1<TpFunc, TpArg>(func, def)); } - + TOpt& Handler(void (*f)()) { return Handler0(f); } TOpt& Handler(void (*f)(const TOptsParser*)) { return Handler1(f); } - + TOpt& Handler(TAutoPtr<IOptHandler> handler) { return HandlerImpl(handler.Release()); } - + template <typename T> // T extends IOptHandler TOpt& Handler(TAutoPtr<T> handler) { return HandlerImpl(handler.Release()); } - + // Stores FromString<T>(arg) in *target // T maybe anything with FromString<T>(const TStringBuf&) defined template <typename TpVal, typename T> TOpt& StoreResultT(T* target) { return Handler1T<TpVal>(NPrivate::TStoreResultFunctor<T, TpVal>(target)); } - + template <typename T> TOpt& StoreResult(T* target) { return StoreResultT<T>(target); } - + // Uses TMaybe<T> to store FromString<T>(arg) template <typename T> TOpt& StoreResult(TMaybe<T>* target) { @@ -614,12 +614,12 @@ namespace NLastGetopt { TOpt& StoreResultT(T* target, const TpDef& def) { return Handler1T<TpVal>(def, NPrivate::TStoreResultFunctor<T, TpVal>(target)); } - + template <typename T, typename TpDef> TOpt& StoreResult(T* target, const TpDef& def) { return StoreResultT<T>(target, def); } - + template <typename T> TOpt& StoreResultDef(T* target) { DefaultValue_ = ToString(*target); @@ -636,7 +636,7 @@ namespace NLastGetopt { TOpt& SetFlag(bool* target) { return DefaultValue("0").StoreResult(target, true); } - + // Similar to store_true in Python's argparse TOpt& StoreTrue(bool* target) { return NoArgument().SetFlag(target); @@ -646,12 +646,12 @@ namespace NLastGetopt { TOpt& StoreMappedResultT(T* target, const TpFunc& func) { return Handler1T<TpVal>(NPrivate::TStoreMappedResultFunctor<T, TpFunc, TpVal>(target, func)); } - + template <typename T, typename TpFunc> TOpt& StoreMappedResult(T* target, const TpFunc& func) { return StoreMappedResultT<T>(target, func); } - + // Stores given value in *target if the option is present. // TValue must be a copyable type, constructible from TParam. // T must be a copyable type, assignable from TValue. @@ -659,25 +659,25 @@ namespace NLastGetopt { TOpt& StoreValueT(T* target, const TParam& value) { return Handler1(NPrivate::TStoreValueFunctor<T, TValue>(target, value)); } - + // save value as target type template <typename T, typename TParam> TOpt& StoreValue(T* target, const TParam& value) { return StoreValueT<T>(target, value); } - + // save value as its original type (2nd template parameter) template <typename T, typename TValue> TOpt& StoreValue2(T* target, const TValue& value) { return StoreValueT<TValue>(target, value); } - + // Appends FromString<T>(arg) to *target for each argument template <typename T> TOpt& AppendTo(TVector<T>* target) { return Handler1T<T>([target](auto&& value) { target->push_back(std::move(value)); }); } - + // Appends FromString<T>(arg) to *target for each argument template <typename T> TOpt& InsertTo(THashSet<T>* target) { @@ -694,7 +694,7 @@ namespace NLastGetopt { TOpt& SplitHandler(Container* target, const char delim) { return Handler(new NLastGetopt::TOptSplitHandler<Container>(target, delim)); } - + template <class Container> TOpt& RangeSplitHandler(Container* target, const char elementsDelim, const char rangesDelim) { return Handler(new NLastGetopt::TOptRangeSplitHandler<Container>(target, elementsDelim, rangesDelim)); @@ -705,7 +705,7 @@ namespace NLastGetopt { return Handler(new NLastGetopt::TOptKVHandler<TpFunc>(func, kvdelim)); } }; - + /** * NLastGetopt::TFreeArgSpec is a storage of data about free argument. * The data is help information and (maybe) linked named argument. @@ -722,7 +722,7 @@ namespace NLastGetopt { , Optional_(optional) { } - + TString Title_; TString Help_; TString CompletionArgHelp_; diff --git a/library/cpp/getopt/small/last_getopt_opts.cpp b/library/cpp/getopt/small/last_getopt_opts.cpp index 7122bba10a..03c432849f 100644 --- a/library/cpp/getopt/small/last_getopt_opts.cpp +++ b/library/cpp/getopt/small/last_getopt_opts.cpp @@ -1,36 +1,36 @@ #include "completer_command.h" -#include "last_getopt_opts.h" +#include "last_getopt_opts.h" #include "wrap.h" #include "last_getopt_parser.h" - + #include <library/cpp/colorizer/colors.h> -#include <util/stream/format.h> +#include <util/stream/format.h> #include <util/charset/utf8.h> -#include <stdlib.h> - -namespace NLastGetoptPrivate { +#include <stdlib.h> + +namespace NLastGetoptPrivate { TString& VersionString() { static TString data; - return data; - } + return data; + } TString& ShortVersionString() { static TString data; return data; } } - + namespace NLastGetopt { static const TStringBuf SPad = " "; - + void PrintVersionAndExit(const TOptsParser*) { - Cout << (NLastGetoptPrivate::VersionString() ? NLastGetoptPrivate::VersionString() : "program version: not linked with library/cpp/getopt") << Endl; + Cout << (NLastGetoptPrivate::VersionString() ? NLastGetoptPrivate::VersionString() : "program version: not linked with library/cpp/getopt") << Endl; exit(NLastGetoptPrivate::VersionString().empty()); } - + void PrintShortVersionAndExit(const TString& appName) { - Cout << appName << " version " << (NLastGetoptPrivate::ShortVersionString() ? NLastGetoptPrivate::ShortVersionString() : "not linked with library/cpp/getopt") << Endl; + Cout << appName << " version " << (NLastGetoptPrivate::ShortVersionString() ? NLastGetoptPrivate::ShortVersionString() : "not linked with library/cpp/getopt") << Endl; exit(NLastGetoptPrivate::ShortVersionString().empty()); } @@ -44,24 +44,24 @@ namespace NLastGetopt { } return str; } - + namespace NPrivate { TString OptToString(char c) { TStringStream ss; ss << "-" << c; return ss.Str(); } - + TString OptToString(const TString& longOption) { TStringStream ss; ss << "--" << longOption; return ss.Str(); } - + TString OptToString(const TOpt* opt) { return opt->ToShortString(); } - } + } TOpts::TOpts(const TStringBuf& optstring) : ArgPermutation_(DEFAULT_ARG_PERMUTATION) @@ -76,8 +76,8 @@ namespace NLastGetopt { AddCharOptions(optstring); } AddVersionOption(0); - } - + } + void TOpts::AddCharOptions(const TStringBuf& optstring) { size_t p = 0; if (optstring[p] == '+') { @@ -102,8 +102,8 @@ namespace NLastGetopt { } AddCharOption(c, ha); } - } - + } + const TOpt* TOpts::FindLongOption(const TStringBuf& name) const { for (const auto& Opt : Opts_) { const TOpt* opt = Opt.Get(); @@ -111,8 +111,8 @@ namespace NLastGetopt { return opt; } return nullptr; - } - + } + TOpt* TOpts::FindLongOption(const TStringBuf& name) { for (auto& Opt : Opts_) { TOpt* opt = Opt.Get(); @@ -120,8 +120,8 @@ namespace NLastGetopt { return opt; } return nullptr; - } - + } + const TOpt* TOpts::FindCharOption(char c) const { for (const auto& Opt : Opts_) { const TOpt* opt = Opt.Get(); @@ -129,45 +129,45 @@ namespace NLastGetopt { return opt; } return nullptr; - } - + } + TOpt* TOpts::FindCharOption(char c) { for (auto& Opt : Opts_) { TOpt* opt = Opt.Get(); if (IsIn(opt->GetShortNames(), c)) return opt; - } + } return nullptr; - } - + } + const TOpt& TOpts::GetCharOption(char c) const { const TOpt* option = FindCharOption(c); if (!option) ythrow TException() << "unknown char option '" << c << "'"; return *option; - } - + } + TOpt& TOpts::GetCharOption(char c) { TOpt* option = FindCharOption(c); if (!option) ythrow TException() << "unknown char option '" << c << "'"; return *option; - } - + } + const TOpt& TOpts::GetLongOption(const TStringBuf& name) const { const TOpt* option = FindLongOption(name); if (!option) ythrow TException() << "unknown option " << name; return *option; - } - + } + TOpt& TOpts::GetLongOption(const TStringBuf& name) { TOpt* option = FindLongOption(name); if (!option) ythrow TException() << "unknown option " << name; return *option; - } - + } + bool TOpts::HasAnyShortOption() const { for (const auto& Opt : Opts_) { const TOpt* opt = Opt.Get(); @@ -175,8 +175,8 @@ namespace NLastGetopt { return true; } return false; - } - + } + bool TOpts::HasAnyLongOption() const { for (const auto& Opt : Opts_) { TOpt* opt = Opt.Get(); @@ -184,8 +184,8 @@ namespace NLastGetopt { return true; } return false; - } - + } + void TOpts::Validate() const { for (TOptsVector::const_iterator i = Opts_.begin(); i != Opts_.end(); ++i) { TOpt* opt = i->Get(); @@ -198,7 +198,7 @@ namespace NLastGetopt { << NPrivate::OptToString(c) << " is defined more than once"; } - } + } const TOpt::TLongNames& longNames = opt->GetLongNames(); for (const auto& longName : longNames) { for (TOptsVector::const_iterator j = i + 1; j != Opts_.end(); ++j) { @@ -208,22 +208,22 @@ namespace NLastGetopt { << NPrivate::OptToString(longName) << " is defined more than once"; } - } - } + } + } if (FreeArgsMax_ < FreeArgsMin_) { ythrow TConfException() << "FreeArgsMax must be >= FreeArgsMin"; } if (!FreeArgSpecs_.empty() && FreeArgSpecs_.rbegin()->first >= FreeArgsMax_) { ythrow TConfException() << "Described args count is greater than FreeArgsMax. Either increase FreeArgsMax or remove unreachable descriptions"; } - } + } TOpt& TOpts::AddOption(const TOpt& option) { if (option.GetShortNames().empty() && option.GetLongNames().empty()) ythrow TConfException() << "bad option: no chars, no long names"; Opts_.push_back(new TOpt(option)); return *Opts_.back(); - } + } TOpt& TOpts::AddCompletionOption(TString command, TString longName) { if (TOpt* o = FindLongOption(longName)) { @@ -257,28 +257,28 @@ namespace NLastGetopt { if (it == Opts_.end()) ythrow TException() << "unknown option"; return it - Opts_.begin(); - } - + } + TStringBuf TOpts::GetFreeArgTitle(size_t pos) const { if (FreeArgSpecs_.contains(pos)) { return FreeArgSpecs_.at(pos).GetTitle(DefaultFreeArgTitle_); } return DefaultFreeArgTitle_; } - + void TOpts::SetFreeArgTitle(size_t pos, const TString& title, const TString& help, bool optional) { FreeArgSpecs_[pos] = TFreeArgSpec(title, help, optional); - } - + } + TFreeArgSpec& TOpts::GetFreeArgSpec(size_t pos) { return FreeArgSpecs_[pos]; - } - + } + static TString FormatOption(const TOpt* option, const NColorizer::TColors& colors) { TStringStream result; const TOpt::TShortNames& shorts = option->GetShortNames(); const TOpt::TLongNames& longs = option->GetLongNames(); - + const size_t nopts = shorts.size() + longs.size(); const bool multiple = 1 < nopts; if (multiple) @@ -286,7 +286,7 @@ namespace NLastGetopt { for (size_t i = 0; i < nopts; ++i) { if (multiple && 0 != i) result << '|'; - + if (i < shorts.size()) // short result << colors.GreenColor() << '-' << shorts[i] << colors.OldColor(); else @@ -294,11 +294,11 @@ namespace NLastGetopt { } if (multiple) result << '}'; - + static const TString metavarDef("VAL"); const TString& title = option->GetArgTitle(); const TString& metavar = title.empty() ? metavarDef : title; - + if (option->GetHasArg() == OPTIONAL_ARGUMENT) { result << " [" << metavar; if (option->HasOptionalValue()) @@ -306,12 +306,12 @@ namespace NLastGetopt { result << ']'; } else if (option->GetHasArg() == REQUIRED_ARGUMENT) result << ' ' << metavar; - else + else Y_ASSERT(option->GetHasArg() == NO_ARGUMENT); - + return result.Str(); - } - + } + void TOpts::PrintCmdLine(const TStringBuf& program, IOutputStream& os, const NColorizer::TColors& colors) const { os << colors.BoldColor() << "Usage" << colors.OldColor() << ": "; if (CustomUsage) { @@ -322,12 +322,12 @@ namespace NLastGetopt { if (CustomCmdLineDescr) { os << CustomCmdLineDescr << Endl; return; - } + } os << "[OPTIONS]"; - + ui32 numDescribedFlags = FreeArgSpecs_.empty() ? 0 : FreeArgSpecs_.rbegin()->first + 1; ui32 numArgsToShow = Max(FreeArgsMin_, FreeArgsMax_ == UNLIMITED_ARGS ? numDescribedFlags : FreeArgsMax_); - + for (ui32 i = 0, nonOptionalFlagsPrinted = 0; i < numArgsToShow; ++i) { bool isOptional = nonOptionalFlagsPrinted >= FreeArgsMin_ || FreeArgSpecs_.Value(i, TFreeArgSpec()).Optional_; @@ -350,26 +350,26 @@ namespace NLastGetopt { os << Endl; } - + void TOpts::PrintUsage(const TStringBuf& program, IOutputStream& osIn, const NColorizer::TColors& colors) const { TStringStream os; - + if (!Title.empty()) os << Title << "\n\n"; - + PrintCmdLine(program, os, colors); - + TVector<TString> leftColumn(Opts_.size()); TVector<size_t> leftColumnSizes(leftColumn.size()); const size_t kMaxLeftWidth = 25; size_t leftWidth = 0; size_t requiredOptionsCount = 0; NColorizer::TColors disabledColors(false); - - for (size_t i = 0; i < Opts_.size(); i++) { - const TOpt* opt = Opts_[i].Get(); - if (opt->IsHidden()) - continue; + + for (size_t i = 0; i < Opts_.size(); i++) { + const TOpt* opt = Opts_[i].Get(); + if (opt->IsHidden()) + continue; leftColumn[i] = FormatOption(opt, colors); size_t leftColumnSize = leftColumn[i].size(); if (colors.IsTTY()) { @@ -382,7 +382,7 @@ namespace NLastGetopt { if (opt->IsRequired()) requiredOptionsCount++; } - + const TString leftPadding(leftWidth, ' '); for (size_t sectionId = 0; sectionId <= 1; sectionId++) { @@ -400,7 +400,7 @@ namespace NLastGetopt { else os << Endl << colors.BoldColor() << "Optional parameters" << colors.OldColor() << ":" << Endl; // optional options would be a tautology } - + for (size_t i = 0; i < Opts_.size(); i++) { const TOpt* opt = Opts_[i].Get(); @@ -415,8 +415,8 @@ namespace NLastGetopt { os << SPad << leftColumn[i] << ' '; if (leftColumnSizes[i] < leftWidth) os << TStringBuf(leftPadding.data(), leftWidth - leftColumnSizes[i]); - } - + } + TStringBuf help = opt->GetHelp(); while (help && isspace(help.back())) { help.Chop(1); @@ -452,8 +452,8 @@ namespace NLastGetopt { if (helpHasParagraphs) { os << Endl; } - } - } + } + } PrintFreeArgsDesc(os, colors); @@ -464,8 +464,8 @@ namespace NLastGetopt { } osIn << os.Str(); - } - + } + void TOpts::PrintUsage(const TStringBuf& program, IOutputStream& os) const { PrintUsage(program, os, NColorizer::AutoColors(os)); } @@ -473,19 +473,19 @@ namespace NLastGetopt { void TOpts::PrintFreeArgsDesc(IOutputStream& os, const NColorizer::TColors& colors) const { if (0 == FreeArgsMax_) return; - + size_t leftFreeWidth = 0; for (size_t i = 0; i < FreeArgSpecs_.size(); ++i) { leftFreeWidth = Max(leftFreeWidth, GetFreeArgTitle(i).size()); } - + if (!TrailingArgSpec_.IsDefault()) { leftFreeWidth = Max(leftFreeWidth, TrailingArgSpec_.GetTitle(DefaultFreeArgTitle_).size()); } - + leftFreeWidth = Min(leftFreeWidth, size_t(30)); os << Endl << colors.BoldColor() << "Free args" << colors.OldColor() << ":"; - + os << " min: " << colors.GreenColor() << FreeArgsMin_ << colors.OldColor() << ","; os << " max: " << colors.GreenColor(); if (FreeArgsMax_ != UNLIMITED_ARGS) { @@ -494,20 +494,20 @@ namespace NLastGetopt { os << "unlimited"; } os << colors.OldColor() << Endl; - + const size_t limit = FreeArgSpecs_.empty() ? 0 : FreeArgSpecs_.rbegin()->first; for (size_t i = 0; i <= limit; ++i) { if (!FreeArgSpecs_.contains(i)) { continue; } - + if (auto help = FreeArgSpecs_.at(i).GetHelp()) { auto title = GetFreeArgTitle(i); os << SPad << colors.GreenColor() << RightPad(title, leftFreeWidth, ' ') << colors.OldColor() << SPad << help << Endl; } } - + if (FreeArgsMax_ == UNLIMITED_ARGS) { auto title = TrailingArgSpec_.GetTitle(DefaultFreeArgTitle_); if (auto help = TrailingArgSpec_.GetHelp()) { @@ -515,5 +515,5 @@ namespace NLastGetopt { << SPad << help << Endl; } } - } -} + } +} diff --git a/library/cpp/getopt/small/last_getopt_opts.h b/library/cpp/getopt/small/last_getopt_opts.h index be6978ed4d..825b99c871 100644 --- a/library/cpp/getopt/small/last_getopt_opts.h +++ b/library/cpp/getopt/small/last_getopt_opts.h @@ -1,19 +1,19 @@ -#pragma once - -#include "last_getopt_opt.h" - +#pragma once + +#include "last_getopt_opt.h" + #include <library/cpp/colorizer/fwd.h> -#include <util/generic/map.h> - -namespace NLastGetopt { +#include <util/generic/map.h> + +namespace NLastGetopt { enum EArgPermutation { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER, DEFAULT_ARG_PERMUTATION = PERMUTE }; - + /** * NLastGetopt::TOpts is a storage of program options' parse rules. * It contains information about all options, free args, some parsing options @@ -37,36 +37,36 @@ namespace NLastGetopt { class TOpts { friend class TOptsParseResult; friend class TOptsParser; - + public: static constexpr const ui32 UNLIMITED_ARGS = Max<ui32>(); typedef TVector<TSimpleSharedPtr<TOpt>> TOptsVector; TOptsVector Opts_; // infomation about named (short and long) options TVector<std::function<void(TStringBuf)>> ArgBindings_; - + EArgPermutation ArgPermutation_ = DEFAULT_ARG_PERMUTATION; // determines how to parse positions of named and free options. See information below. bool AllowSingleDashForLong_ = false; // bool AllowPlusForLong_ = false; // using '+' instead '--' for long options - + //Allows unknwon options: bool AllowUnknownCharOptions_ = false; bool AllowUnknownLongOptions_ = false; - + ui32 Wrap_ = 80; private: ui32 FreeArgsMin_; // minimal number of free args ui32 FreeArgsMax_; // maximal number of free args - + TMap<ui32, TFreeArgSpec> FreeArgSpecs_; // mapping [free arg position] -> [free arg specification] TFreeArgSpec TrailingArgSpec_; // spec for the trailing argument (when arguments are unlimited) TString DefaultFreeArgTitle_ = "ARG"; // title that's used for free args without a title - + TString Title; // title of the help string TString CustomCmdLineDescr; // user defined help string TString CustomUsage; // user defined usage string - + TVector<std::pair<TString, TString>> Sections; // additional help entries to print after usage public: @@ -74,7 +74,7 @@ namespace NLastGetopt { * Constructs TOpts from string as in getopt(3) */ TOpts(const TStringBuf& optstring = TStringBuf()); - + /** * Constructs TOpts from string as in getopt(3) and * additionally adds help option (for '?') and svn-verstion option (for 'V') @@ -85,7 +85,7 @@ namespace NLastGetopt { opts.AddVersionOption(); return opts; } - + /** * Checks correctness of options' descriptions. * Throws TConfException if validation failed. @@ -94,35 +94,35 @@ namespace NLastGetopt { * -compability of settings, that responsable for freeArgs parsing */ void Validate() const; - + /** * Search for the option with given long name * @param name long name for search * @return ptr on result (nullptr if not found) */ const TOpt* FindLongOption(const TStringBuf& name) const; - + /** * Search for the option with given short name * @param c short name for search * @return ptr on result (nullptr if not found) */ const TOpt* FindCharOption(char c) const; - + /** * Search for the option with given long name * @param name long name for search * @return ptr on result (nullptr if not found) */ TOpt* FindLongOption(const TStringBuf& name); - + /** * Search for the option with given short name * @param c short name for search * @return ptr on result (nullptr if not found) */ TOpt* FindCharOption(char c); - + /** * Search for the option with given name * @param name name for search @@ -155,7 +155,7 @@ namespace NLastGetopt { void SetTitle(const TString& title) { Title = title; } - + /** * @return true if there is an option with given long name * @@ -164,7 +164,7 @@ namespace NLastGetopt { bool HasLongOption(const TString& name) const { return FindLongOption(name) != nullptr; } - + /** * @return true if there is an option with given short name * @@ -173,35 +173,35 @@ namespace NLastGetopt { bool HasCharOption(char c) const { return FindCharOption(c) != nullptr; } - + /** * Search for the option with given long name * @param name long name for search * @return ref on result (throw exception if not found) */ const TOpt& GetLongOption(const TStringBuf& name) const; - + /** * Search for the option with given long name * @param name long name for search * @return ref on result (throw exception if not found) */ TOpt& GetLongOption(const TStringBuf& name); - + /** * Search for the option with given short name * @param c short name for search * @return ref on result (throw exception if not found) */ const TOpt& GetCharOption(char c) const; - + /** * Search for the option with given short name * @param c short name for search * @return ref on result (throw exception if not found) */ TOpt& GetCharOption(char c); - + /** * Search for the option with given name * @param name name for search @@ -231,19 +231,19 @@ namespace NLastGetopt { * @return true if short options exist */ bool HasAnyShortOption() const; - + /** * @return true if long options exist */ bool HasAnyLongOption() const; - + /** * Creates new [option description (TOpt)] as a copy of given one * @param option source * @return reference for created option */ TOpt& AddOption(const TOpt& option); - + /** * Creates new free argument handling * @param name name of free arg to show in help @@ -266,7 +266,7 @@ namespace NLastGetopt { * @param optstring source */ void AddCharOptions(const TStringBuf& optstring); - + /** * Creates new [option description (TOpt)] with given short name and given help string * @@ -277,7 +277,7 @@ namespace NLastGetopt { TOpt& AddCharOption(char c, const TString& help = "") { return AddCharOption(c, DEFAULT_HAS_ARG, help); } - + /** * Creates new [option description (TOpt)] with given short name and given help string * @@ -292,7 +292,7 @@ namespace NLastGetopt { option.HasArg(hasArg); return AddOption(option); } - + /** * Creates new [option description (TOpt)] with given long name and given help string * @@ -303,7 +303,7 @@ namespace NLastGetopt { TOpt& AddLongOption(const TString& name, const TString& help = "") { return AddLongOption(0, name, help); } - + /** * Creates new [option description (TOpt)] with given long and short names and given help string * @@ -320,7 +320,7 @@ namespace NLastGetopt { option.Help(help); return AddOption(option); } - + /** * Creates new [option description (TOpt)] for help printing, * adds appropriate handler for it @@ -338,8 +338,8 @@ namespace NLastGetopt { .HasArg(NO_ARGUMENT) .IfPresentDisableCompletion() .Handler(&PrintUsageAndExit); - } - + } + /** * Creates new [option description (TOpt)] for svn-revision printing, * adds appropriate handler for it. @@ -357,8 +357,8 @@ namespace NLastGetopt { .HasArg(NO_ARGUMENT) .IfPresentDisableCompletion() .Handler(&PrintVersionAndExit); - } - + } + /** * Creates new option for generating completion shell scripts. * @@ -379,8 +379,8 @@ namespace NLastGetopt { AddCharOption(c); return const_cast<TOpt&>(GetCharOption(c)); } - } - + } + /** * Indicate that some options can't appear together. * @@ -405,7 +405,7 @@ namespace NLastGetopt { * @param opt pointer of option to search */ size_t IndexOf(const TOpt* opt) const; - + /** * Replace help string with given * @@ -414,7 +414,7 @@ namespace NLastGetopt { void SetCmdLineDescr(const TString& descr) { CustomCmdLineDescr = descr; } - + /** * Replace usage string with given * @@ -448,7 +448,7 @@ namespace NLastGetopt { void SetFreeArgsMin(size_t min) { FreeArgsMin_ = ui32(min); } - + /** * Get current minimal number of free args @@ -466,7 +466,7 @@ namespace NLastGetopt { FreeArgsMax_ = ui32(max); FreeArgsMax_ = Max<ui32>(FreeArgsMax_, ArgBindings_.size()); } - + /** * Get current maximal number of free args */ @@ -490,7 +490,7 @@ namespace NLastGetopt { FreeArgsMin_ = ui32(count); FreeArgsMax_ = ui32(count); } - + /** * Set minimal and maximal number of free args * @@ -501,7 +501,7 @@ namespace NLastGetopt { FreeArgsMin_ = ui32(min); FreeArgsMax_ = ui32(max); } - + /** * Set title and help string of free argument * @@ -512,7 +512,7 @@ namespace NLastGetopt { * does not affect actual flags parsing */ void SetFreeArgTitle(size_t pos, const TString& title, const TString& help = TString(), bool optional = false); - + /** * Get free argument's spec for further modification. */ @@ -555,7 +555,7 @@ namespace NLastGetopt { TrailingArgSpec_.Help(std::move(help)); } /// @} - + /** * Get spec for the trailing argument. * @@ -578,7 +578,7 @@ namespace NLastGetopt { void SetAllowSingleDashForLong(bool value) { AllowSingleDashForLong_ = value; } - + /** * Wrap help text at this number of characters. 0 to disable wrapping. */ @@ -613,7 +613,7 @@ namespace NLastGetopt { } return ret; } - + private: /** * @return argument title of a free argument @@ -621,7 +621,7 @@ namespace NLastGetopt { * @param pos position of the argument */ TStringBuf GetFreeArgTitle(size_t pos) const; - + /** * Print usage helper * @@ -630,7 +630,7 @@ namespace NLastGetopt { * @param colors colorizer */ void PrintCmdLine(const TStringBuf& program, IOutputStream& os, const NColorizer::TColors& colors) const; - + /** * Print usage helper * @@ -639,5 +639,5 @@ namespace NLastGetopt { */ void PrintFreeArgsDesc(IOutputStream& os, const NColorizer::TColors& colors) const; }; - + } diff --git a/library/cpp/getopt/small/last_getopt_parse_result.cpp b/library/cpp/getopt/small/last_getopt_parse_result.cpp index 423e035bc5..f4b5607a90 100644 --- a/library/cpp/getopt/small/last_getopt_parse_result.cpp +++ b/library/cpp/getopt/small/last_getopt_parse_result.cpp @@ -1,6 +1,6 @@ -#include "last_getopt_parse_result.h" - -namespace NLastGetopt { +#include "last_getopt_parse_result.h" + +namespace NLastGetopt { const TOptParseResult* TOptsParseResult::FindParseResult(const TdVec& vec, const TOpt* opt) { for (const auto& r : vec) { if (r.OptPtr() == opt) @@ -8,35 +8,35 @@ namespace NLastGetopt { } return nullptr; } - + const TOptParseResult* TOptsParseResult::FindOptParseResult(const TOpt* opt, bool includeDefault) const { const TOptParseResult* r = FindParseResult(Opts_, opt); if (nullptr == r && includeDefault) r = FindParseResult(OptsDef_, opt); return r; - } - + } + const TOptParseResult* TOptsParseResult::FindLongOptParseResult(const TString& name, bool includeDefault) const { return FindOptParseResult(&Parser_->Opts_->GetLongOption(name), includeDefault); } - + const TOptParseResult* TOptsParseResult::FindCharOptParseResult(char c, bool includeDefault) const { return FindOptParseResult(&Parser_->Opts_->GetCharOption(c), includeDefault); } - + bool TOptsParseResult::Has(const TOpt* opt, bool includeDefault) const { Y_ASSERT(opt); return FindOptParseResult(opt, includeDefault) != nullptr; } - + bool TOptsParseResult::Has(const TString& name, bool includeDefault) const { return FindLongOptParseResult(name, includeDefault) != nullptr; } - + bool TOptsParseResult::Has(char c, bool includeDefault) const { return FindCharOptParseResult(c, includeDefault) != nullptr; } - + const char* TOptsParseResult::Get(const TOpt* opt, bool includeDefault) const { Y_ASSERT(opt); const TOptParseResult* r = FindOptParseResult(opt, includeDefault); @@ -52,7 +52,7 @@ namespace NLastGetopt { return r->Back(); } } - + const char* TOptsParseResult::GetOrElse(const TOpt* opt, const char* defaultValue) const { Y_ASSERT(opt); const TOptParseResult* r = FindOptParseResult(opt); @@ -60,29 +60,29 @@ namespace NLastGetopt { return defaultValue; } else { return r->Back(); - } - } - + } + } + const char* TOptsParseResult::Get(const TString& name, bool includeDefault) const { return Get(&Parser_->Opts_->GetLongOption(name), includeDefault); - } - + } + const char* TOptsParseResult::Get(char c, bool includeDefault) const { return Get(&Parser_->Opts_->GetCharOption(c), includeDefault); } - + const char* TOptsParseResult::GetOrElse(const TString& name, const char* defaultValue) const { if (!Has(name)) return defaultValue; return Get(name); } - + const char* TOptsParseResult::GetOrElse(char c, const char* defaultValue) const { if (!Has(c)) return defaultValue; return Get(c); } - + TOptParseResult& TOptsParseResult::OptParseResult() { const TOpt* opt = Parser_->CurOpt(); Y_ASSERT(opt); @@ -94,11 +94,11 @@ namespace NLastGetopt { opts.push_back(TOptParseResult(opt)); return opts.back(); } - + TString TOptsParseResult::GetProgramName() const { return Parser_->ProgramName_; } - + void TOptsParseResult::PrintUsage(IOutputStream& os) const { Parser_->Opts_->PrintUsage(Parser_->ProgramName_, os); } @@ -106,7 +106,7 @@ namespace NLastGetopt { size_t TOptsParseResult::GetFreeArgsPos() const { return Parser_->Pos_; } - + TVector<TString> TOptsParseResult::GetFreeArgs() const { TVector<TString> v; for (size_t i = GetFreeArgsPos(); i < Parser_->Argc_; ++i) { @@ -114,11 +114,11 @@ namespace NLastGetopt { } return v; } - + size_t TOptsParseResult::GetFreeArgCount() const { return Parser_->Argc_ - GetFreeArgsPos(); - } - + } + void TOptsParseResult::Init(const TOpts* options, int argc, const char** argv) { try { Parser_.Reset(new TOptsParser(options, argc, argv)); @@ -138,9 +138,9 @@ namespace NLastGetopt { } } catch (...) { HandleError(); - } - } - + } + } + void TOptsParseResult::HandleError() const { Cerr << CurrentExceptionMessage() << Endl; if (Parser_.Get()) { // parser initializing can fail (and we get here, see Init) @@ -149,12 +149,12 @@ namespace NLastGetopt { } else { PrintUsage(); } - } + } exit(1); - } - + } + void TOptsParseResultException::HandleError() const { throw; } - -} + +} diff --git a/library/cpp/getopt/small/last_getopt_parse_result.h b/library/cpp/getopt/small/last_getopt_parse_result.h index e55a1a1eec..1ab6f598c9 100644 --- a/library/cpp/getopt/small/last_getopt_parse_result.h +++ b/library/cpp/getopt/small/last_getopt_parse_result.h @@ -1,9 +1,9 @@ -#pragma once - -#include "last_getopt_opts.h" +#pragma once + +#include "last_getopt_opts.h" #include "last_getopt_parser.h" - -namespace NLastGetopt { + +namespace NLastGetopt { /** * NLastGetopt::TOptParseResult contains all arguments for exactly one TOpt, * that have been fetched during parsing @@ -13,13 +13,13 @@ namespace NLastGetopt { class TOptParseResult { public: typedef TVector<const char*> TValues; - + public: TOptParseResult(const TOpt* opt = nullptr) : Opt_(opt) { } - + public: const TOpt& Opt() const { return *Opt_; @@ -49,7 +49,7 @@ namespace NLastGetopt { const char* Back(const char* def = nullptr) const { return Empty() ? DefVal(def) : Values().back(); } - + private: const TOpt* Opt_; TValues Values_; @@ -67,16 +67,16 @@ namespace NLastGetopt { class TOptsParseResult { private: THolder<TOptsParser> Parser_; //The instance of parser. - + // XXX: make argc, argv typedef TVector<TOptParseResult> TdVec; - + TdVec Opts_; //Parsing result for all options, that have been explicitly defined in argc/argv TdVec OptsDef_; //Parsing result for options, that have been defined by default values only - + private: TOptParseResult& OptParseResult(); - + /** * Searchs for object in given container * @@ -86,15 +86,15 @@ namespace NLastGetopt { * @retunr ptr on corresponding TOptParseResult */ static const TOptParseResult* FindParseResult(const TdVec& vec, const TOpt* opt); - + protected: /** * Performs parsing of comand line arguments. */ void Init(const TOpts* options, int argc, const char** argv); - + TOptsParseResult() = default; - + public: /** * The action in case of parser failure. @@ -102,7 +102,7 @@ namespace NLastGetopt { * By default prints error string and aborts the program */ virtual void HandleError() const; - + /** * Constructs object by parsing arguments with given rules * @@ -113,7 +113,7 @@ namespace NLastGetopt { TOptsParseResult(const TOpts* options, int argc, const char* argv[]) { Init(options, argc, argv); } - + /** * Constructs object by parsing arguments with given rules * @@ -124,9 +124,9 @@ namespace NLastGetopt { TOptsParseResult(const TOpts* options, int argc, char* argv[]) { Init(options, argc, const_cast<const char**>(argv)); } - + virtual ~TOptsParseResult() = default; - + /** * Search for TOptParseResult that corresponds to given option (TOpt) * @@ -136,7 +136,7 @@ namespace NLastGetopt { * @return ptr on result */ const TOptParseResult* FindOptParseResult(const TOpt* opt, bool includeDefault = false) const; - + /** * Search for TOptParseResult that corresponds to given long name * @@ -146,7 +146,7 @@ namespace NLastGetopt { * @return ptr on result */ const TOptParseResult* FindLongOptParseResult(const TString& name, bool includeDefault = false) const; - + /** * Search for TOptParseResult that corresponds to given short name * @@ -156,12 +156,12 @@ namespace NLastGetopt { * @return ptr on result */ const TOptParseResult* FindCharOptParseResult(char c, bool includeDefault = false) const; - + /** * @return argv[0] */ TString GetProgramName() const; - + /** * Print usage string. */ @@ -171,17 +171,17 @@ namespace NLastGetopt { * @return position in [premuted argv] of the first free argument */ size_t GetFreeArgsPos() const; - + /** * @return number of fetched free arguments */ size_t GetFreeArgCount() const; - + /** * @return all fetched free arguments */ TVector<TString> GetFreeArgs() const; - + /** * @return true if given option exist in results of parsing * @@ -190,7 +190,7 @@ namespace NLastGetopt { * */ bool Has(const TOpt* opt, bool includeDefault = false) const; - + /** * @return nil terminated string on the last fetched argument of givne option * @@ -198,7 +198,7 @@ namespace NLastGetopt { * @param includeDefault search in results obtained from default values */ const char* Get(const TOpt* opt, bool includeDefault = true) const; - + /** * @return nil terminated string on the last fetched argument of givne option * if option haven't been fetched, given defaultValue will be returned @@ -207,7 +207,7 @@ namespace NLastGetopt { * @param defaultValue */ const char* GetOrElse(const TOpt* opt, const char* defaultValue) const; - + /** * @return true if given option exist in results of parsing * @@ -216,7 +216,7 @@ namespace NLastGetopt { * */ bool Has(const TString& name, bool includeDefault = false) const; - + /** * @return nil terminated string on the last fetched argument of givne option * @@ -224,7 +224,7 @@ namespace NLastGetopt { * @param includeDefault search in results obtained from default values */ const char* Get(const TString& name, bool includeDefault = true) const; - + /** * @return nil terminated string on the last fetched argument of givne option * if option haven't been fetched, given defaultValue will be returned @@ -233,7 +233,7 @@ namespace NLastGetopt { * @param defaultValue */ const char* GetOrElse(const TString& name, const char* defaultValue) const; - + /** * @return true if given option exist in results of parsing * @@ -242,7 +242,7 @@ namespace NLastGetopt { * */ bool Has(char name, bool includeDefault = false) const; - + /** * @return nil terminated string on the last fetched argument of givne option * @@ -250,7 +250,7 @@ namespace NLastGetopt { * @param includeDefault search in results obtained from default values */ const char* Get(char name, bool includeDefault = true) const; - + /** * @return nil terminated string on the last fetched argument of givne option * if option haven't been fetched, given defaultValue will be returned @@ -259,7 +259,7 @@ namespace NLastGetopt { * @param defaultValue */ const char* GetOrElse(char name, const char* defaultValue) const; - + /** * for givne option return parsed value of the last fetched argument * if option haven't been fetched, HandleError action is called @@ -277,8 +277,8 @@ namespace NLastGetopt { HandleError(); throw; } - } - + } + /** * for givne option return parsed value of the last fetched argument * if option haven't been fetched, given defaultValue will be returned @@ -296,7 +296,7 @@ namespace NLastGetopt { return defaultValue; } }; - + /** * NLastGetopt::TOptsParseResultException contains result of parsing argc,argv be parser. * @@ -317,5 +317,5 @@ namespace NLastGetopt { protected: TOptsParseResultException() = default; }; - -} + +} diff --git a/library/cpp/getopt/small/last_getopt_parser.cpp b/library/cpp/getopt/small/last_getopt_parser.cpp index 7c474809ce..7668b12a03 100644 --- a/library/cpp/getopt/small/last_getopt_parser.cpp +++ b/library/cpp/getopt/small/last_getopt_parser.cpp @@ -1,23 +1,23 @@ -#include "last_getopt_parser.h" - +#include "last_getopt_parser.h" + #include <library/cpp/colorizer/colors.h> -#include <util/string/escape.h> - -namespace NLastGetopt { +#include <util/string/escape.h> + +namespace NLastGetopt { void TOptsParser::Init(const TOpts* opts, int argc, const char* argv[]) { opts->Validate(); - + Opts_ = opts; - + if (argc < 1) throw TUsageException() << "argv must have at least one argument"; - + Argc_ = argc; Argv_ = argv; - + ProgramName_ = argv[0]; - + Pos_ = 1; Sop_ = 0; CurrentOpt_ = nullptr; @@ -27,11 +27,11 @@ namespace NLastGetopt { OptsSeen_.clear(); OptsDefault_.clear(); } - + void TOptsParser::Init(const TOpts* opts, int argc, char* argv[]) { Init(opts, argc, const_cast<const char**>(argv)); } - + void TOptsParser::Swap(TOptsParser& that) { DoSwap(Opts_, that.Opts_); DoSwap(Argc_, that.Argc_); @@ -46,7 +46,7 @@ namespace NLastGetopt { DoSwap(GotMinusMinus_, that.GotMinusMinus_); DoSwap(OptsSeen_, that.OptsSeen_); } - + bool TOptsParser::Commit(const TOpt* currentOpt, const TStringBuf& currentValue, size_t pos, size_t sop) { Pos_ = pos; Sop_ = sop; @@ -56,62 +56,62 @@ namespace NLastGetopt { OptsSeen_.insert(currentOpt); return true; } - + bool TOptsParser::CommitEndOfOptions(size_t pos) { Pos_ = pos; Sop_ = 0; Y_ASSERT(!CurOpt()); Y_ASSERT(!CurVal()); - + Y_ASSERT(!Stopped_); - + if (Opts_->FreeArgsMin_ == Opts_->FreeArgsMax_ && Argc_ - Pos_ != Opts_->FreeArgsMin_) throw TUsageException() << "required exactly " << Opts_->FreeArgsMin_ << " free args"; else if (Argc_ - Pos_ < Opts_->FreeArgsMin_) throw TUsageException() << "required at least " << Opts_->FreeArgsMin_ << " free args"; else if (Argc_ - Pos_ > Opts_->FreeArgsMax_) throw TUsageException() << "required at most " << Opts_->FreeArgsMax_ << " free args"; - + return false; } - + bool TOptsParser::ParseUnknownShortOptWithinArg(size_t pos, size_t sop) { Y_ASSERT(pos < Argc_); const TStringBuf arg(Argv_[pos]); Y_ASSERT(sop > 0); Y_ASSERT(sop < arg.length()); Y_ASSERT(EIO_NONE != IsOpt(arg)); - + if (!Opts_->AllowUnknownCharOptions_) throw TUsageException() << "unknown option '" << EscapeC(arg[sop]) << "' in '" << arg << "'"; - + TempCurrentOpt_.Reset(new TOpt); TempCurrentOpt_->AddShortName(arg[sop]); - + sop += 1; - + // mimic behavior of Opt: unknown option has arg only if char is last within arg if (sop < arg.length()) { return Commit(TempCurrentOpt_.Get(), nullptr, pos, sop); } - + pos += 1; sop = 0; if (pos == Argc_ || EIO_NONE != IsOpt(Argv_[pos])) { return Commit(TempCurrentOpt_.Get(), nullptr, pos, 0); } - + return Commit(TempCurrentOpt_.Get(), Argv_[pos], pos + 1, 0); - } - + } + bool TOptsParser::ParseShortOptWithinArg(size_t pos, size_t sop) { Y_ASSERT(pos < Argc_); const TStringBuf arg(Argv_[pos]); Y_ASSERT(sop > 0); Y_ASSERT(sop < arg.length()); Y_ASSERT(EIO_NONE != IsOpt(arg)); - + size_t p = sop; char c = arg[p]; const TOpt* opt = Opts_->FindCharOption(c); @@ -126,15 +126,15 @@ namespace NLastGetopt { } return Commit(opt, arg.SubStr(p), pos + 1, 0); } - + bool TOptsParser::ParseShortOptArg(size_t pos) { Y_ASSERT(pos < Argc_); const TStringBuf arg(Argv_[pos]); Y_ASSERT(EIO_NONE != IsOpt(arg)); Y_ASSERT(!arg.StartsWith("--")); return ParseShortOptWithinArg(pos, 1); - } - + } + bool TOptsParser::ParseOptArg(size_t pos) { Y_ASSERT(pos < Argc_); TStringBuf arg(Argv_[pos]); @@ -155,7 +155,7 @@ namespace NLastGetopt { throw TUsageException() << "unknown option '" << optionName << "' in '" << Argv_[pos] << "'"; } - } + } if (arg.IsInited()) { if (option->GetHasArg() == NO_ARGUMENT) throw TUsageException() << "option " << optionName << " must have no arg"; @@ -165,9 +165,9 @@ namespace NLastGetopt { return ParseOptParam(option, pos); } else { return ParseShortOptArg(pos); - } - } - + } + } + bool TOptsParser::ParseOptParam(const TOpt* opt, size_t pos) { Y_ASSERT(opt); if (opt->GetHasArg() == NO_ARGUMENT) { @@ -183,8 +183,8 @@ namespace NLastGetopt { return Commit(opt, arg, pos + 1, 0); } return Commit(opt, nullptr, pos, 0); - } - + } + TOptsParser::EIsOpt TOptsParser::IsOpt(const TStringBuf& arg) const { EIsOpt eio = EIO_NONE; if (1 < arg.length()) { @@ -202,35 +202,35 @@ namespace NLastGetopt { eio = EIO_PLUS; break; } - } + } return eio; - } - + } + static void memrotate(void* ptr, size_t size, size_t shift) { TTempBuf buf(shift); memcpy(buf.Data(), (char*)ptr + size - shift, shift); memmove((char*)ptr + shift, ptr, size - shift); memcpy(ptr, buf.Data(), shift); } - + bool TOptsParser::ParseWithPermutation() { Y_ASSERT(Sop_ == 0); Y_ASSERT(Opts_->ArgPermutation_ == PERMUTE); - + const size_t p0 = Pos_; - + size_t pc = Pos_; - + for (; pc < Argc_ && EIO_NONE == IsOpt(Argv_[pc]); ++pc) { // count non-args } - + if (pc == Argc_) { return CommitEndOfOptions(Pos_); } - + Pos_ = pc; - + bool r = ParseOptArg(Pos_); Y_ASSERT(r); while (Pos_ == pc) { @@ -238,33 +238,33 @@ namespace NLastGetopt { r = ParseShortOptWithinArg(Pos_, Sop_); Y_ASSERT(r); } - + size_t p2 = Pos_; - + Y_ASSERT(p2 - pc >= 1); Y_ASSERT(p2 - pc <= 2); - + memrotate(Argv_ + p0, (p2 - p0) * sizeof(void*), (p2 - pc) * sizeof(void*)); - + bool r2 = ParseOptArg(p0); Y_ASSERT(r2); return r2; } - + bool TOptsParser::DoNext() { Y_ASSERT(Pos_ <= Argc_); - + if (Pos_ == Argc_) return CommitEndOfOptions(Pos_); - + if (GotMinusMinus_ && Opts_->ArgPermutation_ == RETURN_IN_ORDER) { Y_ASSERT(Sop_ == 0); return Commit(nullptr, Argv_[Pos_], Pos_ + 1, 0); } - + if (Sop_ > 0) return ParseShortOptWithinArg(Pos_, Sop_); - + size_t pos = Pos_; const TStringBuf arg(Argv_[pos]); if (EIO_NONE != IsOpt(arg)) { @@ -283,59 +283,59 @@ namespace NLastGetopt { return Commit(nullptr, arg, pos + 1, 0); } else if (Opts_->ArgPermutation_ == REQUIRE_ORDER) { return CommitEndOfOptions(Pos_); - } else { + } else { return ParseWithPermutation(); - } - } - + } + } + bool TOptsParser::Next() { bool r = false; - + if (OptsDefault_.empty()) { CurrentOpt_ = nullptr; TempCurrentOpt_.Destroy(); - + CurrentValue_ = nullptr; - + if (Stopped_) return false; - + TOptsParser copy = *this; - + r = copy.DoNext(); - + Swap(copy); - + if (!r) { Stopped_ = true; // we are done; check for missing options Finish(); } - } - + } + if (!r && !OptsDefault_.empty()) { CurrentOpt_ = OptsDefault_.front(); CurrentValue_ = CurrentOpt_->GetDefaultValue(); OptsDefault_.pop_front(); r = true; } - + if (r) { if (CurOpt()) CurOpt()->FireHandlers(this); } return r; - } - + } + void TOptsParser::Finish() { const TOpts::TOptsVector& optvec = Opts_->Opts_; if (optvec.size() == OptsSeen_.size()) return; - + TVector<TString> missingLong; TVector<char> missingShort; - + TOpts::TOptsVector::const_iterator it; for (it = optvec.begin(); it != optvec.end(); ++it) { const TOpt* opt = (*it).Get(); @@ -343,7 +343,7 @@ namespace NLastGetopt { continue; if (OptsSeen_.contains(opt)) continue; - + if (opt->IsRequired()) { const TOpt::TLongNames& optnames = opt->GetLongNames(); if (!optnames.empty()) @@ -355,18 +355,18 @@ namespace NLastGetopt { } continue; } - + if (opt->HasDefaultValue()) OptsDefault_.push_back(opt); - } - + } + // also indicates subsequent options, if any, haven't been seen actually OptsSeen_.clear(); - + const size_t nmissing = missingLong.size() + missingShort.size(); if (0 == nmissing) return; - + TUsageException usage; usage << "The following option"; usage << ((1 == nmissing) ? " is" : "s are"); @@ -377,11 +377,11 @@ namespace NLastGetopt { usage << " -" << missingShort[i]; throw usage; // don't need lineinfo, just the message } - + void TOptsParser::PrintUsage(IOutputStream& os, const NColorizer::TColors& colors) const { Opts_->PrintUsage(ProgramName(), os, colors); } - + void TOptsParser::PrintUsage(IOutputStream& os) const { PrintUsage(os, NColorizer::AutoColors(os)); } diff --git a/library/cpp/getopt/small/last_getopt_parser.h b/library/cpp/getopt/small/last_getopt_parser.h index 292fb49d11..2cf8a6c308 100644 --- a/library/cpp/getopt/small/last_getopt_parser.h +++ b/library/cpp/getopt/small/last_getopt_parser.h @@ -1,20 +1,20 @@ -#pragma once - -#include "last_getopt_opts.h" - +#pragma once + +#include "last_getopt_opts.h" + #include <library/cpp/colorizer/fwd.h> -#include <util/generic/hash_set.h> -#include <util/generic/list.h> - -namespace NLastGetopt { +#include <util/generic/hash_set.h> +#include <util/generic/list.h> + +namespace NLastGetopt { /** -* NLastGetopt::TOptsParser is an implementation of parsing -* argv/argv into TOptsParseResult by rules of TOpts. -* -* The class allows to make complicated handlers. -* Note, that if PERMUTE mode is on, then data, pointed by argv can be changed. -*/ +* NLastGetopt::TOptsParser is an implementation of parsing +* argv/argv into TOptsParseResult by rules of TOpts. +* +* The class allows to make complicated handlers. +* Note, that if PERMUTE mode is on, then data, pointed by argv can be changed. +*/ class TOptsParser { enum EIsOpt { EIO_NONE, //is not an option name @@ -22,72 +22,72 @@ namespace NLastGetopt { EIO_DDASH, //double-dashed ("--opt") option name EIO_PLUS, //plus prefix ("+opt") option name }; - + public: // TODO: make private const TOpts* Opts_; //rules of parsing - + // argc/argv pair size_t Argc_; const char** Argv_; - + private: //the storage of last unkown options. TODO: can be moved to local-method scope TCopyPtr<TOpt> TempCurrentOpt_; - + public: //storage of argv[0] TString ProgramName_; - + //state of parsing: - + size_t Pos_; // current element withing argv size_t Sop_; // current char within arg bool Stopped_; bool GotMinusMinus_; //true if "--" have been seen in argv - + protected: const TOpt* CurrentOpt_; // ptr on the last meeted option TStringBuf CurrentValue_; // the value of the last met argument (corresponding to CurrentOpt_) - + private: typedef THashSet<const TOpt*> TdOptSet; TdOptSet OptsSeen_; //the set of options that have been met during parsing - + TList<const TOpt*> OptsDefault_; - + private: void Init(const TOpts* options, int argc, const char* argv[]); void Init(const TOpts* options, int argc, char* argv[]); - + bool CommitEndOfOptions(size_t pos); bool Commit(const TOpt* currentOption, const TStringBuf& currentValue, size_t pos, size_t sop); - + bool ParseShortOptArg(size_t pos); bool ParseOptArg(size_t pos); bool ParseOptParam(const TOpt* opt, size_t pos); bool ParseUnknownShortOptWithinArg(size_t pos, size_t sop); bool ParseShortOptWithinArg(size_t pos, size_t sop); bool ParseWithPermutation(); - + bool DoNext(); void Finish(); - + EIsOpt IsOpt(const TStringBuf& arg) const; - + void Swap(TOptsParser& that); - + public: TOptsParser(const TOpts* options, int argc, const char* argv[]) { Init(options, argc, argv); } - + TOptsParser(const TOpts* options, int argc, char* argv[]) { Init(options, argc, argv); } - + /// fetch next argument, false if no more arguments left bool Next(); - + bool Seen(const TOpt* opt) const { return OptsSeen_.contains(opt); } @@ -111,38 +111,38 @@ namespace NLastGetopt { const TOpt* CurOpt() const { return CurrentOpt_; } - + const char* CurVal() const { return CurrentValue_.data(); } - + const TStringBuf& CurValStr() const { return CurrentValue_; } - + TStringBuf CurValOrOpt() const { TStringBuf val(CurValStr()); if (!val.IsInited() && CurOpt()->HasOptionalValue()) val = CurOpt()->GetOptionalValue(); return val; } - + TStringBuf CurValOrDef(bool useDef = true) const { TStringBuf val(CurValOrOpt()); if (!val.IsInited() && useDef && CurOpt()->HasDefaultValue()) val = CurOpt()->GetDefaultValue(); return val; } - + // true if this option was actually specified by the user bool IsExplicit() const { return nullptr == CurrentOpt_ || !OptsSeen_.empty(); } - + bool CurrentIs(const TString& name) const { return CurOpt()->NameIs(name); } - + const TString& ProgramName() const { return ProgramName_; } diff --git a/library/cpp/getopt/small/last_getopt_support.h b/library/cpp/getopt/small/last_getopt_support.h index 352a8616fb..17bed3e614 100644 --- a/library/cpp/getopt/small/last_getopt_support.h +++ b/library/cpp/getopt/small/last_getopt_support.h @@ -136,21 +136,21 @@ namespace NLastGetopt { , Value(value) { } - + void operator()(const TOptsParser*) { *Target = Value; } }; - + TString OptToString(char c); TString OptToString(const TString& longOption); TString OptToString(const TOpt* opt); - + template <typename T> inline T OptFromStringImpl(const TStringBuf& value) { return FromString<T>(value); - } - + } + template <> inline TStringBuf OptFromStringImpl<TStringBuf>(const TStringBuf& value) { return value; diff --git a/library/cpp/getopt/small/opt.h b/library/cpp/getopt/small/opt.h index 2a04caab29..ecb57439bc 100644 --- a/library/cpp/getopt/small/opt.h +++ b/library/cpp/getopt/small/opt.h @@ -25,7 +25,7 @@ == 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 + GetEnv(_POSIX_OPTION_ORDER) :=> REQUIRE_ORDER == 1st non-option forces getopt to return EOF 3. default :=> PERMUTE diff --git a/library/cpp/getopt/small/ya.make b/library/cpp/getopt/small/ya.make index a11fc36ab3..96de0f04b1 100644 --- a/library/cpp/getopt/small/ya.make +++ b/library/cpp/getopt/small/ya.make @@ -12,11 +12,11 @@ SRCS( completion_generator.cpp formatted_output.cpp last_getopt.cpp - last_getopt_easy_setup.cpp - last_getopt_opt.cpp - last_getopt_opts.cpp - last_getopt_parser.cpp - last_getopt_parse_result.cpp + last_getopt_easy_setup.cpp + last_getopt_opt.cpp + last_getopt_opts.cpp + last_getopt_parser.cpp + last_getopt_parse_result.cpp modchooser.cpp opt.cpp opt2.cpp diff --git a/library/cpp/getopt/ut/last_getopt_ut.cpp b/library/cpp/getopt/ut/last_getopt_ut.cpp index 1d5f19d25f..c99a1d053d 100644 --- a/library/cpp/getopt/ut/last_getopt_ut.cpp +++ b/library/cpp/getopt/ut/last_getopt_ut.cpp @@ -1,7 +1,7 @@ -#include <library/cpp/getopt/last_getopt.h> +#include <library/cpp/getopt/last_getopt.h> #include <library/cpp/colorizer/colors.h> -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <util/generic/array_size.h> #include <util/string/subst.h> diff --git a/library/cpp/getopt/ut/modchooser_ut.cpp b/library/cpp/getopt/ut/modchooser_ut.cpp index 4e308c7ed3..a14c8a5853 100644 --- a/library/cpp/getopt/ut/modchooser_ut.cpp +++ b/library/cpp/getopt/ut/modchooser_ut.cpp @@ -1,6 +1,6 @@ -#include <library/cpp/getopt/modchooser.h> +#include <library/cpp/getopt/modchooser.h> -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <util/stream/str.h> diff --git a/library/cpp/getopt/ut/opt2_ut.cpp b/library/cpp/getopt/ut/opt2_ut.cpp index aedb2c24ea..0e7464747c 100644 --- a/library/cpp/getopt/ut/opt2_ut.cpp +++ b/library/cpp/getopt/ut/opt2_ut.cpp @@ -1,6 +1,6 @@ -#include <library/cpp/getopt/opt2.h> +#include <library/cpp/getopt/opt2.h> -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> //using namespace NLastGetopt; diff --git a/library/cpp/getopt/ut/opt_ut.cpp b/library/cpp/getopt/ut/opt_ut.cpp index c3d1ed3709..441aa493a0 100644 --- a/library/cpp/getopt/ut/opt_ut.cpp +++ b/library/cpp/getopt/ut/opt_ut.cpp @@ -1,6 +1,6 @@ -#include <library/cpp/getopt/opt.h> +#include <library/cpp/getopt/opt.h> -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <util/string/vector.h> Y_UNIT_TEST_SUITE(OptTest) { diff --git a/library/cpp/getopt/ut/posix_getopt_ut.cpp b/library/cpp/getopt/ut/posix_getopt_ut.cpp index b9edd9350f..b6d374bf28 100644 --- a/library/cpp/getopt/ut/posix_getopt_ut.cpp +++ b/library/cpp/getopt/ut/posix_getopt_ut.cpp @@ -1,6 +1,6 @@ -#include <library/cpp/getopt/posix_getopt.h> +#include <library/cpp/getopt/posix_getopt.h> -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> using namespace NLastGetopt; diff --git a/library/cpp/getopt/ut/wrap.cpp b/library/cpp/getopt/ut/wrap.cpp index 2d8bdf72b3..3444eea102 100644 --- a/library/cpp/getopt/ut/wrap.cpp +++ b/library/cpp/getopt/ut/wrap.cpp @@ -1,6 +1,6 @@ -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> -#include <library/cpp/getopt/small/wrap.h> +#include <library/cpp/getopt/small/wrap.h> Y_UNIT_TEST_SUITE(Wrap) { Y_UNIT_TEST(TestWrapping) { diff --git a/library/cpp/getopt/ut/ya.make b/library/cpp/getopt/ut/ya.make index c8e0b5377e..8d00669efb 100644 --- a/library/cpp/getopt/ut/ya.make +++ b/library/cpp/getopt/ut/ya.make @@ -1,4 +1,4 @@ -UNITTEST_FOR(library/cpp/getopt) +UNITTEST_FOR(library/cpp/getopt) OWNER(pg) diff --git a/library/cpp/getopt/ut/ygetopt_ut.cpp b/library/cpp/getopt/ut/ygetopt_ut.cpp index dfa4dfbfac..a76f117216 100644 --- a/library/cpp/getopt/ut/ygetopt_ut.cpp +++ b/library/cpp/getopt/ut/ygetopt_ut.cpp @@ -1,6 +1,6 @@ -#include <library/cpp/getopt/ygetopt.h> +#include <library/cpp/getopt/ygetopt.h> -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> class TGetOptTest: public TTestBase { UNIT_TEST_SUITE(TGetOptTest); diff --git a/library/cpp/getopt/ya.make b/library/cpp/getopt/ya.make index c8fc111c19..6df23b22b2 100644 --- a/library/cpp/getopt/ya.make +++ b/library/cpp/getopt/ya.make @@ -3,13 +3,13 @@ LIBRARY() OWNER(pg) PEERDIR( - library/cpp/getopt/small + library/cpp/getopt/small library/cpp/svnversion library/cpp/build_info ) SRCS( - GLOBAL print.cpp + GLOBAL print.cpp ) END() diff --git a/library/cpp/getopt/ygetopt.h b/library/cpp/getopt/ygetopt.h index 9e716e768d..fd018b3128 100644 --- a/library/cpp/getopt/ygetopt.h +++ b/library/cpp/getopt/ygetopt.h @@ -1,3 +1,3 @@ #pragma once -#include <library/cpp/getopt/small/ygetopt.h> +#include <library/cpp/getopt/small/ygetopt.h> |