summary refs log tree commit diff
diff options
context:
space:
mode:
authorzimoun <zimon.toutoune@gmail.com>2021-01-19 22:28:09 +0100
committerLudovic Courtès <ludo@gnu.org>2021-02-03 12:41:28 +0100
commit0df4d5aa04b53e8a6842045deb323b785db5b20a (patch)
tree5eb76b5cfea9706ca4df497d5aefe95212971d81
parent9505b54a4f9f0265c9d8be53763f0c59d6f62a44 (diff)
downloadguix-0df4d5aa04b53e8a6842045deb323b785db5b20a.tar.gz
guix: scripts: Add hint for option typo.
* guix/scripts.scm (option-hint): New procedure.
(parse-command-line): Add 'option-hint'.

Co-authored-by: Ludovic Courtès <ludo@gnu.org>
-rw-r--r--guix/scripts.scm15
1 files changed, 14 insertions, 1 deletions
diff --git a/guix/scripts.scm b/guix/scripts.scm
index 34cba35401..c9ea9f2e29 100644
--- a/guix/scripts.scm
+++ b/guix/scripts.scm
@@ -4,6 +4,7 @@
 ;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
 ;;; Copyright © 2021 Ricardo Wurmus <rekado@elephly.net>
+;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -112,6 +113,13 @@ procedure, but both the category and synopsis are meant to be read (parsed) by
          doc
          body ...)))))
 
+(define (option-hint guess options)
+  "Return the closest long-name OPTIONS from GUESS,
+according to'string-distance'."
+  (define (options->long-names options)
+    (filter string? (append-map option-names options)))
+  (string-closest guess (options->long-names options) #:threshold 3))
+
 (define (args-fold* args options unrecognized-option-proc operand-proc . seeds)
   "A wrapper on top of `args-fold' that does proper user-facing error
 reporting."
@@ -149,7 +157,12 @@ parameter of 'args-fold'."
     ;; Actual parsing takes place here.
     (apply args-fold* args options
            (lambda (opt name arg . rest)
-             (leave (G_ "~A: unrecognized option~%") name))
+             (let ((hint (option-hint name options)))
+               (report-error (G_ "~A: unrecognized option~%") name)
+               (when hint
+                 (display-hint
+                  (format #f (G_ "Did you mean @code{~a}?~%") hint)))
+               (exit 1)))
            argument-handler
            seeds))