GNU bug report logs - #45893
[PATCH 0/2] DRAFT: Hint for options.

Previous Next

Package: guix-patches;

Reported by: zimoun <zimon.toutoune <at> gmail.com>

Date: Fri, 15 Jan 2021 16:38:01 UTC

Severity: normal

Tags: patch

Done: Ludovic Courtès <ludo <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


Message #29 received at 45893 <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: zimoun <zimon.toutoune <at> gmail.com>
Cc: 45893 <at> debbugs.gnu.org
Subject: Re: bug#45893: [PATCH 0/2] DRAFT: Hint for options.
Date: Tue, 19 Jan 2021 18:31:25 +0100
zimoun <zimon.toutoune <at> gmail.com> skribis:

> * guix/utils.scm (levenshtein-distance): New procedure.
> (string-closest): New procedure.
> * guix/scripts.scm (option-hint): New procedure.
> (parse-command-line): Add 'option-hint'.

Yay!

> +(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))))

I think this can be simplified a bit:

  options->long-names = (filter string? (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))

[...]

> +(define (levenshtein-distance s1 s2)
> +  "Compute the Levenshtein distance between two strings."

Maybe call it ‘string-distance’?

> +  ;; Naive implemenation
> +  (define loop
> +    (memoize
> +     (lambda (as bt)

Instead of (memoize (lambda …)), you can write:

  (mlambda (str1 str2) …)

> +       (match as
> +         ('() (length bt))

The pattern for the empty list is (), not '().

How about making this addition to (guix utils) a commit of its own, and
to add a small test in tests/utils.scm?

> +(define (string-closest trial tests)
> +  "Return the list from TESTS the closest from the string TRIAL based on
> +Levenshtein distance."

Maybe something like: “Return the string from TESTS that is the closest
from TRIAL, according to 'string-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)))))

You can simplify this a bit by using ‘fold2’, which allows you to pass
two seeds instead of one:

  (fold2 (lambda (test closest shortest-distance)
           …)
         "" +inf.0
         tests)

It returns two values and the first one is the string.

Ludo’.




This bug report was last modified 4 years and 106 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.