Skip to content

Commit 84e9baf

Browse files
committed
Fix #218 - handle options with multiple characters in the short option name when only specified in a subcommand
1 parent a0822b6 commit 84e9baf

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,11 @@ public CommandOption OptionHelp
313313
/// </remarks>
314314
public bool ClusterOptions
315315
{
316-
get => _clusterOptions ?? true;
316+
// unless explicitly set, use the value of cluster options from the parent command
317+
// or default to true if this is the root command
318+
get => _clusterOptions.HasValue
319+
? _clusterOptions.Value
320+
: Parent == null || Parent.ClusterOptions;
317321
set => _clusterOptions = value;
318322
}
319323

src/CommandLineUtils/Internal/CommandLineProcessor.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public CommandLineProcessor(CommandLineApplication command,
3737
_initialCommand = command;
3838
_enumerator = new ParameterEnumerator(arguments ?? new string[0]);
3939

40-
// TODO in 3.0, remove this check, and make ClusterOptions true always
41-
// and make it an error to use short options with multiple characters
4240
if (!command.ClusterOptionsWasSetExplicitly)
4341
{
4442
foreach (var option in AllOptions(command))
@@ -63,7 +61,7 @@ public CommandLineProcessor(CommandLineApplication command,
6361
}
6462
}
6563

66-
static internal IEnumerable<CommandOption> AllOptions(CommandLineApplication command)
64+
internal static IEnumerable<CommandOption> AllOptions(CommandLineApplication command)
6765
{
6866
foreach (var option in command.Options)
6967
{

src/CommandLineUtils/releasenotes.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<Project>
22
<PropertyGroup>
3+
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.3.4'">
4+
Bugs fixed:
5+
* Handle options with multiple characters in the short option name when only specified in a subcommand
6+
</PackageReleaseNotes>
37
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.3.3'">
48
Enhancement:
59
* @mpipo: add an API to disable the pager for help text (CommandLineApplication.UsePagerForHelpText)

src/Directory.Build.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
55
</PropertyGroup>
66

7-
<ItemGroup>
8-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="All" />
7+
<ItemGroup Condition=" '$(CI)' == 'true' ">
8+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05" PrivateAssets="All" />
99
</ItemGroup>
1010

1111
<Import Project="..\Directory.Build.targets" />

test/CommandLineUtils.Tests/CommandLineProcessorTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ public void ItInfersClusterOptionsCannotBeUsed()
182182
app.Parse();
183183
Assert.False(app.ClusterOptions);
184184
}
185+
186+
{
187+
// Issue #218
188+
var app = new CommandLineApplication();
189+
app.Command("sub1", c => { c.Option("-o1", "Option 1", CommandOptionType.SingleValue); });
190+
Assert.False(app.ClusterOptionsWasSetExplicitly);
191+
app.Parse("sub1", "-o1", "abc");
192+
Assert.False(app.ClusterOptions);
193+
}
185194
}
186195

187196
[Fact]

version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>2.3.3</VersionPrefix>
3+
<VersionPrefix>2.3.4</VersionPrefix>
44
<VersionSuffix>build</VersionSuffix>
55
<IncludePreReleaseLabelInPackageVersion Condition="'$(IsStableBuild)' != 'true'">true</IncludePreReleaseLabelInPackageVersion>
66
<BuildNumber Condition=" '$(BuildNumber)' == '' ">$(BUILD_NUMBER)</BuildNumber>

0 commit comments

Comments
 (0)