Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/CommandLine/VerbAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace CommandLine
/// Models a verb command specification.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
public sealed class VerbAttribute : Attribute
//public sealed class VerbAttribute : Attribute
public class VerbAttribute : Attribute
{
private readonly string name;
private string helpText;
private Infrastructure.LocalizableAttributeProperty helpText;
private Type resourceType;

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

this.name = name;
this.helpText = string.Empty;
helpText = new Infrastructure.LocalizableAttributeProperty(nameof(HelpText));
resourceType = null;
}

/// <summary>
Expand All @@ -48,11 +51,16 @@ public bool Hidden
/// </summary>
public string HelpText
{
get { return helpText; }
set
{
helpText = value ?? throw new ArgumentNullException("value");
}
get => helpText.Value??string.Empty;
set => helpText.Value = value ?? throw new ArgumentNullException("value");
}
/// <summary>
/// Gets or sets the <see cref="System.Type"/> that contains the resources for <see cref="HelpText"/>.
/// </summary>
public Type ResourceType
{
get => resourceType;
set => resourceType =helpText.ResourceType = value;
}
}
}
}
50 changes: 50 additions & 0 deletions tests/CommandLine.Tests/Unit/VerbAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Xunit;

namespace CommandLine.Tests
{
//Test localization of VerbAttribute
public class VerbAttributeTests
{
[Theory]
[InlineData("", null, "")]
[InlineData("", typeof(Fakes.StaticResource), "")]
[InlineData("Help text", null, "Help text")]
[InlineData("HelpText", typeof(Fakes.StaticResource), "Localized HelpText")]
[InlineData("HelpText", typeof(Fakes.NonStaticResource), "Localized HelpText")]
public static void VerbHelpText(string helpText, Type resourceType, string expected)
{
TestVerbAttribute verbAttribute = new TestVerbAttribute
{
HelpText = helpText,
ResourceType = resourceType
};

Assert.Equal(expected, verbAttribute.HelpText);
}
[Theory]
[InlineData("HelpText", typeof(Fakes.NonStaticResource_WithNonStaticProperty))]
[InlineData("WriteOnlyText", typeof(Fakes.NonStaticResource))]
[InlineData("PrivateOnlyText", typeof(Fakes.NonStaticResource))]
[InlineData("HelpText", typeof(Fakes.InternalResource))]
public void ThrowsHelpText(string helpText, Type resourceType)
{
TestVerbAttribute verbAttribute = new TestVerbAttribute
{
HelpText = helpText,
ResourceType = resourceType
};

// Verify exception
Assert.Throws<ArgumentException>(() => verbAttribute.HelpText);
}

private class TestVerbAttribute : VerbAttribute
{
public TestVerbAttribute() : base("verb")
{
// Do nothing
}
}
}
}