diff options
author | Ilnur Khuziev <ilnur.khuziev@yandex.ru> | 2022-02-10 16:46:13 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:13 +0300 |
commit | 736dcd8ca259457a136f2f9f9168c44643914323 (patch) | |
tree | ddd46a036d68bfa83aa11b892f31243ea6b068a1 /library/cpp/getopt/small/last_getopt_opts.cpp | |
parent | 9bf2fa2b060c9881d3135c2208c624a1dd546ecc (diff) | |
download | ydb-736dcd8ca259457a136f2f9f9168c44643914323.tar.gz |
Restoring authorship annotation for Ilnur Khuziev <ilnur.khuziev@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/getopt/small/last_getopt_opts.cpp')
-rw-r--r-- | library/cpp/getopt/small/last_getopt_opts.cpp | 182 |
1 files changed, 91 insertions, 91 deletions
diff --git a/library/cpp/getopt/small/last_getopt_opts.cpp b/library/cpp/getopt/small/last_getopt_opts.cpp index 03c432849f..7122bba10a 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; } } - } -} + } +} |