From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id KMYjB/YyAmBaPQAA0tVLHw (envelope-from ) for ; Sat, 16 Jan 2021 00:27:34 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id ADHRAvYyAmCyYgAAbx9fmQ (envelope-from ) for ; Sat, 16 Jan 2021 00:27:34 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 4E512940483 for ; Sat, 16 Jan 2021 00:27:33 +0000 (UTC) Received: from localhost ([::1]:59480 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l0ZRE-0005gh-AZ for larch@yhetil.org; Fri, 15 Jan 2021 19:27:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42926) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0ZQl-0005Uw-4j for guix-patches@gnu.org; Fri, 15 Jan 2021 19:27:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:58687) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l0ZQk-00048I-Rh for guix-patches@gnu.org; Fri, 15 Jan 2021 19:27:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l0ZQk-0004zN-Nq for guix-patches@gnu.org; Fri, 15 Jan 2021 19:27:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#45893] [PATCH v2 2/3] guix: scripts: Add hint for option typo. Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 16 Jan 2021 00:27:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45893 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 45893@debbugs.gnu.org Received: via spool by 45893-submit@debbugs.gnu.org id=B45893.161075681419144 (code B ref 45893); Sat, 16 Jan 2021 00:27:02 +0000 Received: (at 45893) by debbugs.gnu.org; 16 Jan 2021 00:26:54 +0000 Received: from localhost ([127.0.0.1]:41996 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0ZQc-0004yi-4K for submit@debbugs.gnu.org; Fri, 15 Jan 2021 19:26:54 -0500 Received: from mail-wr1-f42.google.com ([209.85.221.42]:44321) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0ZQa-0004yH-Ck for 45893@debbugs.gnu.org; Fri, 15 Jan 2021 19:26:52 -0500 Received: by mail-wr1-f42.google.com with SMTP id w5so10911797wrm.11 for <45893@debbugs.gnu.org>; Fri, 15 Jan 2021 16:26:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=I7VQJBXPfHyF+i0beuahnjKJIC2NPAMgEmH0FrDiPa8=; b=pswU8U7qFsIBtc3LSaUAyLKwh3ZJxEqosjJqtNx2R4npsStlCf/wHuqbswDXjg4cX3 mgpBZsw3Lv0ZQFF63Ola0buidQWht0MZEBwaGAQ2psVsbQNI8dzuU5wATMotjeqdl7r2 60ovLRjurFPEmsCFalAJJ7zVSeG+1rpy72/LecYAyDOuel1ttu4ZdaYtxF24N+RnMp50 u/W+UvpcUwUap3bVM62BXxFCLoIm2JdwrzxQuHd/vzk9q8/zIZrCi53H3s9iws0m7u05 jbQl6gNuMBl1Sa8e5AC2/C6j8p/fBByH7++hHQiSLP0DS/0yVSiGCV0pxMCZVAUhloHq uCmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=I7VQJBXPfHyF+i0beuahnjKJIC2NPAMgEmH0FrDiPa8=; b=plJHGPDyDSFd85gPBnMeW3F60OLapXIt3PzqrxLjUsK74AAPj4E6DKrdgOuR3j+wXc Ff9m6C1K2y93nR9pqVkv1oswXWFYErMiehHktHzlsC7Pg66M3/1cqGZw4oq9l5GhvGKb EiRm0Y4JdGFIOBUemXAsfkeIp+BS3QMSNBADc8il6dgqSvzCFy4o4xTVwFTC/AaXiqU6 XH5WVm0KrRB3AaTSuVutWpcbNlHdxBdw0DeJOHGD/pQfWQJpzaLzvUZgAtxUiHOMTSJr 5ME1n/lOAxi6Z1XydRS+Ix8FMzEwANJuqi4sVrjjUA9BWkp2+HqDLyvGbhmN94Lsa4Lw BOig== X-Gm-Message-State: AOAM533Qi+1/xkBiY7d1IU55fTIQXxb/iIGxmmM/r50Or/qB3XVmQwYc OqpfEfPYZ/cXAtPk7f8yEwLoyH82oCg= X-Google-Smtp-Source: ABdhPJxbzhUBh9Z1d6Uj1AUFBsHCuS89bpN2xfUPvj3rmxXzAIdx7DFUvo3bZfQucAZOoXYPriUw+g== X-Received: by 2002:adf:ab56:: with SMTP id r22mr15722154wrc.351.1610756806763; Fri, 15 Jan 2021 16:26:46 -0800 (PST) Received: from lili.univ-paris-diderot.fr ([2a01:e0a:59b:9120:65d2:2476:f637:db1e]) by smtp.gmail.com with ESMTPSA id u16sm4797475wrn.68.2021.01.15.16.26.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jan 2021 16:26:46 -0800 (PST) From: zimoun Date: Sat, 16 Jan 2021 01:26:33 +0100 Message-Id: <20210116002634.10401-2-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210116002634.10401-1-zimon.toutoune@gmail.com> References: <865z3xlp2y.fsf@gmail.com> <20210116002634.10401-1-zimon.toutoune@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -1.26 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20161025 header.b=pswU8U7q; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 4E512940483 X-Spam-Score: -1.26 X-Migadu-Scanner: scn0.migadu.com X-TUID: BvG+ffo/Ejh3 * guix/utils.scm (levenshtein-distance): New procedure. (string-closest): New procedure. * guix/scripts.scm (option-hint): New procedure. (parse-command-line): Add 'option-hint'. --- guix/scripts.scm | 21 ++++++++++++++++++++ guix/utils.scm | 51 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/guix/scripts.scm b/guix/scripts.scm index 34cba35401..03d45c0888 100644 --- a/guix/scripts.scm +++ b/guix/scripts.scm @@ -4,6 +4,7 @@ ;;; Copyright © 2015, 2016 Alex Kost ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen ;;; Copyright © 2021 Ricardo Wurmus +;;; Copyright © 2021 Simon Tournier ;;; ;;; This file is part of GNU Guix. ;;; @@ -112,6 +113,23 @@ 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 from name based on Levenshtein distance." + (define (options->long-names options) + (fold (lambda (name res) + (match name + ((? char?) res) + ((? string?) (cons name res)))) + '() + (fold append '() (map option-names options)))) + + (fold (lambda (name res) + (if (string-null? res) + (string-append "@code{" name "}") + (string-append "@code{" name "}, " res))) + "" + (string-closest guess (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 +167,9 @@ 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 ~a?~%") + (option-hint name options))) (leave (G_ "~A: unrecognized option~%") name)) argument-handler seeds)) diff --git a/guix/utils.scm b/guix/utils.scm index f8b05e7e80..2a0fb28917 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -8,6 +8,7 @@ ;;; Copyright © 2017 Mathieu Othacehe ;;; Copyright © 2018, 2020 Marius Bakke ;;; Copyright © 2020 Efraim Flashner +;;; Copyright © 2021 Simon Tournier ;;; ;;; This file is part of GNU Guix. ;;; @@ -114,7 +115,10 @@ call-with-decompressed-port compressed-output-port call-with-compressed-output-port - canonical-newline-port)) + canonical-newline-port + + levenshtein-distance + string-closest)) ;;; @@ -847,6 +851,51 @@ be determined." ;; raising an error would upset Geiser users #f)))))) + +;;; +;;; Hint based on Levenshtein distance +;;; + +(define (levenshtein-distance s1 s2) + "Compute the Levenshtein distance between two strings." + ;; Naive implemenation + (define loop + (memoize + (lambda (as bt) + (match as + ('() (length bt)) + ((a s ...) + (match bt + ('() (length as)) + ((b t ...) + (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 (string-closest trial tests) + "Return the list from TESTS the closest from the string TRIAL based on +Levenshtein distance." + (match (fold (lambda (test res) + (let ((dist (levenshtein-distance trial test))) + (match res + ((val lst) + (if (< dist val) + (list dist (list test)) + (if (= dist val) + (list dist (cons test lst)) + res))) + (_ (list dist (list test)))))) + '() + tests) + ((_ rest ...) (match rest ((head _ ...) head))))) + ;;; Local Variables: ;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1) ;;; End: -- 2.29.2