Skip to content

Commit 11bcce2

Browse files
GangWang01vlada-shubina
authored andcommitted
Improve the example with specified language when language option is given
1 parent df044ed commit 11bcce2

File tree

5 files changed

+183
-6
lines changed

5 files changed

+183
-6
lines changed

src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/InstantiateCommand.NoMatchHandling.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,16 @@ private static NewCommandStatus HandleNoTemplateFoundResult(
152152
if (templateGroup.ShortNames.Any())
153153
{
154154
reporter.WriteLine(LocalizableStrings.InvalidParameterTemplateHint);
155-
reporter.WriteCommand(
156-
Example
157-
.For<NewCommand>(args.ParseResult)
158-
.WithArgument(NewCommand.ShortNameArgument, templateGroup.ShortNames[0])
159-
.WithHelpOption());
155+
var example = Example
156+
.For<NewCommand>(args.ParseResult)
157+
.WithArgument(NewCommand.ShortNameArgument, templateGroup.ShortNames[0]);
158+
var language = matchInfos.Where(mi => mi.Language != null).FirstOrDefault()?.Language;
159+
if (language != null)
160+
{
161+
example.WithOption(language.Option, language.GetValueOrDefault<string>()!);
162+
}
163+
example.WithHelpOption();
164+
reporter.WriteCommand(example);
160165
}
161166

162167
return invalidOptionsList.Any() ? NewCommandStatus.InvalidOption : NewCommandStatus.NotFound;

src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/TemplateOptionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal TemplateOptionResult(
4747
return new TemplateOptionResult(
4848
option,
4949
optionResult.Token?.Value ?? string.Empty,
50-
optionResult.GetValueOrDefault<string>());
50+
optionResult.GetValueOrDefault()?.ToString());
5151
}
5252
}
5353
}

src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/TemplateResult.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
using System.CommandLine;
6+
using System.CommandLine.Parsing;
67

78
namespace Microsoft.TemplateEngine.Cli.Commands
89
{
@@ -29,6 +30,8 @@ private TemplateResult(TemplateCommand templateCommand, ParseResult parseResult)
2930

3031
internal bool IsBaselineMatch { get; private set; }
3132

33+
internal OptionResult? Language { get; private set; }
34+
3235
internal CliTemplateInfo TemplateInfo => _templateCommand.Template;
3336

3437
internal IEnumerable<TemplateOptionResult> ValidTemplateOptions => _parametersInfo.Where(i => !(i is InvalidTemplateOptionResult));
@@ -41,6 +44,12 @@ internal static TemplateResult FromParseResult(TemplateCommand templateCommand,
4144
result.IsLanguageMatch = templateCommand.LanguageOption == null || !parseResult.HasErrorFor(templateCommand.LanguageOption);
4245
result.IsTypeMatch = templateCommand.TypeOption == null || !parseResult.HasErrorFor(templateCommand.TypeOption);
4346
result.IsBaselineMatch = templateCommand.BaselineOption == null || !parseResult.HasErrorFor(templateCommand.BaselineOption);
47+
48+
if (templateCommand.LanguageOption != null && result.IsTemplateMatch)
49+
{
50+
result.Language = parseResult.FindResultFor(templateCommand.LanguageOption);
51+
}
52+
4453
foreach (var option in templateCommand.TemplateOptions)
4554
{
4655
if (parseResult.HasErrorFor(option.Value.Option))

src/Tests/Microsoft.TemplateEngine.Cli.UnitTests/ParserTests/InstantiateTests.NoMatchHandling.cs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,154 @@ public static IEnumerable<object[]> GetInvalidParametersTestData()
131131
new string?[] { "value", "langVersion", "--langVersion", null, "Required argument missing for option: '--langVersion'." }
132132
}
133133
};
134+
135+
yield return new object[]
136+
{
137+
"foo --fake",
138+
new MockTemplateInfo[]
139+
{
140+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithParameter("langVersion")
141+
},
142+
new string?[][]
143+
{
144+
new string?[] { "name", null, "--fake", null, null }
145+
}
146+
};
147+
148+
yield return new object[]
149+
{
150+
"foo --fake value",
151+
new MockTemplateInfo[]
152+
{
153+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithParameter("langVersion")
154+
},
155+
new string?[][]
156+
{
157+
new string?[] { "name", null, "--fake", null, null },
158+
new string?[] { "name", null, "value", null, null }
159+
}
160+
};
161+
162+
yield return new object[]
163+
{
164+
"foo --language F# --include",
165+
new MockTemplateInfo[]
166+
{
167+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithTag("language", "C#").WithParameter("include", "bool"),
168+
new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group").WithTag("language", "F#")
169+
},
170+
new string?[][]
171+
{
172+
new string?[] { "name", null, "--include", null, null }
173+
}
174+
};
175+
176+
yield return new object[]
177+
{
178+
"foo --language F# --exclude",
179+
new MockTemplateInfo[]
180+
{
181+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithTag("language", "C#").WithParameter("include", "bool"),
182+
new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group").WithTag("language", "F#")
183+
},
184+
new string?[][]
185+
{
186+
new string?[] { "name", null, "--exclude", null, null }
187+
}
188+
};
189+
190+
yield return new object[]
191+
{
192+
"foo --int 6 --float 3.14 --hex 0x1A2F --bool --string stringtype --choice c1 --fake",
193+
new MockTemplateInfo[]
194+
{
195+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
196+
.WithParameter("int", paramType: "integer")
197+
.WithParameter("float", paramType: "float")
198+
.WithParameter("hex", paramType: "hex")
199+
.WithParameter("bool", paramType: "bool")
200+
.WithParameter("string", paramType: "string")
201+
.WithChoiceParameter("choice", "c1", "c2")
202+
},
203+
new string?[][]
204+
{
205+
new string?[] { "name", null, "--fake", null, null }
206+
}
207+
};
208+
209+
yield return new object[]
210+
{
211+
"foo --int 6 --float 3.14 --hex 0x1A2F --bool --string stringtype --choice c1 --fake value",
212+
new MockTemplateInfo[]
213+
{
214+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
215+
.WithParameter("int", paramType: "integer")
216+
.WithParameter("float", paramType: "float")
217+
.WithParameter("hex", paramType: "hex")
218+
.WithParameter("bool", paramType: "bool")
219+
.WithParameter("string", paramType: "string")
220+
.WithChoiceParameter("choice", "c1", "c2")
221+
},
222+
new string?[][]
223+
{
224+
new string?[] { "name", null, "--fake", null, null },
225+
new string?[] { "name", null, "value", null, null }
226+
}
227+
};
228+
229+
yield return new object[]
230+
{
231+
"foo --language F# --int 6 --float 3.14 --hex 0x1A2F --bool --string stringtype --choice c1 --include",
232+
new MockTemplateInfo[]
233+
{
234+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithTag("language", "C#")
235+
.WithParameter("int", paramType: "integer")
236+
.WithParameter("float", paramType: "float")
237+
.WithParameter("hex", paramType: "hex")
238+
.WithParameter("bool", paramType: "bool")
239+
.WithParameter("string", paramType: "string")
240+
.WithChoiceParameter("choice", "c1", "c2")
241+
.WithParameter("include", "bool"),
242+
new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group").WithTag("language", "F#")
243+
.WithParameter("int", paramType: "integer")
244+
.WithParameter("float", paramType: "float")
245+
.WithParameter("hex", paramType: "hex")
246+
.WithParameter("bool", paramType: "bool")
247+
.WithParameter("string", paramType: "string")
248+
.WithChoiceParameter("choice", "c1", "c2")
249+
},
250+
new string?[][]
251+
{
252+
new string?[] { "name", null, "--include", null, null }
253+
}
254+
};
255+
256+
yield return new object[]
257+
{
258+
"foo --language F# --int 6 --float 3.14 --hex 0x1A2F --bool --string stringtype --choice c1 --exclude",
259+
new MockTemplateInfo[]
260+
{
261+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithTag("language", "C#")
262+
.WithParameter("int", paramType: "integer")
263+
.WithParameter("float", paramType: "float")
264+
.WithParameter("hex", paramType: "hex")
265+
.WithParameter("bool", paramType: "bool")
266+
.WithParameter("string", paramType: "string")
267+
.WithChoiceParameter("choice", "c1", "c2")
268+
.WithParameter("include", "bool"),
269+
new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group").WithTag("language", "F#")
270+
.WithParameter("int", paramType: "integer")
271+
.WithParameter("float", paramType: "float")
272+
.WithParameter("hex", paramType: "hex")
273+
.WithParameter("bool", paramType: "bool")
274+
.WithParameter("string", paramType: "string")
275+
.WithChoiceParameter("choice", "c1", "c2")
276+
},
277+
new string?[][]
278+
{
279+
new string?[] { "name", null, "--exclude", null, null }
280+
}
281+
};
134282
}
135283

136284
[Theory]

src/Tests/dotnet-new.Tests/DotnetNewInstantiateTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ public void ItCanShowHelpForTemplate()
398398
.And.HaveStdOutContaining("--framework");
399399
}
400400

401+
[Theory]
402+
[InlineData("-lang", "F#", "--use-program-main")]
403+
[InlineData("--language", "F#", "--use-program-main")]
404+
[InlineData("-lang", "C#", "--no-exist")]
405+
public void ExampleHasLanguageForSepecifiedLanguageWithInvalidOption(string languageOption, string language, string invalidOption)
406+
{
407+
CommandResult cmd = new DotnetNewCommand(Log, "console", languageOption, language, invalidOption)
408+
.WithVirtualHive()
409+
.Execute();
410+
cmd.Should().Fail()
411+
.And.HaveStdErrContaining($"'{invalidOption}' is not a valid option")
412+
.And.HaveStdErrContaining("For more information, run:")
413+
.And.HaveStdErrContaining($"dotnet new console --language {language} -h");
414+
}
415+
401416
[Fact]
402417
public void ItCanShowParseError()
403418
{

0 commit comments

Comments
 (0)