From 5e732379f4fda688da3532d635b11a50dd0dc5b7 Mon Sep 17 00:00:00 2001 From: jangko Date: Tue, 2 Nov 2021 18:04:48 +0700 Subject: [PATCH] feature: suggest nearest match if user give unrecognized option similar to git `Did you mean '...'?`, this feature put `std/editDistance` into action help users to quickly recognize his error instead of plain `Unrecognized option '...'.` --- confutils.nim | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/confutils.nim b/confutils.nim index 9383e88..2f80881 100644 --- a/confutils.nim +++ b/confutils.nim @@ -1,5 +1,5 @@ import - std/[options, strutils, wordwrap], + std/[options, strutils, wordwrap, editdistance], stew/shims/macros, confutils/[defs, cli_parser, config_file] @@ -409,6 +409,36 @@ proc findSubCmd(cmd: CmdInfo, name: string): CmdInfo = return nil +proc distanceOpt(opts: openarray[OptInfo], name: string): (OptInfo, int) = + # find nearest match using `editDistance` + if opts.len == 0: + return + + var + currOpt = opts[0] + currDist = editDistance(currOpt.name, name) + + for i in 1..