/Sources/Guaka/Help/HelpGenerator.swift

https://github.com/oarrabi/Guaka · Swift · 486 lines · 18 code · 18 blank · 450 comment · 0 complexity · be1a26598ac02aec7e286f1df94ca862 MD5 · raw file

  1. //
  2. // DefaultHelpGenerator.swift
  3. // Guaka
  4. //
  5. // Created by Omar Abdelhafith on 19/11/2016.
  6. //
  7. //
  8. /// Protocol that defines the help generation logic.
  9. /// This protocol has an extension that implements all the methods required.
  10. /// You can choose to override `helpMessage` to completely costumize the help message to print.
  11. /// Alternatively, override one of the sections only to alter that section.
  12. ///
  13. /// When the help is about to be printed, a `CommandHelp` is generated from the current `Command`.
  14. /// a `HelpGenerator` is created and passed this `CommandHelp`.
  15. /// the `HelpGenerator` is used to print the help message string
  16. ///
  17. /// -----
  18. ///
  19. /// The help has this format
  20. ///
  21. /// ```
  22. /// Usage:
  23. /// command [flags]
  24. /// command [command]
  25. ///
  26. /// Aliases:
  27. /// alias1, alias2
  28. ///
  29. /// Examples:
  30. /// the command usage example
  31. ///
  32. /// Available Commands:
  33. /// sub_command1 subcommand description
  34. /// sub_command2 subcommand description
  35. ///
  36. /// Flags:
  37. /// -f, --flag flag1 description
  38. /// --user string flag2 description
  39. ///
  40. /// Use "command [command] --help" for more information about a command.
  41. /// ```
  42. ///
  43. public protocol HelpGenerator {
  44. /// Command Help instance
  45. var commandHelp: CommandHelp { get }
  46. /// Initialize with a command help
  47. init(commandHelp: CommandHelp)
  48. /// Initialize with a command
  49. init(command: Command)
  50. /// Return the full help message
  51. /// This help message will be printed when passing `-h` or `--help` to the command
  52. /// Override this method when you need to costumize the full message returned to consol
  53. /// If you need to only costumize part of the help, override one or more of the section strings
  54. ///
  55. /// ----
  56. /// Examples:
  57. ///
  58. /// ```
  59. /// var helpMessage: String {
  60. /// return "the help"
  61. /// }
  62. /// ```
  63. ///
  64. /// The help returned
  65. ///
  66. /// ```
  67. /// the help
  68. /// ```
  69. ///
  70. var helpMessage: String { get }
  71. /// Return the message to be printed after printing the error occured
  72. /// This message will be printed after the specific error occured
  73. /// Override this method when you need to costumize the full error message returned to consol
  74. /// If you need to only costumize part of the help, override one or more of the section strings
  75. ///
  76. /// ----
  77. /// Examples:
  78. ///
  79. /// ```
  80. /// var errorHelpMessage: String {
  81. /// return "the help"
  82. /// }
  83. /// ```
  84. ///
  85. /// The help returned
  86. ///
  87. /// ```
  88. /// Some error occured
  89. /// the help
  90. /// ```
  91. ///
  92. var errorHelpMessage: String { get }
  93. /// Returns the deprecation for this command
  94. /// When this is called, the help generator must return a message that uses the command.deprecatedStatus message
  95. /// This method will only be called if Guaka is about to execute this specific command.
  96. /// Returning nil ignores this section from the help message.
  97. ///
  98. /// ----
  99. /// Examples:
  100. ///
  101. /// ```
  102. /// var deprecationSection: String? {
  103. /// return "this command is deprecated"
  104. /// }
  105. /// ```
  106. ///
  107. /// The help returned
  108. ///
  109. /// ```
  110. /// this command is deprecated
  111. /// Usage:
  112. /// command [flags]
  113. /// command [command]
  114. ///
  115. /// Aliases:
  116. /// alias1, alias2
  117. ///
  118. /// Examples:
  119. /// the command usage example
  120. ///
  121. /// Available Commands:
  122. /// sub_command1 subcommand description
  123. /// sub_command2 subcommand description
  124. ///
  125. /// Flags:
  126. /// -f, --flag flag1 description
  127. /// --user string flag2 description
  128. ///
  129. /// Use "command [command] --help" for more information about a command.
  130. /// ```
  131. ///
  132. var deprecationSection: String? { get }
  133. /// Return command description section to be printed for when the command help is printed.
  134. /// Returning nil ignores this section from the help message.
  135. ///
  136. /// ----
  137. /// Examples:
  138. ///
  139. /// ```
  140. /// var commandDescriptionSection: String? {
  141. /// return "this is the command"
  142. /// }
  143. /// ```
  144. ///
  145. /// The help returned
  146. ///
  147. /// ```
  148. /// this is the command
  149. /// Usage:
  150. /// command [flags]
  151. /// command [command]
  152. ///
  153. /// Aliases:
  154. /// alias1, alias2
  155. ///
  156. /// Examples:
  157. /// the command usage example
  158. ///
  159. /// Available Commands:
  160. /// sub_command1 subcommand description
  161. /// sub_command2 subcommand description
  162. ///
  163. /// Flags:
  164. /// -f, --flag flag1 description
  165. /// --user string flag2 description
  166. ///
  167. /// Use "command [command] --help" for more information about a command.
  168. /// ```
  169. ///
  170. var commandDescriptionSection: String? { get }
  171. /// Return the usage secion to be printed for when the command help is printed.
  172. /// Returning nil ignores this section from the help message.
  173. ///
  174. /// ----
  175. /// Examples:
  176. ///
  177. /// ```
  178. /// var usageSection: String? {
  179. /// return "use as"
  180. /// }
  181. /// ```
  182. ///
  183. /// The help returned
  184. ///
  185. /// ```
  186. /// use as
  187. ///
  188. /// Aliases:
  189. /// alias1, alias2
  190. ///
  191. /// Examples:
  192. /// the command usage example
  193. ///
  194. /// Available Commands:
  195. /// sub_command1 subcommand description
  196. /// sub_command2 subcommand description
  197. ///
  198. /// Flags:
  199. /// -f, --flag flag1 description
  200. /// --user string flag2 description
  201. ///
  202. /// Use "command [command] --help" for more information about a command.
  203. /// ```
  204. ///
  205. var usageSection: String? { get }
  206. /// Return the aliases section to be printed for when the command help is printed.
  207. /// Returning nil ignores this section from the help message.
  208. ///
  209. /// ----
  210. /// Examples:
  211. ///
  212. /// ```
  213. /// var aliasesSection: String? {
  214. /// return "my aliases are 1, 2"
  215. /// }
  216. /// ```
  217. ///
  218. /// The help returned
  219. ///
  220. /// ```
  221. /// Usage:
  222. /// command [flags]
  223. /// command [command]
  224. ///
  225. /// my aliases are 1, 2
  226. ///
  227. /// Examples:
  228. /// the command usage example
  229. ///
  230. /// Available Commands:
  231. /// sub_command1 subcommand description
  232. /// sub_command2 subcommand description
  233. ///
  234. /// Flags:
  235. /// -f, --flag flag1 description
  236. /// --user string flag2 description
  237. ///
  238. /// Use "command [command] --help" for more information about a command.
  239. /// ```
  240. ///
  241. var aliasesSection: String? { get }
  242. /// Return the example section to be printed for when the command help is printed.
  243. /// Returning nil ignores this section from the help message.
  244. ///
  245. /// ----
  246. /// Examples:
  247. ///
  248. /// ```
  249. /// var exampleSection: String? {
  250. /// return "some examples"
  251. /// }
  252. /// ```
  253. ///
  254. /// The help returned
  255. ///
  256. /// ```
  257. /// Usage:
  258. /// command [flags]
  259. /// command [command]
  260. ///
  261. /// Aliases:
  262. /// alias1, alias2
  263. ///
  264. /// some examples
  265. ///
  266. /// Available Commands:
  267. /// sub_command1 subcommand description
  268. /// sub_command2 subcommand description
  269. ///
  270. /// Flags:
  271. /// -f, --flag flag1 description
  272. /// --user string flag2 description
  273. ///
  274. /// Use "command [command] --help" for more information about a command.
  275. /// ```
  276. ///
  277. var exampleSection: String? { get }
  278. /// Return the subcommands section to be printed for when the command help is printed.
  279. /// Returning nil ignores this section from the help message.
  280. ///
  281. /// ----
  282. /// Examples:
  283. ///
  284. /// ```
  285. /// var subCommandsSection: String? {
  286. /// return "Some sub commands" //should iterate the commands yourself
  287. /// }
  288. /// ```
  289. ///
  290. /// The help returned
  291. ///
  292. /// ```
  293. /// Usage:
  294. /// command [flags]
  295. /// command [command]
  296. ///
  297. /// Aliases:
  298. /// alias1, alias2
  299. ///
  300. /// Examples:
  301. /// the command usage example
  302. ///
  303. /// Some sub commands
  304. ///
  305. /// Flags:
  306. /// -f, --flag flag1 description
  307. /// --user string flag2 description
  308. ///
  309. /// Use "command [command] --help" for more information about a command.
  310. /// ```
  311. ///
  312. var subCommandsSection: String? { get }
  313. /// Return the flags description section to be printed for when the command help is printed.
  314. /// Returning nil ignores this section from the help message.
  315. ///
  316. /// ----
  317. /// Examples:
  318. ///
  319. /// ```
  320. /// var flagsSection: String? {
  321. /// return "the flags I have" // iterate the flags
  322. /// }
  323. /// ```
  324. ///
  325. /// The help returned
  326. ///
  327. /// ```
  328. /// Usage:
  329. /// command [flags]
  330. /// command [command]
  331. ///
  332. /// Aliases:
  333. /// alias1, alias2
  334. ///
  335. /// Examples:
  336. /// the command usage example
  337. ///
  338. /// Available Commands:
  339. /// sub_command1 subcommand description
  340. /// sub_command2 subcommand description
  341. ///
  342. /// the flags I have
  343. ///
  344. /// Use "command [command] --help" for more information about a command.
  345. /// ```
  346. ///
  347. var flagsSection: String? { get }
  348. /// Return command information description section to be printed for when the command help is printed.
  349. /// Returning nil ignores this section from the help message.
  350. ///
  351. /// ----
  352. /// Examples:
  353. ///
  354. /// ```
  355. /// var informationSection: String? {
  356. /// return "command info to get help"
  357. /// }
  358. /// ```
  359. ///
  360. /// The help returned
  361. ///
  362. /// ```
  363. /// Usage:
  364. /// command [flags]
  365. /// command [command]
  366. ///
  367. /// Aliases:
  368. /// alias1, alias2
  369. ///
  370. /// Examples:
  371. /// the command usage example
  372. ///
  373. /// Available Commands:
  374. /// sub_command1 subcommand description
  375. /// sub_command2 subcommand description
  376. ///
  377. /// Flags:
  378. /// -f, --flag flag1 description
  379. /// --user string flag2 description
  380. ///
  381. /// command info to get help
  382. /// ```
  383. ///
  384. var informationSection: String? { get }
  385. /// Return the string printed when an error occur.
  386. /// Returning nil ignores this section from the help message.
  387. ///
  388. /// ----
  389. /// Examples:
  390. ///
  391. /// ```
  392. /// func errorString(forError error: CommandError) -> String {
  393. /// return "error occured"
  394. /// }
  395. /// ```
  396. ///
  397. /// The help returned
  398. ///
  399. /// ```
  400. /// error occured
  401. /// Usage:
  402. /// command [flags]
  403. /// command [command]
  404. ///
  405. /// Aliases:
  406. /// alias1, alias2
  407. ///
  408. /// Examples:
  409. /// the command usage example
  410. ///
  411. /// Available Commands:
  412. /// sub_command1 subcommand description
  413. /// sub_command2 subcommand description
  414. ///
  415. /// Flags:
  416. /// -f, --flag flag1 description
  417. /// --user string flag2 description
  418. ///
  419. /// Use "command [command] --help" for more information about a command.
  420. /// ```
  421. ///
  422. func errorString(forError error: CommandError) -> String
  423. /// Return the string printed when a flag is deprecated.
  424. /// Returning nil ignores this section from the help message
  425. ///
  426. /// ----
  427. /// Examples:
  428. ///
  429. /// ```
  430. /// func deprecationMessage(forDeprecatedFlag flag: FlagHelp) -> String? {
  431. /// return "this flag is deprecated"
  432. /// }
  433. /// ```
  434. ///
  435. /// The help returned
  436. ///
  437. /// ```
  438. /// this flag is deprecated
  439. /// ```
  440. ///
  441. func deprecationMessage(forDeprecatedFlag flag: FlagHelp) -> String?
  442. /// Returns a suggestion message string when a unrecognized command is passed to the root command.
  443. /// Returning nil does not show the message
  444. ///
  445. /// ----
  446. /// Examples:
  447. ///
  448. /// ```
  449. /// public func suggestionMessage(original: String, suggestion: String) -> String? {
  450. /// return [
  451. /// "\(commandHelp.name): '\(original)' is not a \(commandHelp.name) command. See '\(commandHelp.name) --help'.",
  452. /// "",
  453. /// "Did you mean this?",
  454. /// " \(suggestion)"].joined(separator: "\n")
  455. /// }
  456. /// ```
  457. ///
  458. /// The suggestion message
  459. ///
  460. /// ```
  461. /// git: 'rbase' is not a git command. See 'git --help'.
  462. ///
  463. /// Did you mean this?
  464. /// rebase
  465. /// ```
  466. ///
  467. func suggestionMessage(original: String, suggestion: String) -> String?
  468. }