modchooser.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #pragma once
  2. #include "last_getopt_opts.h"
  3. #include <util/generic/map.h>
  4. #include <util/generic/string.h>
  5. #include <util/generic/vector.h>
  6. #include <functional>
  7. //! Mode function with vector of cli arguments.
  8. using TMainFunctionPtrV = std::function<int(const TVector<TString>&)> ;
  9. using TMainFunctionRawPtrV = int (*)(const TVector<TString>& argv);
  10. //! Mode function with classic argc and argv arguments.
  11. using TMainFunctionPtr = std::function<int(int, const char**)> ;
  12. using TMainFunctionRawPtr = int (*)(const int argc, const char** argv);
  13. //! Mode class with vector of cli arguments.
  14. class TMainClassV {
  15. public:
  16. virtual int operator()(const TVector<TString>& argv) = 0;
  17. virtual ~TMainClassV() = default;
  18. };
  19. //! Mode class with classic argc and argv arguments.
  20. class TMainClass {
  21. public:
  22. virtual int operator()(int argc, const char** argv) = 0;
  23. virtual ~TMainClass() = default;
  24. };
  25. //! Function to handle '--version' parameter
  26. typedef void (*TVersionHandlerPtr)();
  27. /*! Main class for handling different modes in single tool.
  28. *
  29. * You can add modes for this class, use autogenerated help with
  30. * list of modes and automaticly call necessary mode in run().
  31. *
  32. * In first argv element mode get joined by space tool name and
  33. * current mode name.
  34. */
  35. class TModChooser {
  36. public:
  37. TModChooser();
  38. ~TModChooser();
  39. public:
  40. void AddMode(const TString& mode, TMainFunctionRawPtr func, const TString& description, bool hidden = false, bool noCompletion = false);
  41. void AddMode(const TString& mode, TMainFunctionRawPtrV func, const TString& description, bool hidden = false, bool noCompletion = false);
  42. void AddMode(const TString& mode, TMainFunctionPtr func, const TString& description, bool hidden = false, bool noCompletion = false);
  43. void AddMode(const TString& mode, TMainFunctionPtrV func, const TString& description, bool hidden = false, bool noCompletion = false);
  44. void AddMode(const TString& mode, TMainClass* func, const TString& description, bool hidden = false, bool noCompletion = false);
  45. void AddMode(const TString& mode, TMainClassV* func, const TString& description, bool hidden = false, bool noCompletion = false);
  46. //! Hidden groups won't be displayed in 'help' block
  47. void AddGroupModeDescription(const TString& description, bool hidden = false, bool noCompletion = false);
  48. //! Set default mode (if not specified explicitly)
  49. void SetDefaultMode(const TString& mode);
  50. void AddAlias(const TString& alias, const TString& mode);
  51. //! Set main program description.
  52. void SetDescription(const TString& descr);
  53. //! Set modes help option name (-? is by default)
  54. void SetModesHelpOption(const TString& helpOption);
  55. //! Specify handler for '--version' parameter
  56. void SetVersionHandler(TVersionHandlerPtr handler);
  57. //! Set description show mode
  58. void SetSeparatedMode(bool separated = true);
  59. //! Set separation string
  60. void SetSeparationString(const TString& str);
  61. //! Set short command representation in Usage block
  62. void SetPrintShortCommandInUsage(bool printShortCommandInUsage);
  63. void DisableSvnRevisionOption();
  64. void AddCompletions(TString progName, const TString& name = "completion", bool hidden = false, bool noCompletion = false);
  65. /*! Run appropriate mode.
  66. *
  67. * In this method following things happen:
  68. * 1) If first argument is -h/--help/-? then print short description of
  69. * all modes and exit with zero code.
  70. * 2) If first argument is -v/--version and version handler is specified,
  71. * then call it and exit with zero code.
  72. * 3) Find mode with the same name as first argument. If it's found then
  73. * call it and return its return code.
  74. * 4) If appropriate mode is not found - return non-zero code.
  75. */
  76. int Run(int argc, const char** argv) const;
  77. //! Run appropriate mode. Same as Run(const int, const char**)
  78. int Run(const TVector<TString>& argv) const;
  79. void PrintHelp(const TString& progName) const;
  80. struct TMode {
  81. TString Name;
  82. TMainClass* Main;
  83. TString Description;
  84. bool Hidden;
  85. bool NoCompletion;
  86. TVector<TString> Aliases;
  87. TMode()
  88. : Main(nullptr)
  89. {
  90. }
  91. TMode(const TString& name, TMainClass* main, const TString& descr, bool hidden, bool noCompletion);
  92. // Full name includes primary name and aliases. Also, will add ANSI colors.
  93. size_t CalculateFullNameLen() const;
  94. TString FormatFullName(size_t pad) const;
  95. };
  96. TVector<const TMode*> GetUnsortedModes() const {
  97. auto ret = TVector<const TMode*>(Reserve(UnsortedModes.size()));
  98. for (auto& mode : UnsortedModes) {
  99. ret.push_back(mode.Get());
  100. }
  101. return ret;
  102. }
  103. TVersionHandlerPtr GetVersionHandler() const;
  104. bool IsSvnRevisionOptionDisabled() const;
  105. private:
  106. //! Main program description.
  107. TString Description;
  108. //! Help option for modes.
  109. TString ModesHelpOption;
  110. //! Wrappers around all modes.
  111. TVector<THolder<TMainClass>> Wrappers;
  112. //! Modes
  113. TMap<TString, TMode*> Modes;
  114. TString DefaultMode;
  115. //! Handler for '--version' parameter
  116. TVersionHandlerPtr VersionHandler;
  117. //! When set to true, show descriptions unsorted and display separators
  118. bool ShowSeparated;
  119. //! When set to true, disables --svnrevision option, useful for opensource (git hosted) projects
  120. bool SvnRevisionOptionDisabled;
  121. //! When true - will print only 'mode name' in 'Usage' block
  122. bool PrintShortCommandInUsage;
  123. //! Text string used when displaying each separator
  124. TString SeparationString;
  125. //! Unsorted list of options
  126. TVector<THolder<TMode>> UnsortedModes;
  127. //! Mode that generates completions
  128. THolder<TMainClass> CompletionsGenerator;
  129. };
  130. //! Mode class that allows introspecting its console arguments.
  131. class TMainClassArgs: public TMainClass {
  132. public:
  133. int operator()(int argc, const char** argv) final;
  134. public:
  135. //! Run this mode.
  136. int Run(int argc, const char** argv);
  137. //! Get console arguments for this mode.
  138. const NLastGetopt::TOpts& GetOptions();
  139. protected:
  140. //! Fill given empty `TOpts` with options.
  141. virtual void RegisterOptions(NLastGetopt::TOpts& opts);
  142. //! Actual mode logic. Takes parsed options and returns exit code.
  143. virtual int DoRun(NLastGetopt::TOptsParseResult&& parsedOptions) = 0;
  144. private:
  145. TMaybe<NLastGetopt::TOpts> Opts_;
  146. };
  147. //! Mode class that uses sub-modes to dispatch commands further.
  148. class TMainClassModes: public TMainClass {
  149. public:
  150. int operator()(int argc, const char** argv) final;
  151. public:
  152. //! Run this mode.
  153. int Run(int argc, const char** argv);
  154. //! Get sub-modes for this mode.
  155. const TModChooser& GetSubModes();
  156. protected:
  157. //! Fill given modchooser with sub-modes.
  158. virtual void RegisterModes(TModChooser& modes);
  159. private:
  160. TMaybe<TModChooser> Modes_;
  161. };