help.scm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ;;; Help --- Show help on guild commands
  2. ;;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free
  16. ;;;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;;;; Boston, MA 02110-1301 USA
  18. ;;; Commentary:
  19. ;; Usage: help
  20. ;;
  21. ;; Show help for Guild scripts.
  22. ;;; Code:
  23. (define-module (scripts help)
  24. #:use-module (ice-9 format)
  25. #:use-module (ice-9 documentation)
  26. #:use-module ((srfi srfi-1) #:select (fold append-map))
  27. #:export (show-help show-summary show-usage main))
  28. (define %summary "Show a brief help message.")
  29. (define %synopsis "help\nhelp --all\nhelp COMMAND")
  30. (define %help "
  31. Show help on guild commands. With --all, show arcane incantations as
  32. well. With COMMAND, show more detailed help for a particular command.
  33. ")
  34. (define (directory-files dir)
  35. (if (and (file-exists? dir) (file-is-directory? dir))
  36. (let ((dir-stream (opendir dir)))
  37. (let loop ((new (readdir dir-stream))
  38. (acc '()))
  39. (if (eof-object? new)
  40. (begin
  41. (closedir dir-stream)
  42. acc)
  43. (loop (readdir dir-stream)
  44. (if (or (string=? "." new) ; ignore
  45. (string=? ".." new)) ; ignore
  46. acc
  47. (cons new acc))))))
  48. '()))
  49. (define (strip-extensions path)
  50. (or-map (lambda (ext)
  51. (and
  52. (string-suffix? ext path)
  53. ;; We really can't be adding e.g. ChangeLog-2008 to the set
  54. ;; of runnable scripts, just because "" is a valid
  55. ;; extension, by default. So hack around that here.
  56. (not (string-null? ext))
  57. (substring path 0
  58. (- (string-length path) (string-length ext)))))
  59. (append %load-compiled-extensions %load-extensions)))
  60. (define (unique l)
  61. (cond ((null? l) l)
  62. ((null? (cdr l)) l)
  63. ((equal? (car l) (cadr l)) (unique (cdr l)))
  64. (else (cons (car l) (unique (cdr l))))))
  65. (define (find-submodules head)
  66. (let ((shead (map symbol->string head)))
  67. (unique
  68. (sort
  69. (append-map (lambda (path)
  70. (fold (lambda (x rest)
  71. (let ((stripped (strip-extensions x)))
  72. (if stripped (cons stripped rest) rest)))
  73. '()
  74. (directory-files
  75. (fold (lambda (x y) (in-vicinity y x)) path shead))))
  76. %load-path)
  77. string<?))))
  78. (define (list-commands all?)
  79. (display "\
  80. Usage: guild COMMAND [ARGS]
  81. Run command-line scripts provided by GNU Guile and related programs.
  82. Commands:
  83. ")
  84. (for-each
  85. (lambda (name)
  86. (let* ((modname `(scripts ,(string->symbol name)))
  87. (mod (resolve-module modname #:ensure #f))
  88. (summary (and mod (and=> (module-variable mod '%summary)
  89. variable-ref))))
  90. (if (and mod
  91. (or all?
  92. (let ((v (module-variable mod '%include-in-guild-list)))
  93. (if v (variable-ref v) #t))))
  94. (if summary
  95. (format #t " ~A ~23t~a\n" name summary)
  96. (format #t " ~A\n" name)))))
  97. (find-submodules '(scripts)))
  98. (format #t "
  99. For help on a specific command, try \"guild help COMMAND\".
  100. Report guild bugs to ~a
  101. GNU Guile home page: <http://www.gnu.org/software/guile/>
  102. General help using GNU software: <http://www.gnu.org/gethelp/>
  103. For complete documentation, run: info guile 'Using Guile Tools'
  104. " %guile-bug-report-address))
  105. (define (module-commentary mod)
  106. (file-commentary
  107. (%search-load-path (module-filename mod))))
  108. (define (module-command-name mod)
  109. (symbol->string (car (last-pair (module-name mod)))))
  110. (define* (show-usage mod #:optional (port (current-output-port)))
  111. (let ((usages (string-split
  112. (let ((var (module-variable mod '%synopsis)))
  113. (if var
  114. (variable-ref var)
  115. (string-append (module-command-name mod)
  116. " OPTION...")))
  117. #\newline)))
  118. (display "Usage: guild " port)
  119. (display (car usages))
  120. (newline port)
  121. (for-each (lambda (u)
  122. (display " guild " port)
  123. (display u port)
  124. (newline port))
  125. (cdr usages))))
  126. (define* (show-summary mod #:optional (port (current-output-port)))
  127. (let ((var (module-variable mod '%summary)))
  128. (if var
  129. (begin
  130. (display (variable-ref var) port)
  131. (newline port)))))
  132. (define* (show-help mod #:optional (port (current-output-port)))
  133. (show-usage mod port)
  134. (show-summary mod port)
  135. (cond
  136. ((module-variable mod '%help)
  137. => (lambda (var)
  138. (display (variable-ref var) port)
  139. (newline port)))
  140. ((module-commentary mod)
  141. => (lambda (commentary)
  142. (newline port)
  143. (display commentary port)))
  144. (else
  145. (format #t "No documentation found for command \"~a\".\n"
  146. (module-command-name mod)))))
  147. (define %mod (current-module))
  148. (define (main . args)
  149. (cond
  150. ((null? args)
  151. (list-commands #f))
  152. ((or (equal? args '("--all")) (equal? args '("-a")))
  153. (list-commands #t))
  154. ((and (null? (cdr args)) (not (string-prefix? "-" (car args))))
  155. ;; help for particular command
  156. (let ((name (car args)))
  157. (cond
  158. ((resolve-module `(scripts ,(string->symbol name)) #:ensure #f)
  159. => (lambda (mod)
  160. (show-help mod)
  161. (exit 0)))
  162. (else
  163. (format #t "No command named \"~a\".\n" name)
  164. (exit 1)))))
  165. (else
  166. (show-help %mod (current-error-port))
  167. (exit 1))))