last_getopt_easy_setup.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include "last_getopt_opts.h"
  3. namespace NLastGetopt {
  4. /**
  5. * Wrapper for TOpts class to make the life a bit easier.
  6. * Usual usage:
  7. * TEasySetup opts;
  8. * opts('s', "server", "MR_SERVER", "MapReduce server name in format server:port", true)
  9. * ('u', "user", "MR_USER", "MapReduce user name", true)
  10. * ('o', "output", "MR_TABLE", "Name of MR table which will contain results", true)
  11. * ('r', "rules", "FILE", "Filename for .rules output file") //!< This parameter is optional and has a required argument
  12. * ('v', "version", &PrintSvnVersionAndExit0, "Print version information") //!< Parameter with handler can't get any argument
  13. * ("verbose", "Be verbose") //!< You may not specify short param name
  14. *
  15. * NLastGetopt::TOptsParseResult r(&opts, argc, argv);
  16. */
  17. class TEasySetup: public TOpts {
  18. public:
  19. TEasySetup(const TStringBuf& optstring = TStringBuf());
  20. TEasySetup& operator()(char shortName, const char* longName, const char* help, bool required = false);
  21. TEasySetup& operator()(char shortName, const char* longName, const char* argName, const char* help, bool required = false);
  22. template <class TpFunc>
  23. TEasySetup& operator()(char shortName, const char* longName, TpFunc handler, const char* help, bool required = false) {
  24. AdjustParam(longName, help, nullptr, handler, required).AddShortName(shortName);
  25. return *this;
  26. }
  27. TEasySetup& operator()(const char* longName, const char* help, bool required = false);
  28. TEasySetup& operator()(const char* longName, const char* argName, const char* help, bool required = false);
  29. template <class TpFunc>
  30. TEasySetup& operator()(const char* longName, TpFunc handler, const char* help, bool required = false) {
  31. AdjustParam(longName, help, nullptr, handler, required);
  32. return *this;
  33. }
  34. private:
  35. TOpt& AdjustParam(const char* longName, const char* help, const char* argName, bool required);
  36. template <class TpFunc>
  37. TOpt& AdjustParam(const char* longName, const char* help, const char* argName, TpFunc handler, bool required) {
  38. TOpt& o = AdjustParam(longName, help, argName, required);
  39. o.Handler0(handler);
  40. return o;
  41. }
  42. };
  43. }