summaryrefslogtreecommitdiff
path: root/chickadee/cli.scm
blob: 2e104fb3602e6a82fcb5b50fca997dc855b11a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
;;; Chickadee Game Toolkit
;;; Copyright © 2021 David Thompson <dthompson2@worcester.edu>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (chickadee cli)
  #:use-module (chickadee)
  #:use-module (chickadee config)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-37)
  #:export (launch-chickadee
            display-version-and-exit
            leave
            simple-args-fold
            operands))

(define (display-version-and-exit)
  (format #t "Chickadee ~a
Copyright (C) 2021 David Thompson and Chickadee contributors
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.~%"
          %chickadee-version)
  (exit 0))

(define (display-help-and-exit)
  (format #t "Usage: chickadee SUBCOMMAND ARGS ...~%
Run SUBCOMMAND with ARGS

Valid subcommands:
* bundle
* play~%")
  (exit 1))

(define (leave format-string . args)
  "Display error message and exist."
  (apply format (current-error-port) format-string args)
  (newline)
  (exit 1))

(define (simple-args-fold args options defaults)
  (args-fold args options
             (lambda (opt name arg result)
               (leave "unrecognized option: ~A" name))
             (lambda (arg result)
               (alist-cons 'operand arg result))
             defaults))

(define (operands opts)
  (filter-map (match-lambda
                (('operand . arg) arg)
                (_ #f))
              opts))

(define (run-chickadee-command command . args)
  (define (invalid-command)
    (format (current-error-port) "invalid subcommand: ~a~%~%" command)
    (display-help-and-exit))
  (let* ((module
          (catch 'misc-error
            (lambda ()
              (resolve-interface `(chickadee cli ,command)))
            (lambda args
              (invalid-command))))
         (proc-name (symbol-append 'chickadee- command))
         (command-proc (false-if-exception (module-ref module proc-name))))
    (if (procedure? command-proc)
        (apply command-proc args)
        (invalid-command))))

(define (subcommand? arg)
  (not (string-prefix? "-" arg)))

(define (launch-chickadee . args)
  (match args
    ((program (or "--verison" "-v"))
     (display-version-and-exit))
    ((or (program) (program (or "--help" "-h")))
     (display-help-and-exit))
    ((program (? subcommand? subcommand) . args*)
     (apply run-chickadee-command (string->symbol subcommand) args*))
    ((program invalid-subcommand . args*)
     (leave "invalid subcommand: ~A" invalid-subcommand))))