Skip to content

Commit

Permalink
Fix #218 - handle options with multiple characters in the short optio…
Browse files Browse the repository at this point in the history
…n name when only specified in a subcommand
  • Loading branch information
natemcmaster committed Mar 16, 2019
1 parent a0822b6 commit 84e9baf
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/CommandLineUtils/CommandLineApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ public CommandOption OptionHelp
/// </remarks>
public bool ClusterOptions
{
get => _clusterOptions ?? true;
// unless explicitly set, use the value of cluster options from the parent command
// or default to true if this is the root command
get => _clusterOptions.HasValue
? _clusterOptions.Value
: Parent == null || Parent.ClusterOptions;
set => _clusterOptions = value;
}

Expand Down
4 changes: 1 addition & 3 deletions src/CommandLineUtils/Internal/CommandLineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public CommandLineProcessor(CommandLineApplication command,
_initialCommand = command;
_enumerator = new ParameterEnumerator(arguments ?? new string[0]);

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

static internal IEnumerable<CommandOption> AllOptions(CommandLineApplication command)
internal static IEnumerable<CommandOption> AllOptions(CommandLineApplication command)
{
foreach (var option in command.Options)
{
Expand Down
4 changes: 4 additions & 0 deletions src/CommandLineUtils/releasenotes.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project>
<PropertyGroup>
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.3.4'">
Bugs fixed:
* Handle options with multiple characters in the short option name when only specified in a subcommand
</PackageReleaseNotes>
<PackageReleaseNotes Condition="'$(VersionPrefix)' == '2.3.3'">
Enhancement:
* @mpipo: add an API to disable the pager for help text (CommandLineApplication.UsePagerForHelpText)
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

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

<Import Project="..\Directory.Build.targets" />
Expand Down
9 changes: 9 additions & 0 deletions test/CommandLineUtils.Tests/CommandLineProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ public void ItInfersClusterOptionsCannotBeUsed()
app.Parse();
Assert.False(app.ClusterOptions);
}

{
// Issue #218
var app = new CommandLineApplication();
app.Command("sub1", c => { c.Option("-o1", "Option 1", CommandOptionType.SingleValue); });
Assert.False(app.ClusterOptionsWasSetExplicitly);
app.Parse("sub1", "-o1", "abc");
Assert.False(app.ClusterOptions);
}
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>2.3.3</VersionPrefix>
<VersionPrefix>2.3.4</VersionPrefix>
<VersionSuffix>build</VersionSuffix>
<IncludePreReleaseLabelInPackageVersion Condition="'$(IsStableBuild)' != 'true'">true</IncludePreReleaseLabelInPackageVersion>
<BuildNumber Condition=" '$(BuildNumber)' == '' ">$(BUILD_NUMBER)</BuildNumber>
Expand Down

0 comments on commit 84e9baf

Please sign in to comment.