Skip to content

Commit 8519138

Browse files
committed
Fix #30 - fix help text when OptionAttribute.ShortName is specified
1 parent e916043 commit 8519138

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

src/CommandLineUtils/Attributes/OptionAttribute.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ internal CommandOption Configure(CommandLineApplication app, PropertyInfo prop)
8282
ShortName = longName.Substring(0, 1),
8383
ValueName = prop.Name.ToConstantCase(),
8484
};
85-
86-
option.Template = $"-{option.ShortName}|--{option.LongName}";
87-
88-
if (option.OptionType != CommandOptionType.NoValue)
89-
{
90-
option.Template += $" <{option.ValueName.ToConstantCase()}>";
91-
}
9285
}
9386

9487
Configure(option);

src/CommandLineUtils/Attributes/OptionAttributeBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ internal void Configure(CommandOption option)
6161
option.LongName = LongName ?? option.LongName;
6262
option.ValueName = ValueName ?? option.ValueName;
6363
option.SymbolName = SymbolName ?? option.SymbolName;
64+
65+
if (option.Template == null)
66+
{
67+
option.Template = option.ToTemplateString();
68+
}
6469
}
6570
}
6671
}

src/CommandLineUtils/CommandOption.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Text;
89
using McMaster.Extensions.CommandLineUtils.Validation;
910

1011
namespace McMaster.Extensions.CommandLineUtils
@@ -175,6 +176,40 @@ public string Value()
175176
return HasValue() ? Values[0] : null;
176177
}
177178

179+
/// <summary>
180+
/// Generates the template string in the format "-{Symbol}|-{Short}|--{Long} &lt;{Value}&gt;" for display in help text.
181+
/// </summary>
182+
/// <returns>The template string</returns>
183+
internal string ToTemplateString()
184+
{
185+
var sb = new StringBuilder();
186+
if (SymbolName != null)
187+
{
188+
sb.Append('-').Append(SymbolName);
189+
}
190+
191+
if (ShortName != null)
192+
{
193+
if (sb.Length > 0) sb.Append('|');
194+
195+
sb.Append('-').Append(ShortName);
196+
}
197+
198+
if (LongName != null)
199+
{
200+
if (sb.Length > 0) sb.Append('|');
201+
202+
sb.Append("--").Append(LongName);
203+
}
204+
205+
if (ValueName != null && OptionType != CommandOptionType.NoValue)
206+
{
207+
sb.Append(" <").Append(ValueName).Append('>');
208+
}
209+
210+
return sb.ToString();
211+
}
212+
178213
private bool IsEnglishLetter(char c)
179214
{
180215
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');

test/CommandLineUtils.Tests/ReflectionAppBuilderTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
78
using System.Reflection;
89
using System.Reflection.Emit;
10+
using System.Text;
911
using Xunit;
1012
using Xunit.Abstractions;
1113

@@ -122,8 +124,11 @@ public void CanOverrideShortNameOnOption()
122124
{
123125
var builder = new ReflectionAppBuilder<ShortNameOverride>();
124126
builder.Initialize();
125-
Assert.Contains(builder.App.Options, o => o.ShortName == "d1" && o.LongName == "detail1");
126-
Assert.Contains(builder.App.Options, o => o.ShortName == "d2" && o.LongName == "detail2");
127+
128+
var d1 = Assert.Single(builder.App.Options, o => o.ShortName == "d1");
129+
Assert.Equal("-d1|--detail1 <DETAIL1>", d1.Template);
130+
var d2 = Assert.Single(builder.App.Options, o => o.ShortName == "d2");
131+
Assert.Equal("-d2|--detail2 <DETAIL2>", d2.Template);
127132
}
128133

129134
private class AmbiguousShortOptionName

0 commit comments

Comments
 (0)