From a1bd8d0681824287cf6b7629ea42a08480a2bfa0 Mon Sep 17 00:00:00 2001 From: Timothy Sample Date: Sun, 25 Nov 2018 02:20:57 -0500 Subject: [PATCH] Add basic option processing to the main script * geesh/repl.scm (run-repl): Add optional port parameter. * scripts/geesh.in: Add '--command' and '--stdin' flags, and if neither flag is provided, treat the first argument as a file to interpret. --- geesh/repl.scm | 6 +++--- scripts/geesh.in | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/geesh/repl.scm b/geesh/repl.scm index 310a7e9..21bd5e5 100644 --- a/geesh/repl.scm +++ b/geesh/repl.scm @@ -29,10 +29,10 @@ ;;; ;;; Code: -(define (run-repl) - (let loop ((exp (read-sh (current-input-port)))) +(define* (run-repl #:optional (port (current-input-port))) + (let loop ((exp (read-sh port))) (if (eof-object? exp) (get-status) (begin (eval-sh exp) - (loop (read-sh (current-input-port))))))) + (loop (read-sh port)))))) diff --git a/scripts/geesh.in b/scripts/geesh.in index 481793b..738c053 100644 --- a/scripts/geesh.in +++ b/scripts/geesh.in @@ -20,6 +20,41 @@ ;;; You should have received a copy of the GNU General Public License ;;; along with Geesh. If not, see . -(use-modules (geesh repl)) +(use-modules (geesh repl) + (ice-9 getopt-long)) -(exit (run-repl)) +(define options-spec + '((command (single-char #\c) (value #t)) + (stdin (single-char #\s)))) + +(let* ((options (getopt-long (program-arguments) options-spec + #:stop-at-first-non-option #t)) + (command (option-ref options 'command #f)) + (stdin (option-ref options 'stdin #f)) + (args (option-ref options '() '()))) + (cond + ((and command stdin) + (format (current-error-port) + "~a: Invalid options.~%" + (car (program-arguments))) + (exit EXIT_FAILURE)) + (command + (if (null? args) + (set-program-arguments (list (car (program-arguments)))) + (set-program-arguments args)) + (call-with-input-string command + (lambda (port) + (exit (run-repl port))))) + (stdin + (set-program-arguments (cons (car (program-arguments)) args)) + (exit (run-repl))) + (else + (cond + ((null? args) + (set-program-arguments (list (car (program-arguments)))) + (exit (run-repl))) + (else + (set-program-arguments args) + (call-with-input-file (car args) + (lambda (port) + (exit (run-repl port)))))))))