Skip to content

Commit 86d7582

Browse files
authored
Merge pull request #473 from moh-hassan/localize-verb
Localize VerbAttribute
2 parents 9f647b6 + 09c8f70 commit 86d7582

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

src/CommandLine/VerbAttribute.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ namespace CommandLine
88
/// Models a verb command specification.
99
/// </summary>
1010
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
11-
public sealed class VerbAttribute : Attribute
11+
//public sealed class VerbAttribute : Attribute
12+
public class VerbAttribute : Attribute
1213
{
1314
private readonly string name;
14-
private string helpText;
15+
private Infrastructure.LocalizableAttributeProperty helpText;
16+
private Type resourceType;
1517

1618
/// <summary>
1719
/// Initializes a new instance of the <see cref="CommandLine.VerbAttribute"/> class.
@@ -23,7 +25,8 @@ public VerbAttribute(string name)
2325
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("name");
2426

2527
this.name = name;
26-
this.helpText = string.Empty;
28+
helpText = new Infrastructure.LocalizableAttributeProperty(nameof(HelpText));
29+
resourceType = null;
2730
}
2831

2932
/// <summary>
@@ -48,11 +51,16 @@ public bool Hidden
4851
/// </summary>
4952
public string HelpText
5053
{
51-
get { return helpText; }
52-
set
53-
{
54-
helpText = value ?? throw new ArgumentNullException("value");
55-
}
54+
get => helpText.Value??string.Empty;
55+
set => helpText.Value = value ?? throw new ArgumentNullException("value");
56+
}
57+
/// <summary>
58+
/// Gets or sets the <see cref="System.Type"/> that contains the resources for <see cref="HelpText"/>.
59+
/// </summary>
60+
public Type ResourceType
61+
{
62+
get => resourceType;
63+
set => resourceType =helpText.ResourceType = value;
5664
}
5765
}
58-
}
66+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace CommandLine.Tests
5+
{
6+
//Test localization of VerbAttribute
7+
public class VerbAttributeTests
8+
{
9+
[Theory]
10+
[InlineData("", null, "")]
11+
[InlineData("", typeof(Fakes.StaticResource), "")]
12+
[InlineData("Help text", null, "Help text")]
13+
[InlineData("HelpText", typeof(Fakes.StaticResource), "Localized HelpText")]
14+
[InlineData("HelpText", typeof(Fakes.NonStaticResource), "Localized HelpText")]
15+
public static void VerbHelpText(string helpText, Type resourceType, string expected)
16+
{
17+
TestVerbAttribute verbAttribute = new TestVerbAttribute
18+
{
19+
HelpText = helpText,
20+
ResourceType = resourceType
21+
};
22+
23+
Assert.Equal(expected, verbAttribute.HelpText);
24+
}
25+
[Theory]
26+
[InlineData("HelpText", typeof(Fakes.NonStaticResource_WithNonStaticProperty))]
27+
[InlineData("WriteOnlyText", typeof(Fakes.NonStaticResource))]
28+
[InlineData("PrivateOnlyText", typeof(Fakes.NonStaticResource))]
29+
[InlineData("HelpText", typeof(Fakes.InternalResource))]
30+
public void ThrowsHelpText(string helpText, Type resourceType)
31+
{
32+
TestVerbAttribute verbAttribute = new TestVerbAttribute
33+
{
34+
HelpText = helpText,
35+
ResourceType = resourceType
36+
};
37+
38+
// Verify exception
39+
Assert.Throws<ArgumentException>(() => verbAttribute.HelpText);
40+
}
41+
42+
private class TestVerbAttribute : VerbAttribute
43+
{
44+
public TestVerbAttribute() : base("verb")
45+
{
46+
// Do nothing
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)