last_getopt_parser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "last_getopt_parser.h"
  2. #include <library/cpp/colorizer/colors.h>
  3. #include <util/string/escape.h>
  4. namespace NLastGetopt {
  5. void TOptsParser::Init(const TOpts* opts, int argc, const char* argv[]) {
  6. opts->Validate();
  7. Opts_ = opts;
  8. if (argc < 1)
  9. throw TUsageException() << "argv must have at least one argument";
  10. Argc_ = argc;
  11. Argv_ = argv;
  12. ProgramName_ = argv[0];
  13. Pos_ = 1;
  14. Sop_ = 0;
  15. CurrentOpt_ = nullptr;
  16. CurrentValue_ = nullptr;
  17. GotMinusMinus_ = false;
  18. Stopped_ = false;
  19. OptsSeen_.clear();
  20. OptsDefault_.clear();
  21. }
  22. void TOptsParser::Init(const TOpts* opts, int argc, char* argv[]) {
  23. Init(opts, argc, const_cast<const char**>(argv));
  24. }
  25. void TOptsParser::Swap(TOptsParser& that) {
  26. DoSwap(Opts_, that.Opts_);
  27. DoSwap(Argc_, that.Argc_);
  28. DoSwap(Argv_, that.Argv_);
  29. DoSwap(TempCurrentOpt_, that.TempCurrentOpt_);
  30. DoSwap(ProgramName_, that.ProgramName_);
  31. DoSwap(Pos_, that.Pos_);
  32. DoSwap(Sop_, that.Sop_);
  33. DoSwap(Stopped_, that.Stopped_);
  34. DoSwap(CurrentOpt_, that.CurrentOpt_);
  35. DoSwap(CurrentValue_, that.CurrentValue_);
  36. DoSwap(GotMinusMinus_, that.GotMinusMinus_);
  37. DoSwap(OptsSeen_, that.OptsSeen_);
  38. }
  39. bool TOptsParser::Commit(const TOpt* currentOpt, const TStringBuf& currentValue, size_t pos, size_t sop) {
  40. Pos_ = pos;
  41. Sop_ = sop;
  42. CurrentOpt_ = currentOpt;
  43. CurrentValue_ = currentValue;
  44. if (nullptr != currentOpt)
  45. OptsSeen_.insert(currentOpt);
  46. return true;
  47. }
  48. bool TOptsParser::CommitEndOfOptions(size_t pos) {
  49. Pos_ = pos;
  50. Sop_ = 0;
  51. Y_ASSERT(!CurOpt());
  52. Y_ASSERT(!CurVal());
  53. Y_ASSERT(!Stopped_);
  54. if (Opts_->FreeArgsMin_ == Opts_->FreeArgsMax_ && Argc_ - Pos_ != Opts_->FreeArgsMin_)
  55. throw TUsageException() << "required exactly " << Opts_->FreeArgsMin_ << " free args";
  56. else if (Argc_ - Pos_ < Opts_->FreeArgsMin_)
  57. throw TUsageException() << "required at least " << Opts_->FreeArgsMin_ << " free args";
  58. else if (Argc_ - Pos_ > Opts_->FreeArgsMax_)
  59. throw TUsageException() << "required at most " << Opts_->FreeArgsMax_ << " free args";
  60. return false;
  61. }
  62. bool TOptsParser::ParseUnknownShortOptWithinArg(size_t pos, size_t sop) {
  63. Y_ASSERT(pos < Argc_);
  64. const TStringBuf arg(Argv_[pos]);
  65. Y_ASSERT(sop > 0);
  66. Y_ASSERT(sop < arg.length());
  67. Y_ASSERT(EIO_NONE != IsOpt(arg));
  68. if (!Opts_->AllowUnknownCharOptions_)
  69. throw TUsageException() << "unknown option '" << EscapeC(arg[sop])
  70. << "' in '" << arg << "'";
  71. TempCurrentOpt_.Reset(new TOpt);
  72. TempCurrentOpt_->AddShortName(arg[sop]);
  73. sop += 1;
  74. // mimic behavior of Opt: unknown option has arg only if char is last within arg
  75. if (sop < arg.length()) {
  76. return Commit(TempCurrentOpt_.Get(), nullptr, pos, sop);
  77. }
  78. pos += 1;
  79. sop = 0;
  80. if (pos == Argc_ || EIO_NONE != IsOpt(Argv_[pos])) {
  81. return Commit(TempCurrentOpt_.Get(), nullptr, pos, 0);
  82. }
  83. return Commit(TempCurrentOpt_.Get(), Argv_[pos], pos + 1, 0);
  84. }
  85. bool TOptsParser::ParseShortOptWithinArg(size_t pos, size_t sop) {
  86. Y_ASSERT(pos < Argc_);
  87. const TStringBuf arg(Argv_[pos]);
  88. Y_ASSERT(sop > 0);
  89. Y_ASSERT(sop < arg.length());
  90. Y_ASSERT(EIO_NONE != IsOpt(arg));
  91. size_t p = sop;
  92. char c = arg[p];
  93. const TOpt* opt = Opts_->FindCharOption(c);
  94. if (!opt)
  95. return ParseUnknownShortOptWithinArg(pos, sop);
  96. p += 1;
  97. if (p == arg.length()) {
  98. return ParseOptParam(opt, pos + 1);
  99. }
  100. if (opt->GetHasArg() == NO_ARGUMENT) {
  101. return Commit(opt, nullptr, pos, p);
  102. }
  103. return Commit(opt, arg.SubStr(p), pos + 1, 0);
  104. }
  105. bool TOptsParser::ParseShortOptArg(size_t pos) {
  106. Y_ASSERT(pos < Argc_);
  107. const TStringBuf arg(Argv_[pos]);
  108. Y_ASSERT(EIO_NONE != IsOpt(arg));
  109. Y_ASSERT(!arg.StartsWith("--"));
  110. return ParseShortOptWithinArg(pos, 1);
  111. }
  112. bool TOptsParser::ParseOptArg(size_t pos) {
  113. Y_ASSERT(pos < Argc_);
  114. TStringBuf arg(Argv_[pos]);
  115. const EIsOpt eio = IsOpt(arg);
  116. Y_ASSERT(EIO_NONE != eio);
  117. if (EIO_DDASH == eio || EIO_PLUS == eio || (Opts_->AllowSingleDashForLong_ || !Opts_->HasAnyShortOption())) {
  118. // long option
  119. bool singleCharPrefix = EIO_DDASH != eio;
  120. arg.Skip(singleCharPrefix ? 1 : 2);
  121. TStringBuf optionName = arg.NextTok('=');
  122. const TOpt* option = Opts_->FindLongOption(optionName);
  123. if (!option) {
  124. if (singleCharPrefix && !arg.IsInited()) {
  125. return ParseShortOptArg(pos);
  126. } else if (Opts_->AllowUnknownLongOptions_) {
  127. return false;
  128. } else {
  129. throw TUsageException() << "unknown option '" << optionName
  130. << "' in '" << Argv_[pos] << "'";
  131. }
  132. }
  133. if (arg.IsInited()) {
  134. if (option->GetHasArg() == NO_ARGUMENT)
  135. throw TUsageException() << "option " << optionName << " must have no arg";
  136. return Commit(option, arg, pos + 1, 0);
  137. }
  138. ++pos;
  139. return ParseOptParam(option, pos);
  140. } else {
  141. return ParseShortOptArg(pos);
  142. }
  143. }
  144. bool TOptsParser::ParseOptParam(const TOpt* opt, size_t pos) {
  145. Y_ASSERT(opt);
  146. if (opt->GetHasArg() == NO_ARGUMENT || opt->IsEqParseOnly()) {
  147. return Commit(opt, nullptr, pos, 0);
  148. }
  149. if (pos == Argc_) {
  150. if (opt->GetHasArg() == REQUIRED_ARGUMENT)
  151. throw TUsageException() << "option " << opt->ToShortString() << " must have arg";
  152. return Commit(opt, nullptr, pos, 0);
  153. }
  154. const TStringBuf arg(Argv_[pos]);
  155. if (!arg.StartsWith('-') || opt->GetHasArg() == REQUIRED_ARGUMENT) {
  156. return Commit(opt, arg, pos + 1, 0);
  157. }
  158. return Commit(opt, nullptr, pos, 0);
  159. }
  160. TOptsParser::EIsOpt TOptsParser::IsOpt(const TStringBuf& arg) const {
  161. EIsOpt eio = EIO_NONE;
  162. if (1 < arg.length()) {
  163. switch (arg[0]) {
  164. default:
  165. break;
  166. case '-':
  167. if ('-' != arg[1])
  168. eio = EIO_SDASH;
  169. else if (2 < arg.length())
  170. eio = EIO_DDASH;
  171. break;
  172. case '+':
  173. if (Opts_->AllowPlusForLong_)
  174. eio = EIO_PLUS;
  175. break;
  176. }
  177. }
  178. return eio;
  179. }
  180. static void memrotate(void* ptr, size_t size, size_t shift) {
  181. TTempBuf buf(shift);
  182. memcpy(buf.Data(), (char*)ptr + size - shift, shift);
  183. memmove((char*)ptr + shift, ptr, size - shift);
  184. memcpy(ptr, buf.Data(), shift);
  185. }
  186. bool TOptsParser::ParseWithPermutation() {
  187. Y_ASSERT(Sop_ == 0);
  188. Y_ASSERT(Opts_->ArgPermutation_ == PERMUTE);
  189. const size_t p0 = Pos_;
  190. size_t pc = Pos_;
  191. for (; pc < Argc_ && EIO_NONE == IsOpt(Argv_[pc]); ++pc) {
  192. // count non-args
  193. }
  194. if (pc == Argc_) {
  195. return CommitEndOfOptions(Pos_);
  196. }
  197. Pos_ = pc;
  198. bool r = ParseOptArg(Pos_);
  199. Y_ASSERT(r);
  200. while (Pos_ == pc) {
  201. Y_ASSERT(Sop_ > 0);
  202. r = ParseShortOptWithinArg(Pos_, Sop_);
  203. Y_ASSERT(r);
  204. }
  205. size_t p2 = Pos_;
  206. Y_ASSERT(p2 - pc >= 1);
  207. Y_ASSERT(p2 - pc <= 2);
  208. memrotate(Argv_ + p0, (p2 - p0) * sizeof(void*), (p2 - pc) * sizeof(void*));
  209. bool r2 = ParseOptArg(p0);
  210. Y_ASSERT(r2);
  211. return r2;
  212. }
  213. bool TOptsParser::DoNext() {
  214. Y_ASSERT(Pos_ <= Argc_);
  215. if (Pos_ == Argc_)
  216. return CommitEndOfOptions(Pos_);
  217. if (GotMinusMinus_ && Opts_->ArgPermutation_ == RETURN_IN_ORDER) {
  218. Y_ASSERT(Sop_ == 0);
  219. return Commit(nullptr, Argv_[Pos_], Pos_ + 1, 0);
  220. }
  221. if (Sop_ > 0)
  222. return ParseShortOptWithinArg(Pos_, Sop_);
  223. size_t pos = Pos_;
  224. const TStringBuf arg(Argv_[pos]);
  225. if (EIO_NONE != IsOpt(arg)) {
  226. return ParseOptArg(pos);
  227. } else if (arg == "--") {
  228. if (Opts_->ArgPermutation_ == RETURN_IN_ORDER) {
  229. pos += 1;
  230. if (pos == Argc_)
  231. return CommitEndOfOptions(pos);
  232. GotMinusMinus_ = true;
  233. return Commit(nullptr, Argv_[pos], pos + 1, 0);
  234. } else {
  235. return CommitEndOfOptions(pos + 1);
  236. }
  237. } else if (Opts_->ArgPermutation_ == RETURN_IN_ORDER) {
  238. return Commit(nullptr, arg, pos + 1, 0);
  239. } else if (Opts_->ArgPermutation_ == REQUIRE_ORDER) {
  240. return CommitEndOfOptions(Pos_);
  241. } else {
  242. return ParseWithPermutation();
  243. }
  244. }
  245. bool TOptsParser::Next() {
  246. bool r = false;
  247. if (OptsDefault_.empty()) {
  248. CurrentOpt_ = nullptr;
  249. TempCurrentOpt_.Destroy();
  250. CurrentValue_ = nullptr;
  251. if (Stopped_)
  252. return false;
  253. TOptsParser copy = *this;
  254. r = copy.DoNext();
  255. Swap(copy);
  256. if (!r) {
  257. Stopped_ = true;
  258. // we are done; check for missing options
  259. Finish();
  260. }
  261. }
  262. if (!r && !OptsDefault_.empty()) {
  263. CurrentOpt_ = OptsDefault_.front();
  264. CurrentValue_ = CurrentOpt_->GetDefaultValue();
  265. OptsDefault_.pop_front();
  266. r = true;
  267. }
  268. if (r) {
  269. if (CurOpt())
  270. CurOpt()->FireHandlers(this);
  271. }
  272. return r;
  273. }
  274. void TOptsParser::Finish() {
  275. const TOpts::TOptsVector& optvec = Opts_->Opts_;
  276. if (optvec.size() == OptsSeen_.size())
  277. return;
  278. TVector<TString> missingLong;
  279. TVector<char> missingShort;
  280. TOpts::TOptsVector::const_iterator it;
  281. for (it = optvec.begin(); it != optvec.end(); ++it) {
  282. const TOpt* opt = (*it).Get();
  283. if (nullptr == opt)
  284. continue;
  285. if (OptsSeen_.contains(opt))
  286. continue;
  287. if (opt->IsRequired()) {
  288. const TOpt::TLongNames& optnames = opt->GetLongNames();
  289. if (!optnames.empty())
  290. missingLong.push_back(optnames[0]);
  291. else {
  292. const char ch = opt->GetCharOr0();
  293. if (0 != ch)
  294. missingShort.push_back(ch);
  295. }
  296. continue;
  297. }
  298. if (opt->HasDefaultValue())
  299. OptsDefault_.push_back(opt);
  300. }
  301. // also indicates subsequent options, if any, haven't been seen actually
  302. OptsSeen_.clear();
  303. const size_t nmissing = missingLong.size() + missingShort.size();
  304. if (0 == nmissing)
  305. return;
  306. TUsageException usage;
  307. usage << "The following option";
  308. usage << ((1 == nmissing) ? " is" : "s are");
  309. usage << " required:";
  310. for (size_t i = 0; i != missingLong.size(); ++i)
  311. usage << " --" << missingLong[i];
  312. for (size_t i = 0; i != missingShort.size(); ++i)
  313. usage << " -" << missingShort[i];
  314. throw usage; // don't need lineinfo, just the message
  315. }
  316. void TOptsParser::PrintUsage(IOutputStream& os, const NColorizer::TColors& colors) const {
  317. Opts_->PrintUsage(ProgramName(), os, colors);
  318. }
  319. void TOptsParser::PrintUsage(IOutputStream& os) const {
  320. PrintUsage(os, NColorizer::AutoColors(os));
  321. }
  322. }