Skip to content

Commit 1a724a0

Browse files
committed
Allow empty string for CommandOption.ShortName
1 parent ec042a9 commit 1a724a0

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

src/CommandLineUtils/CommandOption.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,26 @@ public string Value()
183183
internal string ToTemplateString()
184184
{
185185
var sb = new StringBuilder();
186-
if (SymbolName != null)
186+
if (!string.IsNullOrEmpty(SymbolName))
187187
{
188188
sb.Append('-').Append(SymbolName);
189189
}
190190

191-
if (ShortName != null)
191+
if (!string.IsNullOrEmpty(ShortName))
192192
{
193193
if (sb.Length > 0) sb.Append('|');
194194

195195
sb.Append('-').Append(ShortName);
196196
}
197197

198-
if (LongName != null)
198+
if (!string.IsNullOrEmpty(LongName))
199199
{
200200
if (sb.Length > 0) sb.Append('|');
201201

202202
sb.Append("--").Append(LongName);
203203
}
204204

205-
if (ValueName != null && OptionType != CommandOptionType.NoValue)
205+
if (!string.IsNullOrEmpty(ValueName) && OptionType != CommandOptionType.NoValue)
206206
{
207207
sb.Append(" <").Append(ValueName).Append('>');
208208
}

src/CommandLineUtils/Internal/ReflectionAppBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private void AddOption(OptionAttributeBase optionAttr, PropertyInfo prop)
267267
throw new InvalidOperationException(Strings.NoValueTypesMustBeBoolean);
268268
}
269269

270-
if (option.ShortName != null)
270+
if (!string.IsNullOrEmpty(option.ShortName))
271271
{
272272
if (_shortOptions.TryGetValue(option.ShortName, out var otherProp))
273273
{
@@ -277,7 +277,7 @@ private void AddOption(OptionAttributeBase optionAttr, PropertyInfo prop)
277277
_shortOptions.Add(option.ShortName, prop);
278278
}
279279

280-
if (option.LongName != null)
280+
if (!string.IsNullOrEmpty(option.LongName))
281281
{
282282
if (_longOptions.TryGetValue(option.LongName, out var otherProp))
283283
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Nate McMaster.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using System.Text;
6+
using McMaster.Extensions.CommandLineUtils.HelpText;
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace McMaster.Extensions.CommandLineUtils.Tests
11+
{
12+
public class DefaultHelpTextGeneratorTests
13+
{
14+
private readonly ITestOutputHelper _output;
15+
16+
public DefaultHelpTextGeneratorTests(ITestOutputHelper output)
17+
{
18+
_output = output;
19+
}
20+
21+
private class EmptyShortName
22+
{
23+
[Option(ShortName = "")]
24+
public string Option { get; }
25+
}
26+
27+
[Fact]
28+
public void ItListOptions()
29+
{
30+
var builder = new ReflectionAppBuilder<EmptyShortName>();
31+
builder.Initialize();
32+
var sb = new StringBuilder();
33+
DefaultHelpTextGenerator.Singleton.Generate(builder.App, new StringWriter(sb));
34+
var helpText = sb.ToString();
35+
_output.WriteLine(helpText);
36+
37+
Assert.Contains("--option <OPTION>", helpText);
38+
Assert.DoesNotContain("-|--option <OPTION>", helpText);
39+
}
40+
}
41+
}

test/CommandLineUtils.Tests/ReflectionAppBuilderTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ public void CanOverrideShortNameOnOption()
132132
Assert.Equal("-d2|--detail2 <DETAIL2>", d2.Template);
133133
}
134134

135+
private class EmptyShortName
136+
{
137+
[Option(ShortName = "")]
138+
public string Detail1 { get; }
139+
140+
[Option(ShortName = "")]
141+
public string Detail2 { get; }
142+
}
143+
144+
[Fact]
145+
public void CanSetShortNameToEmptyString()
146+
{
147+
var builder = new ReflectionAppBuilder<EmptyShortName>();
148+
builder.Initialize();
149+
150+
Assert.All(builder.App.Options, o => Assert.Empty(o.ShortName));
151+
}
152+
135153
private class AmbiguousShortOptionName
136154
{
137155
[Option]

0 commit comments

Comments
 (0)