last_getopt_parse_result.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #pragma once
  2. #include "last_getopt_opts.h"
  3. #include "last_getopt_parser.h"
  4. namespace NLastGetopt {
  5. /**
  6. * NLastGetopt::TOptParseResult contains all arguments for exactly one TOpt,
  7. * that have been fetched during parsing
  8. *
  9. * The class is a wraper over a vector of nil-terminated strings.
  10. */
  11. class TOptParseResult {
  12. public:
  13. typedef TVector<const char*> TValues;
  14. public:
  15. TOptParseResult(const TOpt* opt = nullptr)
  16. : Opt_(opt)
  17. {
  18. }
  19. public:
  20. const TOpt& Opt() const {
  21. return *Opt_;
  22. }
  23. const TOpt* OptPtr() const {
  24. return Opt_;
  25. }
  26. const TValues& Values() const {
  27. return Values_;
  28. }
  29. bool Empty() const {
  30. return Values().empty();
  31. }
  32. size_t Count() const {
  33. return Values_.size();
  34. }
  35. void AddValue(const char* val) {
  36. if (nullptr != val)
  37. Values_.push_back(val);
  38. }
  39. const char* DefVal(const char* def = nullptr) const {
  40. return Opt().HasDefaultValue() ? Opt().GetDefaultValue().c_str() : def;
  41. }
  42. const char* Front(const char* def = nullptr) const {
  43. return Empty() ? DefVal(def) : Values().front();
  44. }
  45. const char* Back(const char* def = nullptr) const {
  46. return Empty() ? DefVal(def) : Values().back();
  47. }
  48. private:
  49. const TOpt* Opt_;
  50. TValues Values_;
  51. };
  52. /**
  53. * NLastGetopt::TOptsParseResult contains result of parsing argc,argv be parser.
  54. *
  55. * In most common case constructed by argc,argv pair and rules (TOpts).
  56. * The instance being constructed validates rules and performs parsing, stores result for futher access.
  57. *
  58. * If error during parsing occures, the program aborts with exit code 1.
  59. * Note, that if PERMUTE mode is on, then data, pointed by argv can be changed.
  60. */
  61. class TOptsParseResult {
  62. private:
  63. THolder<TOptsParser> Parser_; //The instance of parser.
  64. // XXX: make argc, argv
  65. typedef TVector<TOptParseResult> TdVec;
  66. TdVec Opts_; //Parsing result for all options, that have been explicitly defined in argc/argv
  67. TdVec OptsDef_; //Parsing result for options, that have been defined by default values only
  68. private:
  69. TOptParseResult& OptParseResult();
  70. /**
  71. * Searchs for object in given container
  72. *
  73. * @param vec container
  74. * @param opt ptr for required object
  75. *
  76. * @retunr ptr on corresponding TOptParseResult
  77. */
  78. static const TOptParseResult* FindParseResult(const TdVec& vec, const TOpt* opt);
  79. protected:
  80. /**
  81. * Performs parsing of comand line arguments.
  82. */
  83. void Init(const TOpts* options, int argc, const char** argv);
  84. TOptsParseResult() = default;
  85. public:
  86. /**
  87. * The action in case of parser failure.
  88. * Allows to asjust behavior in derived classes.
  89. * By default prints error string and aborts the program
  90. */
  91. virtual void HandleError() const;
  92. /**
  93. * Constructs object by parsing arguments with given rules
  94. *
  95. * @param options ptr on parsing rules
  96. * @param argc
  97. * @param argv
  98. */
  99. TOptsParseResult(const TOpts* options, int argc, const char* argv[]) {
  100. Init(options, argc, argv);
  101. }
  102. /**
  103. * Constructs object by parsing arguments with given rules
  104. *
  105. * @param options ptr on parsing rules
  106. * @param argc
  107. * @param argv
  108. */
  109. TOptsParseResult(const TOpts* options, int argc, char* argv[]) {
  110. Init(options, argc, const_cast<const char**>(argv));
  111. }
  112. virtual ~TOptsParseResult() = default;
  113. /**
  114. * Search for TOptParseResult that corresponds to given option (TOpt)
  115. *
  116. * @param opt ptr on required object
  117. * @param includeDefault search in results obtained from default values
  118. *
  119. * @return ptr on result
  120. */
  121. const TOptParseResult* FindOptParseResult(const TOpt* opt, bool includeDefault = false) const;
  122. /**
  123. * Search for TOptParseResult that corresponds to given long name
  124. *
  125. * @param name long name of required object
  126. * @param includeDefault search in results obtained from default values
  127. *
  128. * @return ptr on result
  129. */
  130. const TOptParseResult* FindLongOptParseResult(const TString& name, bool includeDefault = false) const;
  131. /**
  132. * Search for TOptParseResult that corresponds to given short name
  133. *
  134. * @param c short name of required object
  135. * @param includeDefault search in results obtained from default values
  136. *
  137. * @return ptr on result
  138. */
  139. const TOptParseResult* FindCharOptParseResult(char c, bool includeDefault = false) const;
  140. /**
  141. * @return argv[0]
  142. */
  143. TString GetProgramName() const;
  144. /**
  145. * Print usage string.
  146. */
  147. void PrintUsage(IOutputStream& os = Cout) const;
  148. /**
  149. * @return position in [premuted argv] of the first free argument
  150. */
  151. size_t GetFreeArgsPos() const;
  152. /**
  153. * @return number of fetched free arguments
  154. */
  155. size_t GetFreeArgCount() const;
  156. /**
  157. * @return all fetched free arguments
  158. */
  159. TVector<TString> GetFreeArgs() const;
  160. /**
  161. * @return true if given option exist in results of parsing
  162. *
  163. * @param opt ptr on required object
  164. * @param includeDefault search in results obtained from default values
  165. *
  166. */
  167. bool Has(const TOpt* opt, bool includeDefault = false) const;
  168. /**
  169. * @return nil terminated string on the last fetched argument of givne option
  170. *
  171. * @param opt ptr on required object
  172. * @param includeDefault search in results obtained from default values
  173. */
  174. const char* Get(const TOpt* opt, bool includeDefault = true) const;
  175. /**
  176. * @return nil terminated string on the last fetched argument of givne option
  177. * if option haven't been fetched, given defaultValue will be returned
  178. *
  179. * @param opt ptr on required object
  180. * @param defaultValue
  181. */
  182. const char* GetOrElse(const TOpt* opt, const char* defaultValue) const;
  183. /**
  184. * @return true if given option exist in results of parsing
  185. *
  186. * @param name long name of required object
  187. * @param includeDefault search in results obtained from default values
  188. *
  189. */
  190. bool Has(const TString& name, bool includeDefault = false) const;
  191. /**
  192. * @return nil terminated string on the last fetched argument of givne option
  193. *
  194. * @param name long name of required object
  195. * @param includeDefault search in results obtained from default values
  196. */
  197. const char* Get(const TString& name, bool includeDefault = true) const;
  198. /**
  199. * @return nil terminated string on the last fetched argument of givne option
  200. * if option haven't been fetched, given defaultValue will be returned
  201. *
  202. * @param name long name of required object
  203. * @param defaultValue
  204. */
  205. const char* GetOrElse(const TString& name, const char* defaultValue) const;
  206. /**
  207. * @return true if given option exist in results of parsing
  208. *
  209. * @param c short name of required object
  210. * @param includeDefault search in results obtained from default values
  211. *
  212. */
  213. bool Has(char name, bool includeDefault = false) const;
  214. /**
  215. * @return nil terminated string on the last fetched argument of givne option
  216. *
  217. * @param c short name of required object
  218. * @param includeDefault search in results obtained from default values
  219. */
  220. const char* Get(char name, bool includeDefault = true) const;
  221. /**
  222. * @return nil terminated string on the last fetched argument of givne option
  223. * if option haven't been fetched, given defaultValue will be returned
  224. *
  225. * @param c short name of required object
  226. * @param defaultValue
  227. */
  228. const char* GetOrElse(char name, const char* defaultValue) const;
  229. /**
  230. * for givne option return parsed value of the last fetched argument
  231. * if option haven't been fetched, HandleError action is called
  232. *
  233. * @param opt required option (one of: ptr, short name, long name).
  234. *
  235. * @return FromString<T>(last feteched argument)
  236. */
  237. template <typename T, typename TKey>
  238. T Get(const TKey opt) const {
  239. const char* value = Get(opt);
  240. try {
  241. return NPrivate::OptFromString<T>(value, opt);
  242. } catch (...) {
  243. HandleError();
  244. throw;
  245. }
  246. }
  247. /**
  248. * for givne option return parsed value of the last fetched argument
  249. * if option haven't been fetched, given defaultValue will be returned
  250. *
  251. * @param opt required option (one of: ptr, short name, long name).
  252. * @param defaultValue
  253. *
  254. * @return FromString<T>(last feteched argument)
  255. */
  256. template <typename T, typename TKey>
  257. T GetOrElse(const TKey opt, const T& defaultValue) const {
  258. if (Has(opt))
  259. return Get<T>(opt);
  260. else
  261. return defaultValue;
  262. }
  263. /**
  264. * @return returns the argv with which the parser was started
  265. */
  266. const char** GetSourceArgv() {
  267. return Parser_ ? Parser_->Argv_ : nullptr;
  268. }
  269. /**
  270. * @returns the argc with which the parser was started
  271. */
  272. size_t GetSourceArgc() {
  273. return Parser_ ? Parser_->Argc_ : 0;
  274. }
  275. };
  276. /**
  277. * NLastGetopt::TOptsParseResultException contains result of parsing argc,argv be parser.
  278. *
  279. * Unlike TOptsParseResult, if error during parsing occures, an exception is thrown.
  280. *
  281. */
  282. class TOptsParseResultException: public TOptsParseResult {
  283. public:
  284. TOptsParseResultException(const TOpts* options, int argc, const char* argv[]) {
  285. Init(options, argc, argv);
  286. }
  287. TOptsParseResultException(const TOpts* options, int argc, char* argv[]) {
  288. Init(options, argc, const_cast<const char**>(argv));
  289. }
  290. virtual ~TOptsParseResultException() = default;
  291. void HandleError() const override;
  292. protected:
  293. TOptsParseResultException() = default;
  294. };
  295. }