unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: zimoun <zimon.toutoune@gmail.com>
To: 45893@debbugs.gnu.org
Subject: [bug#45893] [PATCH 2/2] guix: scripts: Add hint for option typo.
Date: Fri, 15 Jan 2021 17:39:53 +0100	[thread overview]
Message-ID: <20210115163953.53721-2-zimon.toutoune@gmail.com> (raw)
In-Reply-To: <20210115163953.53721-1-zimon.toutoune@gmail.com>

* guix/scripts.scm (levenshtein-distance): New procedure.
(options->long-names): New procedure.
(option-hint): New procedure.
---
 guix/scripts.scm | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/guix/scripts.scm b/guix/scripts.scm
index 34cba35401..f40eadfedd 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.
 ;;;
@@ -31,6 +32,7 @@
   #:use-module ((guix profiles) #:select (%profile-directory))
   #:autoload   (guix describe) (current-profile-date)
   #:use-module (guix build syscalls)
+  #:use-module (guix memoization)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-19)
   #:use-module (srfi srfi-37)
@@ -112,6 +114,61 @@ procedure, but both the category and synopsis are meant to be read (parsed) by
          doc
          body ...)))))
 
+(define (levenshtein-distance s1 s2)
+  "Compute the Levenshtein distance between two strings."
+  ;; Naive implemenation
+  (define loop
+    (memoize
+     (lambda (as bt)
+       (match as
+         ('() (length bt))
+         (_ (match bt
+              ('() (length as))
+              (_
+               (let ((a (car as))
+                     (s (cdr as))
+                     (b (car bt))
+                     (t (cdr bt)))
+                 (if (char=? a b)
+                     (loop s t)
+                     (1+ (min
+                          (loop as t)
+                          (loop s bt)
+                          (loop s t))))))))))))
+
+  (let ((c1 (string->list s1))
+        (c2 (string->list s2)))
+    (loop c1 c2)))
+
+(define (options->long-names options)
+  "Return long names from options."
+  (fold (lambda (name res)
+          (match name
+            ((? char?) res)
+            ((? string?) (cons name res))))
+        '()
+        (fold append '() (map option-names options))))
+
+(define (option-hint name options)
+  "Return the closest long-name from name based on Levenshtein distance."
+  (fold (lambda (name res)
+          (if (string-null? res)
+              name
+              (string-append name " or " res)))
+        ""
+        (cadr (fold (lambda (long-name res)
+                     (let ((dist (levenshtein-distance name long-name)))
+                       (match res
+                         ((val lst)
+                          (if (< dist val)
+                              (list dist (list long-name))
+                              (if (= dist val)
+                                  (list dist (cons long-name lst))
+                                  res)))
+                         (_ (list dist (list long-name))))))
+                   '()
+                   (options->long-names options)))))
+
 (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,6 +206,7 @@ parameter of 'args-fold'."
     ;; Actual parsing takes place here.
     (apply args-fold* args options
            (lambda (opt name arg . rest)
+             (display-hint (format #f (G_ "Do you mean @code{~a}?~%") (option-hint name options)))
              (leave (G_ "~A: unrecognized option~%") name))
            argument-handler
            seeds))
-- 
2.29.2





  reply	other threads:[~2021-01-15 16:44 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-15 16:37 [bug#45893] [PATCH 0/2] DRAFT: Hint for options zimoun
2021-01-15 16:39 ` [bug#45893] [PATCH 1/2] scripts: search, show: Replace 'args-fold*' by 'parse-command-line' zimoun
2021-01-15 16:39   ` zimoun [this message]
2021-01-19 17:20   ` [bug#45893] [PATCH 0/2] DRAFT: Hint for options Ludovic Courtès
2021-01-19 17:35     ` zimoun
2021-01-16  0:09 ` [bug#45893] [PATCH v2 0/3] DRAFT: Hint command line typo zimoun
2021-01-16  0:26   ` [bug#45893] [PATCH v2 1/3] scripts: search, show: Replace 'args-fold*' by 'parse-command-line' zimoun
2021-01-16  0:26     ` [bug#45893] [PATCH v2 2/3] guix: scripts: Add hint for option typo zimoun
2021-01-19 17:31       ` [bug#45893] [PATCH 0/2] DRAFT: Hint for options Ludovic Courtès
2021-01-16  0:26     ` [bug#45893] [PATCH v2 3/3] ui: Add command hint zimoun
2021-01-19 17:38       ` [bug#45893] [PATCH 0/2] DRAFT: Hint for options Ludovic Courtès
2021-01-19 18:01         ` zimoun
2021-01-26 20:53           ` Ludovic Courtès
2021-01-26 21:27             ` zimoun
2021-01-19 23:59         ` [bug#45893] Hint for package name: too slow! zimoun
2021-01-20  9:49           ` [bug#45893] Hint for package name: full matrix iteration zimoun
2021-01-26 21:00           ` [bug#45893] [PATCH 0/2] DRAFT: Hint for options Ludovic Courtès
2021-01-26 21:44             ` zimoun
2021-01-27 22:09               ` Ludovic Courtès
2021-01-19 21:28 ` [bug#45893] [PATCH v3 1/3] utils: Add string distance zimoun
2021-01-19 21:28   ` [bug#45893] [PATCH v3 2/3] guix: scripts: Add hint for option typo zimoun
2021-01-19 21:28   ` [bug#45893] [PATCH v3 3/3] ui: Add hint for command typo zimoun
2021-01-26 21:18   ` [bug#45893] [PATCH 0/2] DRAFT: Hint for options Ludovic Courtès
2021-01-26 21:20   ` Ludovic Courtès
2021-01-26 22:05     ` zimoun
2021-02-03 11:28       ` bug#45893: " Ludovic Courtès
2021-02-03 12:15         ` [bug#45893] " zimoun
     [not found]   ` <YBxQxSO57N4kV4N0@jasmine.lan>
     [not found]     ` <86tuqrbjxr.fsf@gmail.com>
2021-02-04 23:08       ` [bug#45893] Typo helper doesn't always know which command is missing zimoun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210115163953.53721-2-zimon.toutoune@gmail.com \
    --to=zimon.toutoune@gmail.com \
    --cc=45893@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).