From 1ed4ef1115bce001ce20e66e2b3acae102b76de9 Mon Sep 17 00:00:00 2001 From: Benson Chu Date: Sun, 22 Sep 2024 16:31:14 -0500 Subject: [PATCH] Option parsing --- Makefile | 1 + pegrep.asd | 4 ++-- src/pegrep.lisp | 52 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index c1ec4be..65157d0 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ $(LIBS): $(QL)/setup.lisp --eval '(ql:quickload :alexandria)' \ --eval '(ql:quickload :fiveam)' \ --eval '(ql:quickload :deploy)' \ + --eval '(ql:quickload :clingon)' \ --eval '(quit)' touch $@ diff --git a/pegrep.asd b/pegrep.asd index 797fdda..262bcad 100644 --- a/pegrep.asd +++ b/pegrep.asd @@ -11,7 +11,7 @@ :serial t :build-operation "program-op" :entry-point "pegrep:entry" - :depends-on ("alexandria") + :depends-on (:alexandria :clingon) :components ((:module "./src" :serial t @@ -52,7 +52,7 @@ :build-pathname "pegrep-deploy" :build-operation "deploy-op" :entry-point "pegrep:entry" - :depends-on ("alexandria") + :depends-on (:alexandria :clingon) :components ((:module "./src" :serial t diff --git a/src/pegrep.lisp b/src/pegrep.lisp index a8a63f2..6551234 100644 --- a/src/pegrep.lisp +++ b/src/pegrep.lisp @@ -1,8 +1,56 @@ (defpackage :pegrep - (:use :cl :alexandria) + (:use :cl :alexandria :clingon) + (:local-nicknames (:cli :clingon)) (:export :entry)) (in-package :pegrep) +(defun pegrep/options () + (list + (cli:make-option + :string + :description "Whitespace regexp" + :short-name #\w + :long-name "whitespace" + :key :whitespace) + (cli:make-option + :boolean/true + :description "Use default whitespace" + :short-name #\W + :key :whitespace) + (cli:make-option + :list + :description "Config Files" + :short-name #\c + :key :cfg-files) + (cli:make-option + :string + :required t + :description "PEG Expression" + :short-name #\e + :key :expr))) + +(defun pegrep/handler (cmd) + (format t "Hello, world!~%") + (format t "Whitespace Value: \"~a\"~%" + (let ((ws (cli:getopt cmd :whitespace))) + (cond ((stringp ws) + ws) + ((eq ws t) + "[ \\t\\n\\r]*") + (t "")))) + (format t "Config Files: ~a~%" + (cli:getopt cmd :cfg-files)) + (format t "PEG Expression: ~a~%" + (cli:getopt cmd :expr))) + +(defun pegrep/command () + (cli:make-command + :name "pegrep" + :description "when regular expressions aren't enough!" + :options (pegrep/options) + :handler #'pegrep/handler)) + (defun entry () - (format t "Hello, world!~%")) + (let ((cmd (pegrep/command))) + (cli:run cmd)))