(define-module (izicli)) (use-modules (srfi srfi-1)) ;;; izicli (define (path-display path) (let loop ((path path)) (unless (null? path) (display (car path)) (display " ") (loop (cdr path)))) (display #\newline)) (define (%spec-help path) (lambda (spec) (if (procedure? (cadr spec)) (path-display (append path (list (car spec)))) (for-each (%spec-help (append path (list (car spec)))) (cdr spec))))) (define (spec-help name spec) "Display the usage message for this SPEC for the program named NAME" (format #t "Usage:\n\n") (for-each (%spec-help (list name)) spec)) (define (help? string) (or (string=? string "--help") (string=? string "-h"))) (define (%lookup spec args) (if (string=? (symbol->string (car spec)) (car args)) (values (cdr spec) (cdr args)) (values #f #f))) (define (lookup spec args) (let loop ((spec spec)) (if (null? spec) (values #f #f) (call-with-values (lambda () (%lookup (car spec) args)) (lambda (procedure-or-spec args) (cond ((and (not procedure-or-spec) (not args)) (loop (cdr spec))) ((procedure? (car procedure-or-spec)) (values (car procedure-or-spec) args)) (else (lookup procedure-or-spec args)))))))) (define-public (izicli name spec) (let ((args (cdr (program-arguments)))) (if (or (null? args) (equal? args '("--help")) (equal? args '("-h"))) (spec-help name spec) (call-with-values (lambda () (lookup spec args)) (lambda (procedure args) (cond ((and procedure (find help? args)) (display (procedure-documentation procedure))) ((and procedure args) (procedure args)) (else (spec-help name spec)))))))) ;;; example (define (package-install args) "Usage: xote package install [-ef] PACKAGE --expression -e Interpret PACKAGE as Guile expression and install the result of its evaluation. --file -f Interpret PACKAGE as a filename and install the package by evaluating it. Install package in your current environment prolly your profile " (pk 'package-install args)) (define (package-search args) "Usage: xote package search [-rsa] QUERY... --regex -r Interpret query as a regular expression --synopsis -s Consider synopsis during the search too --all -a Consider synopsis and description during the search Lookup the package index for package that match the given QUERY which might be multiple words. " (pk 'package-search args)) (define (system-init args) (pk 'system-init args)) (define (system-reconfigure arg) (pk 'system-reconfigure args)) (define (system-generation-switch args) (pk 'system-generation-switch args)) (define (system-generation-list args) (pk 'system-generation-list args)) (define xote `((package (install ,package-install) (search ,package-search)) (system (init ,system-init) (reconfigure ,system-reconfigure) (generation (switch ,system-generation-switch) (list ,system-generation-list))))) (izicli 'xote xote)