Skip to content

Commit fc7d290

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

File tree

5 files changed

+171
-6
lines changed

5 files changed

+171
-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+
.WithHelpOption();
159+
var language = matchInfos.Where(mi => mi.Language != null).FirstOrDefault()?.Language;
160+
if (language != null)
161+
{
162+
example.WithOption(language.Option, language.GetValueOrDefault<string>()!);
163+
}
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+
option.Option.ValueType == typeof(bool) ? optionResult.GetValueOrDefault<bool>().ToString() : optionResult.GetValueOrDefault<string>());
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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,142 @@ 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 --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("bool", paramType: "bool")
198+
.WithParameter("string", paramType: "string")
199+
.WithChoiceParameter("choice", "c1", "c2")
200+
},
201+
new string?[][]
202+
{
203+
new string?[] { "name", null, "--fake", null, null }
204+
}
205+
};
206+
207+
yield return new object[]
208+
{
209+
"foo --int 6 --bool --string stringtype --choice c1 --fake value",
210+
new MockTemplateInfo[]
211+
{
212+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
213+
.WithParameter("int", paramType: "integer")
214+
.WithParameter("bool", paramType: "bool")
215+
.WithParameter("string", paramType: "string")
216+
.WithChoiceParameter("choice", "c1", "c2")
217+
},
218+
new string?[][]
219+
{
220+
new string?[] { "name", null, "--fake", null, null },
221+
new string?[] { "name", null, "value", null, null }
222+
}
223+
};
224+
225+
yield return new object[]
226+
{
227+
"foo --language F# --int 6 --bool --string stringtype --choice c1 --include",
228+
new MockTemplateInfo[]
229+
{
230+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithTag("language", "C#")
231+
.WithParameter("int", paramType: "integer")
232+
.WithParameter("bool", paramType: "bool")
233+
.WithParameter("string", paramType: "string")
234+
.WithChoiceParameter("choice", "c1", "c2")
235+
.WithParameter("include", "bool"),
236+
new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group").WithTag("language", "F#")
237+
.WithParameter("int", paramType: "integer")
238+
.WithParameter("bool", paramType: "bool")
239+
.WithParameter("string", paramType: "string")
240+
.WithChoiceParameter("choice", "c1", "c2")
241+
},
242+
new string?[][]
243+
{
244+
new string?[] { "name", null, "--include", null, null }
245+
}
246+
};
247+
248+
yield return new object[]
249+
{
250+
"foo --language F# --int 6 --bool --string stringtype --choice c1 --exclude",
251+
new MockTemplateInfo[]
252+
{
253+
new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group").WithTag("language", "C#")
254+
.WithParameter("int", paramType: "integer")
255+
.WithParameter("bool", paramType: "bool")
256+
.WithParameter("string", paramType: "string")
257+
.WithChoiceParameter("choice", "c1", "c2")
258+
.WithParameter("include", "bool"),
259+
new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group").WithTag("language", "F#")
260+
.WithParameter("int", paramType: "integer")
261+
.WithParameter("bool", paramType: "bool")
262+
.WithParameter("string", paramType: "string")
263+
.WithChoiceParameter("choice", "c1", "c2")
264+
},
265+
new string?[][]
266+
{
267+
new string?[] { "name", null, "--exclude", null, null }
268+
}
269+
};
134270
}
135271

136272
[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 -h --language {language}");
414+
}
415+
401416
[Fact]
402417
public void ItCanShowParseError()
403418
{

0 commit comments

Comments
 (0)