preserves-tool

This commit is contained in:
Tony Garnock-Jones 2019-08-23 22:01:03 +01:00
parent f09067d719
commit a817fed40d
2 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,3 @@
#lang setup/infotab
(define racket-launcher-names '("preserves-tool"))
(define racket-launcher-libraries '("tool.rkt"))

View File

@ -0,0 +1,38 @@
#lang racket/base
(require "main.rkt")
(require racket/match)
(require racket/port)
(module+ main
(require racket/cmdline)
(define input-format 'text)
(define output-format 'binary)
(define indent? #t)
(define annotations? #t)
(command-line #:once-each
[("--ib" "--input-binary") "Set binary input mode"
(set! input-format 'binary)]
[("--it" "--input-text") "Set text input mode"
(set! input-format 'text)]
[("--ob" "--output-binary") "Set binary output mode"
(set! output-format 'binary)]
[("--ot" "--output-text") "Set text output mode"
(set! output-format 'text)]
["--indent" "Enable indent for text output"
(set! indent? #t)]
["--no-indent" "Disable indent for text output"
(set! indent? #f)]
["--annotations" "Output annotations"
(set! annotations? #t)]
["--no-annotations" "Strip annotations"
(set! annotations? #f)])
(define v ((if annotations? values strip-annotations)
(match input-format
['text (read-preserve-syntax #:source "<stdin>")]
['binary (decode-syntax (port->bytes))])))
(void (match output-format
['text (write-preserve v #:indent indent?)]
['binary (write-bytes (encode v))]))
(flush-output))