Skip to content

Commit 31f142e

Browse files
authored
Fix func command usage format and conditionally display [subcontext] placeholder (#4562)
1 parent 37e031e commit 31f142e

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/Cli/func/Actions/HelpAction.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,22 @@ private void DisplayContextHelp(Context context, Context subContext)
128128
{
129129
if (subContext == Context.None)
130130
{
131-
ColoredConsole
132-
.WriteLine($"{TitleColor("Usage:")} func {context.ToLowerCaseString()} [context] <action> [-/--options]")
133-
.WriteLine();
134131
var contexts = _actionTypes
135132
.Where(a => a.Contexts.Contains(context))
136133
.Select(a => a.SubContexts)
137134
.SelectMany(c => c)
138135
.Where(c => c != Context.None)
139136
.Distinct()
140137
.OrderBy(c => c.ToLowerCaseString());
138+
139+
var hasSubcontexts = contexts.Any();
140+
var usageFormat = hasSubcontexts
141+
? $"{TitleColor("Usage:")} func {context.ToLowerCaseString()} [subcontext] <action> [-/--options]"
142+
: $"{TitleColor("Usage:")} func {context.ToLowerCaseString()} <action> [-/--options]";
143+
144+
ColoredConsole
145+
.WriteLine(usageFormat)
146+
.WriteLine();
141147
DisplayContextsHelp(contexts);
142148
}
143149
else
@@ -178,7 +184,7 @@ private void DisplayGeneralHelp()
178184
.OrderBy(c => c.ToLowerCaseString());
179185
Utilities.PrintVersion();
180186
ColoredConsole
181-
.WriteLine("Usage: func [context] [context] <action> [-/--options]")
187+
.WriteLine("Usage: func [context] <action> [-/--options]")
182188
.WriteLine();
183189
DisplayContextsHelp(contexts);
184190
var actions = _actionTypes.Where(a => a.Contexts.Contains(Context.None));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using FluentAssertions;
5+
using Xunit;
6+
7+
namespace Azure.Functions.Cli.UnitTests.ActionsTests
8+
{
9+
public class HelpActionTests
10+
{
11+
[Fact]
12+
public void UsageFormat_ShouldNotContainDuplicateContext()
13+
{
14+
// This test validates that the usage format strings in HelpAction.cs
15+
// do not contain duplicate [context] placeholders
16+
17+
// Expected formats after the fix:
18+
var expectedGeneralFormat = "Usage: func [context] <action> [-/--options]";
19+
var expectedContextWithSubcontextFormat = "func {context} [subcontext] <action> [-/--options]";
20+
var expectedContextWithoutSubcontextFormat = "func {context} <action> [-/--options]";
21+
var expectedSubContextFormat = "func {context} {subcontext} <action> [-/--options]";
22+
23+
// Problematic format that should NOT appear:
24+
var problematicFormat = "func [context] [context] <action>";
25+
26+
// Assert correct formats are used
27+
expectedGeneralFormat.Should().NotContain("[context] [context]");
28+
expectedContextWithSubcontextFormat.Should().NotContain("[context] <action>");
29+
expectedContextWithSubcontextFormat.Should().Contain("[subcontext]");
30+
expectedContextWithoutSubcontextFormat.Should().NotContain("[subcontext]");
31+
expectedSubContextFormat.Should().NotContain("[context]");
32+
33+
// Assert problematic format is avoided
34+
problematicFormat.Should().Contain("[context] [context]", "this validates our test itself");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)