diff --git a/metaconfig-core/shared/src/main/scala/metaconfig/internal/Levenshtein.scala b/metaconfig-core/shared/src/main/scala/metaconfig/internal/Levenshtein.scala index dfd07e06..69ed4580 100644 --- a/metaconfig-core/shared/src/main/scala/metaconfig/internal/Levenshtein.scala +++ b/metaconfig-core/shared/src/main/scala/metaconfig/internal/Levenshtein.scala @@ -13,7 +13,17 @@ object Levenshtein { else None // Don't return candidate when difference is large. } val result = candidatesWithRatio.sortBy(_._2).headOption.map(_._1) - result + result.orElse(prefixCandidate(query, candidates)) + } + + private def prefixCandidate( + query: String, + candidates: Seq[String] + ): Option[String] = { + val prefixCandidates = candidates.flatMap { candidate => + if (candidate.startsWith(query)) Some(candidate) else None + } + Option(prefixCandidates).filter(_.length == 1).map(_.head) } /** Levenshtein distance. Implementation based on Wikipedia's algorithm. */ diff --git a/metaconfig-tests/jvm/src/test/scala/metaconfig/cli/SubcommandSuite.scala b/metaconfig-tests/jvm/src/test/scala/metaconfig/cli/SubcommandSuite.scala index aa0a4397..12fab986 100644 --- a/metaconfig-tests/jvm/src/test/scala/metaconfig/cli/SubcommandSuite.scala +++ b/metaconfig-tests/jvm/src/test/scala/metaconfig/cli/SubcommandSuite.scala @@ -160,6 +160,7 @@ class SubcommandSuite extends FunSuite { checkError( List("test", "--max=40"), """|error: found argument '--max' which wasn't expected, or isn't valid in this context. + | Did you mean '--max-count'? |""".stripMargin ) checkError(