Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ private static ValidationResult ValidateNoUnknownOptions(
// parsing is already case-sensitive but a case-insensitive lookup is a strict superset and
// does not change CLI behavior.
var validOptionNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var visibleOptionNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (KeyValuePair<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> provider in extensionOptionsByProvider)
{
foreach (CommandLineOption option in provider.Value)
{
validOptionNames.Add(option.Name);
if (!option.IsHidden)
{
visibleOptionNames.Add(option.Name);
}
Comment thread
Evangelink marked this conversation as resolved.
Resolved
}
}

Expand All @@ -34,6 +39,10 @@ private static ValidationResult ValidateNoUnknownOptions(
foreach (CommandLineOption option in provider.Value)
{
validOptionNames.Add(option.Name);
if (!option.IsHidden)
{
visibleOptionNames.Add(option.Name);
}
}
}

Expand All @@ -43,7 +52,7 @@ private static ValidationResult ValidateNoUnknownOptions(
if (!validOptionNames.Contains(optionRecord.Name))
{
stringBuilder ??= new();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineUnknownOption, optionRecord.Name));
AppendUnknownOptionError(stringBuilder, optionRecord.Name, visibleOptionNames);
}
}

Expand All @@ -58,7 +67,9 @@ private static ValidationResult ValidateNoUnknownOptions(
if (!validOptionNames.Contains(entry.OptionName))
{
stringBuilder ??= new();
string innerError = string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineUnknownOption, entry.OptionName);
StringBuilder innerErrorBuilder = new();
AppendUnknownOptionError(innerErrorBuilder, entry.OptionName, visibleOptionNames);
string innerError = innerErrorBuilder.ToTrimmedString();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.JsonCommandLineOptionsValidationErrorPrefix, innerError));
}
}
Expand All @@ -69,6 +80,94 @@ private static ValidationResult ValidateNoUnknownOptions(
: ValidationResult.Valid();
}

private static void AppendUnknownOptionError(
StringBuilder stringBuilder,
string unknownOptionName,
HashSet<string> visibleOptionNames)
{
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineUnknownOption, unknownOptionName));

if (FindSuggestedOption(unknownOptionName, visibleOptionNames) is not { } suggestedOptionName)
{
return;
}

stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionSuggestion, suggestedOptionName));
Comment thread
Evangelink marked this conversation as resolved.
Resolved
}

private static string? FindSuggestedOption(string unknownOptionName, HashSet<string> candidateOptionNames)
{
int maximumDistance = unknownOptionName.Length switch
{
<= 4 => 1,
<= 12 => 2,
_ => 3,
};

string? bestCandidate = null;
int bestDistance = maximumDistance + 1;
bool hasAmbiguousBestCandidate = false;

foreach (string candidateOptionName in candidateOptionNames)
{
if (Math.Abs(candidateOptionName.Length - unknownOptionName.Length) > maximumDistance)
{
continue;
}

int distance = CalculateEditDistance(unknownOptionName, candidateOptionName);
if (distance < bestDistance)
{
bestCandidate = candidateOptionName;
bestDistance = distance;
hasAmbiguousBestCandidate = false;
}
else if (distance == bestDistance)
{
hasAmbiguousBestCandidate = true;
}
}

return bestDistance <= maximumDistance && !hasAmbiguousBestCandidate
? bestCandidate
: null;
}

private static int CalculateEditDistance(string source, string target)
{
int[,] distances = new int[source.Length + 1, target.Length + 1];
for (int i = 0; i <= source.Length; i++)
{
distances[i, 0] = i;
}

for (int j = 0; j <= target.Length; j++)
{
distances[0, j] = j;
}

for (int i = 1; i <= source.Length; i++)
{
for (int j = 1; j <= target.Length; j++)
{
int substitutionCost = char.ToUpperInvariant(source[i - 1]) == char.ToUpperInvariant(target[j - 1]) ? 0 : 1;
distances[i, j] = Math.Min(
Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1),
distances[i - 1, j - 1] + substitutionCost);

if (i > 1
&& j > 1
&& char.ToUpperInvariant(source[i - 1]) == char.ToUpperInvariant(target[j - 2])
&& char.ToUpperInvariant(source[i - 2]) == char.ToUpperInvariant(target[j - 1]))
{
distances[i, j] = Math.Min(distances[i, j], distances[i - 2, j - 2] + 1);
}
}
}

return distances[source.Length, target.Length];
}

private static ValidationResult ValidateNoBootstrapOnlyOptionsInJson(
IReadOnlyList<JsonCommandLineOptionEntry>? jsonCommandLineOptions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@
<value>Unknown option '--{0}'</value>
<comment>{0} is the unknown option name without the leading dashes.</comment>
</data>
<data name="CommandLineOptionSuggestion" xml:space="preserve">
<value>Did you mean '--{0}'?</value>
<comment>{0} is the suggested option name without the leading dashes.</comment>
</data>
<data name="InvalidCommandLineArguments" xml:space="preserve">
<value>Invalid command line arguments:</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">Možnost --{0} od zprostředkovatele {1} (UID: {2}) používá vyhrazenou předponu --internal.</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">Rozhraní ICommandLineOptions ještě není sestavené.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">Die Option "--{0}" vom Anbieter "{1}" (UID: {2}) verwendet das reservierte Präfix "--internal".</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions wurde noch nicht erstellt.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">La opción “--{0}” del proveedor “{1}” (UID: {2}) usa el prefijo reservado “--internal”</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions aún no se ha compilado.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">L’option « --{0} » du fournisseur « {1} » (UID : {2}) utilise le préfixe réservé « --internal »</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions n’a pas encore été généré.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">L'opzione '--{0}' del provider '{1}' (UID: {2}) usa il prefisso riservato '--internal'</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions non è stato ancora compilato.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">プロバイダー '{1}' のオプション '--{0}' (UID:{2}) は予約済みプレフィックス '--internal' を 使用しています</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions はまだ構築されていません。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">공급자 '{1}'(UID: {2})의 옵션 '-- {0}'이 예약된 접두사 '--internal'을 사용하고 있습니다.</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions가 아직 빌드되지 않았습니다.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">Opcja „--{0}” od dostawcy „{1}” (UID: {2}) używa zastrzeżonego prefiksu „--internal”</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">Obiekt ICommandLineOptions nie został jeszcze skompilowany.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">A opção '--{0}' do provedor '{1}' (UID: {2}) está usando o prefixo reservado '--internal'</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">O ICommandLineOptions ainda não foi criado.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">Параметр "--{0}" от поставщика "{1}" (UID: {2}) использует зарезервированный префикс "--internal"</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">Параметр ICommandLineOptions еще не создан.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">'{1}' sağlayıcısındaki (UID: {2}) `--{0}` seçeneği ayrılmış '--internal' önekini kullanıyor</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions henüz derlenmedi.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">来自提供程序“{1}” (UID: {2}) 的选项“--{0}”正在使用保留前缀“--internal”</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">ICommandLineOptions 尚未生成。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<target state="translated">提供者 '{1}' (UID: {2}) 中的選項 '--{0}' 使用保留的前置詞 '--internal'</target>
<note>{0} is the option name without the leading dashes. {1} is the provider display name. {2} is the provider UID. {Locked="--internal"}</note>
</trans-unit>
<trans-unit id="CommandLineOptionSuggestion">
<source>Did you mean '--{0}'?</source>
<target state="new">Did you mean '--{0}'?</target>
<note>{0} is the suggested option name without the leading dashes.</note>
</trans-unit>
<trans-unit id="CommandLineOptionsNotReady">
<source>The ICommandLineOptions has not been built yet.</source>
<target state="translated">尚未建置 ICommandLineOptions。</target>
Expand Down
Loading
Loading