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 wBj7IVXGAWCJfAAA0tVLHw (envelope-from ) for ; Fri, 15 Jan 2021 16:44:05 +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 eIDaHVXGAWAFEQAAbx9fmQ (envelope-from ) for ; Fri, 15 Jan 2021 16:44:05 +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 0126E940483 for ; Fri, 15 Jan 2021 16:44:05 +0000 (UTC) Received: from localhost ([::1]:52232 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l0SCh-0002Yl-Tc for larch@yhetil.org; Fri, 15 Jan 2021 11:44:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58724) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0S9m-00086N-Iq for guix-patches@gnu.org; Fri, 15 Jan 2021 11:41:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:58254) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l0S9m-0000nV-AH for guix-patches@gnu.org; Fri, 15 Jan 2021 11:41:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l0S9m-0006Ne-80 for guix-patches@gnu.org; Fri, 15 Jan 2021 11:41:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#45893] [PATCH 2/2] guix: scripts: Add hint for option typo. Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 15 Jan 2021 16:41: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.161072880624436 (code B ref 45893); Fri, 15 Jan 2021 16:41:02 +0000 Received: (at 45893) by debbugs.gnu.org; 15 Jan 2021 16:40:06 +0000 Received: from localhost ([127.0.0.1]:41565 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0S8r-0006M2-Sa for submit@debbugs.gnu.org; Fri, 15 Jan 2021 11:40:06 -0500 Received: from mail-wr1-f41.google.com ([209.85.221.41]:38631) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0S8n-0006LH-Pg for 45893@debbugs.gnu.org; Fri, 15 Jan 2021 11:40:03 -0500 Received: by mail-wr1-f41.google.com with SMTP id a9so6459216wrt.5 for <45893@debbugs.gnu.org>; Fri, 15 Jan 2021 08:40:01 -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=gRFuJZ81oxeGiRFlPuyyccU0Gx0IMa9uuI0KW5kyrSo=; b=hoO/GbObWc1gGds4Cm+xnqRlGpINPJT/CadHjAkasg1WtRgVvkNxRQSMEXGIDz30VS PciXeaZG0QvkLZ0SAzC6NHTZkzEwKMxDiU2YW6Tj4Yhi09wC0+aeTfBv5OxUrr7idOwD oHXR1iOJJZ21yFxUax4Zmou9rT7kfPVtGoiyudomVCdfo4QmWgiqC2rQDicQ1rnaOnnV b0bnU4CQczL56XLzlz7P6SqeWmV9cJ4V8yk+3Ju3HlITW8bRBIy0d4V1FHpT0mfyaPqW Mwj4t6hzxQ2CCjCrtUdtT5iBG+tAzInE54rv2u1oM4a6AmPbmMW6/5VpXAS9kmpV0USN YamA== 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=gRFuJZ81oxeGiRFlPuyyccU0Gx0IMa9uuI0KW5kyrSo=; b=CJmnuoIKskX6MYCQIxSYn/S+2DRQyfw6GXv4Tasoxer6jKF0iobECCbXeGS9RJ5bBo sP109xHfPWnCSt0MolKMTZrAG5Dr26ekVGWtqYH5w54v8QY8Ye+FxWXKJ5xM49WVesXl b7RdGQKKMMWzphvrn9uc60y3/os0i7xIjFz7v4mcCaIx/Pn4mvqBkCMrqQWE1w8l/x8e hjJyF+FwAfIHRtF8+PhR3iCCeTCKyKEX0CsSl84v9qddHJighGgQsL3B+D2P2zqxWRGa MBxlLG/MnEElfMY2s4PpBhpCHFZJ3JU9bNLlhoF7B4l+EJ0BPKDcs2d2MLpZMeHYsKYE wGiw== X-Gm-Message-State: AOAM530e5KM9NKP4l2vTm6I/M1xDTjbjBQGrZl2MTWNutbS1FAOO/lpq EoeWdoCoRMhJ76oM2WLeso8ndFQB/1A= X-Google-Smtp-Source: ABdhPJwU1/kYPYzZU+yjebRRFKzmu52HrtJ0OU572x4XNPGlbAEQomgtJ2IjBENCLTn05EjMyeSn3w== X-Received: by 2002:adf:fdce:: with SMTP id i14mr14113212wrs.58.1610728796150; Fri, 15 Jan 2021 08:39:56 -0800 (PST) Received: from localhost.localdomain ([193.48.40.110]) by smtp.gmail.com with ESMTPSA id y63sm13744574wmd.21.2021.01.15.08.39.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jan 2021 08:39:55 -0800 (PST) From: zimoun Date: Fri, 15 Jan 2021 17:39:53 +0100 Message-Id: <20210115163953.53721-2-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210115163953.53721-1-zimon.toutoune@gmail.com> References: <20210115163953.53721-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 (headers rsa verify failed) header.d=gmail.com header.s=20161025 header.b=hoO/GbOb; 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: 0126E940483 X-Spam-Score: -1.26 X-Migadu-Scanner: scn1.migadu.com X-TUID: kaWgj9Xlggv0 * 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 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen ;;; Copyright © 2021 Ricardo Wurmus +;;; Copyright © 2021 Simon Tournier ;;; ;;; 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