Skip to content

Commit 87982bf

Browse files
Inspect sub-commands recursively when looking for long-short-options
Fixes #189
1 parent 4d3e283 commit 87982bf

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/CommandLineUtils/Internal/CommandLineProcessor.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@ public CommandLineProcessor(CommandLineApplication command,
3939

4040
// TODO in 3.0, remove this check, and make ClusterOptions true always
4141
// and make it an error to use short options with multiple characters
42-
var allOptions = command.Commands.SelectMany(c => c.Options).Concat(command.GetOptions());
4342
if (!command.ClusterOptionsWasSetExplicitly)
4443
{
45-
command.ClusterOptions = !allOptions.Any(o => o.ShortName != null && o.ShortName.Length > 1);
44+
foreach (var option in AllOptions(command))
45+
{
46+
if (option.ShortName != null && option.ShortName.Length != 1)
47+
{
48+
command.ClusterOptions = false;
49+
break;
50+
}
51+
}
4652
}
47-
48-
if (command.ClusterOptions)
53+
else if (command.ClusterOptions)
4954
{
50-
foreach (var option in allOptions)
55+
foreach (var option in AllOptions(command))
5156
{
5257
if (option.ShortName != null && option.ShortName.Length != 1)
5358
{
@@ -58,6 +63,22 @@ public CommandLineProcessor(CommandLineApplication command,
5863
}
5964
}
6065

66+
static internal IEnumerable<CommandOption> AllOptions(CommandLineApplication command)
67+
{
68+
foreach (var option in command.Options)
69+
{
70+
yield return option;
71+
}
72+
73+
foreach (var subCommand in command.Commands)
74+
{
75+
foreach (var option in AllOptions(subCommand))
76+
{
77+
yield return option;
78+
}
79+
}
80+
}
81+
6182
public ParseResult Process()
6283
{
6384
_currentCommand = _initialCommand;

test/CommandLineUtils.Tests/CommandLineProcessorTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ private class ShortNameType
127127
public string Auth { get; }
128128
}
129129

130+
[Command]
131+
[Subcommand(typeof(ShortNameType))]
132+
private class ParentCommand
133+
{
134+
135+
}
136+
137+
[Command]
138+
[Subcommand(typeof(ParentCommand))]
139+
private class ParentParentCommand
140+
{
141+
142+
}
143+
130144
[Fact]
131145
public void ItInfersClusterOptionsCannotBeUsed()
132146
{
@@ -152,6 +166,22 @@ public void ItInfersClusterOptionsCannotBeUsed()
152166
app.Parse();
153167
Assert.False(app.ClusterOptions);
154168
}
169+
170+
{
171+
var app = new CommandLineApplication<ParentCommand>();
172+
app.Conventions.UseDefaultConventions();
173+
Assert.False(app.ClusterOptionsWasSetExplicitly);
174+
app.Parse();
175+
Assert.False(app.ClusterOptions);
176+
}
177+
178+
{
179+
var app = new CommandLineApplication<ParentParentCommand>();
180+
app.Conventions.UseDefaultConventions();
181+
Assert.False(app.ClusterOptionsWasSetExplicitly);
182+
app.Parse();
183+
Assert.False(app.ClusterOptions);
184+
}
155185
}
156186

157187
[Fact]

0 commit comments

Comments
 (0)