Add #:usage-help option to command-line.

This allows specifying additional help text at the
top of the auto-generated help, before the options
specification.
This commit is contained in:
Sam Tobin-Hochstadt 2013-10-16 09:25:03 -04:00
parent 4037bc23d6
commit ac1a6ecda7
2 changed files with 30 additions and 10 deletions

View File

@ -17,6 +17,7 @@
(code:line #:once-each flag-spec ...)
(code:line #:once-any flag-spec ...)
(code:line #:final flag-spec ...)
(code:line #:usage-help string ...)
(code:line #:help-labels string ...)
(code:line #:ps string ...)]
[flag-spec (flags id ... help-spec body ...+)
@ -140,6 +141,10 @@ handler procedure and help string list returned by
@racket[handler-expr] and @racket[help-expr] are used as in the
@racket[_table] argument of @racket[parse-command-line].
A @racket[#:usage-help] clause inserts text lines immediately after
the usage line. Each string in the clause provides a separate line
of text.
A @racket[#:help-labels] clause inserts text lines into the help table
of command-line flags. Each string in the clause provides a separate
line of text.
@ -265,12 +270,13 @@ A @racket[table] is a list of flag specification sets. Each set is
represented as a pair of two items: a mode symbol and a list of either
help strings or flag specifications. A mode symbol is one of
@racket['once-each], @racket['once-any], @racket['multi],
@racket['final], @racket['help-labels], or @racket['ps] with the same meanings as
the corresponding clause tags in @racket[command-line]. For the
@racket['help-labels] or @racket['ps] mode, a list of help string is provided. For the
other modes, a list of flag specifications is provided, where each
specification maps a number of flags to a single handler procedure. A
specification is a list of three items:
@racket['final], @racket['help-labels], @racket['usage-help], or
@racket['ps] with the same meanings as the corresponding clause tags
in @racket[command-line]. For the @racket['help-labels],
@racket['usage-help] or @racket['ps] mode, a list of help strings is
provided. For the other modes, a list of flag specifications is
provided, where each specification maps a number of flags to a single
handler procedure. A specification is a list of three items:
@itemize[

View File

@ -64,6 +64,14 @@
(let ([a (syntax-e (car lst))]
[pieces (up-to-next-keyword (cdr lst))])
(case a
[(#:usage-help)
(for ([x (in-list pieces)])
(unless (string? (syntax-e x))
(serror "#:usage-help clause contains non-string"
x)))
(loop (at-next-keyword (cdr lst))
(cons (list* #'list #`(quote usage-help) pieces)
accum))]
[(#:help-labels)
(for ([x (in-list pieces)])
(unless (string? (syntax-e x))
@ -243,11 +251,11 @@
(for ([spec (in-list table)])
(unless (and (list? spec) (pair? spec))
(bad-table "spec-set must be a non-empty list: ~a" spec))
(unless (memq (car spec) '(once-any once-each multi final help-labels ps))
(bad-table "spec-set type must be 'once-any, 'once-each, 'multi, 'final, 'help-labels, or 'ps: ~a"
(unless (memq (car spec) '(once-any once-each multi final help-labels usage-help ps))
(bad-table "spec-set type must be 'once-any, 'once-each, 'multi, 'final, 'help-labels, 'usage-help, or 'ps: ~a"
(car spec)))
(for ([line (in-list (cdr spec))])
(if (memq (car spec) '(help-labels ps))
(if (memq (car spec) '(help-labels ps usage-help))
(unless (string? line)
(bad-table "~a line must be a string: ~e" (car spec) line))
(begin
@ -338,9 +346,13 @@
(let ([sp (open-output-string)])
(fprintf sp "~a [ <option> ... ]" (program-name program))
(print-args sp finish-help finish)
(for ([set (in-list table)]
#:when (eq? (car set) 'usage-help))
(for ([line (in-list (cdr set))])
(fprintf sp "\n ~a" line)))
(fprintf sp "\n where <option> is one of\n")
(for ([set (in-list table)] ; the original table
#:unless (eq? (car set) 'ps))
#:unless (memq (car set) '(ps usage-help)))
(if (eq? (car set) 'help-labels)
(for ([line (in-list (cdr set))])
(fprintf sp " ~a\n" line))
@ -398,6 +410,8 @@
(cdr spec)))]
[(eq? (car spec) 'once-any)
(once-spec-set (cdr spec))]
[(eq? (car spec) 'usage-help)
null]
[(eq? (car spec) 'help-labels)
null]
[(eq? (car spec) 'multi)