Skip to content

Commit 0b720d0

Browse files
authored
Include name of argument when an argument is missing (#2747)
* Include name of argument when an argument is missing * Review: Test for multiple missing arguments * Review: Use HelpName (falling back to Name since HelpName is nullable)
1 parent a3bae7d commit 0b720d0

18 files changed

+60
-34
lines changed

src/System.CommandLine.Tests/LocalizationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ namespace System.CommandLine.Tests
77
public class LocalizationTests
88
{
99
private const string CommandName = "the-command";
10+
private const string ArgumentName = "arg";
1011

1112
[Theory]
12-
[InlineData("es", $"Falta el argumento requerido para el comando: '{CommandName}'.")]
13-
[InlineData("en-US", $"Required argument missing for command: '{CommandName}'.")]
13+
[InlineData("es", $"Falta el argumento requerido '{ArgumentName}' para el comando: '{CommandName}'.")]
14+
[InlineData("en-US", $"Required argument '{ArgumentName}' missing for command: '{CommandName}'.")]
1415
public void ErrorMessages_AreLocalized(string cultureName, string expectedMessage)
1516
{
1617
CultureInfo uiCultureBefore = CultureInfo.CurrentUICulture;

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,29 @@ public void When_option_arguments_are_greater_than_maximum_arity_then_an_error_i
16261626
.Contain(LocalizationResources.UnrecognizedCommandOrArgument("4"));
16271627
}
16281628

1629+
[Fact]
1630+
public void When_required_argument_is_missing_then_an_error_is_returned()
1631+
{
1632+
var command = new Command("the-command")
1633+
{
1634+
new Argument<string>("arg0"),
1635+
new Argument<string>("missing-arg1"),
1636+
new Argument<string>("missing-arg2")
1637+
};
1638+
1639+
var result = command.Parse("value-for-arg1");
1640+
1641+
result.Errors
1642+
.Select(e => e.Message)
1643+
.Should()
1644+
.BeEquivalentTo(
1645+
// "Required argument 'missing-arg1' missing for command: 'the-command'."
1646+
LocalizationResources.RequiredArgumentMissing(result.GetResult(command.Arguments[1])),
1647+
// "Required argument 'missing-arg2' missing for command: 'the-command'."
1648+
LocalizationResources.RequiredArgumentMissing(result.GetResult(command.Arguments[2]))
1649+
);
1650+
}
1651+
16291652
[Fact]
16301653
public void Tokens_are_not_split_if_the_part_before_the_delimiter_is_not_an_option()
16311654
{

src/System.CommandLine/LocalizationResources.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ internal static string InvalidCharactersInFileName(char invalidChar) =>
5050
GetResourceString(Properties.Resources.InvalidCharactersInFileName, invalidChar);
5151

5252
/// <summary>
53-
/// Interpolates values into a localized string similar to Required argument missing for command: {0}.
53+
/// Interpolates values into a localized string similar to Required argument '{0}' missing for command: '{1}'.
5454
/// </summary>
5555
internal static string RequiredArgumentMissing(ArgumentResult argumentResult) =>
5656
argumentResult.Parent is CommandResult commandResult
57-
? GetResourceString(Properties.Resources.CommandRequiredArgumentMissing, commandResult.IdentifierToken.Value)
58-
: RequiredArgumentMissing((OptionResult)argumentResult.Parent!);
57+
? GetResourceString(Properties.Resources.CommandRequiredArgumentMissing,
58+
argumentResult.Argument.HelpName ?? argumentResult.Argument.Name,
59+
commandResult.IdentifierToken.Value)
60+
: RequiredArgumentMissing((OptionResult) argumentResult.Parent!);
5961

6062
/// <summary>
6163
/// Interpolates values into a localized string similar to Required argument missing for option: {0}.

src/System.CommandLine/Properties/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
<value>Character not allowed in a path: '{0}'.</value>
134134
</data>
135135
<data name="CommandRequiredArgumentMissing" xml:space="preserve">
136-
<value>Required argument missing for command: '{0}'.</value>
136+
<value>Required argument '{0}' missing for command: '{1}'.</value>
137137
</data>
138138
<data name="OptionRequiredArgumentMissing" xml:space="preserve">
139139
<value>Required argument missing for option: '{0}'.</value>

src/System.CommandLine/Properties/xlf/Resources.cs.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<note />
2929
</trans-unit>
3030
<trans-unit id="CommandRequiredArgumentMissing">
31-
<source>Required argument missing for command: '{0}'.</source>
32-
<target state="translated">Chybí povinný argument pro příkaz: '{0}'.</target>
31+
<source>Required argument '{0}' missing for command: '{1}'.</source>
32+
<target state="translated">Chybí povinný argument '{0}' pro příkaz: '{1}'.</target>
3333
<note />
3434
</trans-unit>
3535
<trans-unit id="DirectoryDoesNotExist">

src/System.CommandLine/Properties/xlf/Resources.de.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<note />
2929
</trans-unit>
3030
<trans-unit id="CommandRequiredArgumentMissing">
31-
<source>Required argument missing for command: '{0}'.</source>
32-
<target state="translated">Ein erforderliches Argument fehlt für den Befehl: '{0}'.</target>
31+
<source>Required argument '{0}' missing for command: '{1}'.</source>
32+
<target state="translated">Ein erforderliches Argument '{0}' fehlt für den Befehl: '{1}'.</target>
3333
<note />
3434
</trans-unit>
3535
<trans-unit id="DirectoryDoesNotExist">

src/System.CommandLine/Properties/xlf/Resources.es.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<note />
2929
</trans-unit>
3030
<trans-unit id="CommandRequiredArgumentMissing">
31-
<source>Required argument missing for command: '{0}'.</source>
32-
<target state="translated">Falta el argumento requerido para el comando: '{0}'.</target>
31+
<source>Required argument '{0}' missing for command: '{1}'.</source>
32+
<target state="translated">Falta el argumento requerido '{0}' para el comando: '{1}'.</target>
3333
<note></note>
3434
</trans-unit>
3535
<trans-unit id="DirectoryDoesNotExist">

src/System.CommandLine/Properties/xlf/Resources.fi.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<note />
2929
</trans-unit>
3030
<trans-unit id="CommandRequiredArgumentMissing">
31-
<source>Required argument missing for command: '{0}'.</source>
32-
<target state="translated">Pakollinen argumentti puuttuu komennolta: '{0}'.</target>
31+
<source>Required argument '{0}' missing for command: '{1}'.</source>
32+
<target state="translated">Pakollinen argumentti '{0}' puuttuu komennolta: '{1}'.</target>
3333
<note />
3434
</trans-unit>
3535
<trans-unit id="DirectoryDoesNotExist">

src/System.CommandLine/Properties/xlf/Resources.fr.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<note />
2929
</trans-unit>
3030
<trans-unit id="CommandRequiredArgumentMissing">
31-
<source>Required argument missing for command: '{0}'.</source>
32-
<target state="translated">Argument obligatoire manquant pour la commande : '{0}'.</target>
31+
<source>Required argument '{0}' missing for command: '{1}'.</source>
32+
<target state="translated">Argument obligatoire '{0}' manquant pour la commande : '{1}'.</target>
3333
<note />
3434
</trans-unit>
3535
<trans-unit id="DirectoryDoesNotExist">

src/System.CommandLine/Properties/xlf/Resources.it.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<note />
2929
</trans-unit>
3030
<trans-unit id="CommandRequiredArgumentMissing">
31-
<source>Required argument missing for command: '{0}'.</source>
32-
<target state="translated">Manca l'argomento obbligatorio per il comando: '{0}'.</target>
31+
<source>Required argument '{0}' missing for command: '{1}'.</source>
32+
<target state="translated">Manca l'argomento obbligatorio '{0}' per il comando: '{1}'.</target>
3333
<note />
3434
</trans-unit>
3535
<trans-unit id="DirectoryDoesNotExist">

0 commit comments

Comments
 (0)