|
| 1 | +#lang syntax-parse-example |
| 2 | +@require[ |
| 3 | + (for-label racket/base racket/cmdline syntax/parse syntax-parse-example/hierarchical-cmdline/hierarchical-cmdline)] |
| 4 | + |
| 5 | +@(define hierarchical-cmdline-eval |
| 6 | + (make-base-eval '(require racket/cmdline syntax-parse-example/hierarchical-cmdline/hierarchical-cmdline))) |
| 7 | + |
| 8 | +@title{Hierarchical parsing of command-line arguments} |
| 9 | +@stxbee2021["Metaxal" 16] |
| 10 | +@nested[#:style 'inset @emph{Adapted from a @hyperlink["https://github.com/jackfirth/resyntax/pull/147/files" @elem{PR to @tt{resyntax}}]}] |
| 11 | + |
| 12 | +@; ============================================================================= |
| 13 | + |
| 14 | +@defmodule[syntax-parse-example/hierarchical-cmdline/hierarchical-cmdline]{} |
| 15 | + |
| 16 | +@defform[(shift-command-line-arguments body ...)]{ |
| 17 | +} |
| 18 | + |
| 19 | +@defform[(parameterize-help-if-empty-ccla body ...)]{ |
| 20 | +} |
| 21 | + |
| 22 | +The purpose of the first macro is to make it easy to parse command line |
| 23 | +arguments in a hierarchical way using the built-in @racket[command-line] form. The |
| 24 | +second macro is an additional helper that displays the help message |
| 25 | +automatically when no command-line argument is specified at this level, which |
| 26 | +avoids the case where the user tries one argument is then has no information |
| 27 | +about what to do next. |
| 28 | + |
| 29 | +@examples[#:eval hierarchical-cmdline-eval |
| 30 | + (define prog "my-prog") |
| 31 | + |
| 32 | + (define (parse-relative) |
| 33 | + (parameterize-help-if-empty-ccla |
| 34 | + (command-line |
| 35 | + #:program (string-append prog " --relative") |
| 36 | + #:once-each |
| 37 | + [("--left") => (shift-command-line-arguments |
| 38 | + (displayln "You're going left!") |
| 39 | + (parse-main)) |
| 40 | + '("Go to the left")] |
| 41 | + [("--right") => (shift-command-line-arguments |
| 42 | + (displayln "You're going right!") |
| 43 | + (parse-main)) |
| 44 | + '("Go to the right")]))) |
| 45 | + |
| 46 | + (define (parse-absolute) |
| 47 | + (parameterize-help-if-empty-ccla |
| 48 | + (command-line |
| 49 | + #:program (string-append prog " --absolute") |
| 50 | + #:once-each |
| 51 | + [("--north") => (shift-command-line-arguments |
| 52 | + (displayln "You're going north!") |
| 53 | + (parse-main)) |
| 54 | + '("Go to the north")] |
| 55 | + [("--south") => (shift-command-line-arguments |
| 56 | + (displayln "You're going south!") |
| 57 | + (parse-main)) |
| 58 | + '("Go to the south")]))) |
| 59 | + |
| 60 | + (define (parse-move) |
| 61 | + (parameterize-help-if-empty-ccla |
| 62 | + (command-line |
| 63 | + #:program (string-append prog " --move") |
| 64 | + #:once-each |
| 65 | + [("--relative") => (shift-command-line-arguments (parse-relative)) |
| 66 | + '("Specify a relative direction")] |
| 67 | + [("--absolute") => (shift-command-line-arguments (parse-absolute)) |
| 68 | + '("Specify an absolute direction")]))) |
| 69 | + |
| 70 | + (define (parse-main) |
| 71 | + (command-line |
| 72 | + #:program prog |
| 73 | + #:once-each |
| 74 | + [("--move") => (shift-command-line-arguments (parse-move)) |
| 75 | + '("Specify directions")] |
| 76 | + [("--jump") => (shift-command-line-arguments |
| 77 | + (displayln "You're jumping!") |
| 78 | + (parse-main)) |
| 79 | + '("jump")])) |
| 80 | + |
| 81 | + (code:comment "$ racket syntax-bee.rkt --move --relative --left --jump --jump --move --absolute --south --jump") |
| 82 | + (parameterize ([current-command-line-arguments (vector "--move" "--relative" "--left" "--jump" "--jump" "--move" "--absolute" "--south" "--jump")]) |
| 83 | + (parse-main)) |
| 84 | +] |
| 85 | + |
| 86 | +Implementation: |
| 87 | + |
| 88 | +@racketfile{hierarchical-cmdline.rkt} |
| 89 | + |
0 commit comments