Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #830 - Recognize Option, Value and Usage attributes on private members #831

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/CommandLine/Core/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ static class ReflectionExtensions
{
public static IEnumerable<T> GetSpecifications<T>(this Type type, Func<PropertyInfo, T> selector)
{
return from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties())
return from pi in type.FlattenHierarchy()
.SelectMany(x => x.GetTypeInfo()
.GetProperties(BindingFlags.Instance | BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public))
let attrs = pi.GetCustomAttributes(true)
where
attrs.OfType<OptionAttribute>().Any() ||
Expand All @@ -38,7 +41,9 @@ public static Maybe<VerbAttribute> GetVerbSpecification(this Type type)
public static Maybe<Tuple<PropertyInfo, UsageAttribute>> GetUsageData(this Type type)
{
return
(from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties())
(from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties(
BindingFlags.Instance | BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public))
let attrs = pi.GetCustomAttributes(typeof(UsageAttribute), true)
where attrs.Any()
select Tuple.Create(pi, (UsageAttribute)attrs.First()))
Expand Down
6 changes: 4 additions & 2 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,10 @@ private IEnumerable<Specification> GetSpecificationsFromType(Type type)
var prop = tuple.Item1;
var attr = tuple.Item2;

var examples = (IEnumerable<Example>)prop
.GetValue(null, BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty, null, null, null);
var examples = (IEnumerable<Example>) prop
.GetValue(null,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static |
BindingFlags.GetProperty, null, null, null);

return Tuple.Create(attr, examples);
});
Expand Down
65 changes: 65 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue830Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.IO;
using CommandLine.Text;
using FluentAssertions;
using Xunit;

// Issue #830
// Allow private properties as options and usage
namespace CommandLine.Tests.Unit
{
public class Issue830Tests
{
[Fact]
public void Parse_options_with_private_value_and_option()
{
var expectedOptions = new Options
{
Option = "a",
Value = "b"
};

var result = Parser.Default.ParseArguments<Options>(
new[] { "b", "--opt", "a" });

((Parsed<Options>)result).Value.Should().BeEquivalentTo(expectedOptions);
}

[Fact]
public void Print_private_usage()
{
var help = new StringWriter();
var sut = new Parser(config => config.HelpWriter = help);

sut.ParseArguments<Options>(new string[] { "--help" });
var result = help.ToString();

var lines = result.ToLines().TrimStringArray();
lines[3].Should().BeEquivalentTo("Do something very cool:");
lines[4].Should().BeEquivalentTo("myApp.txt --opt test1 test2");
}

private class Options
{
public string Option { get => PrivateOption; set => PrivateOption = value; }

public string Value { get => PrivateValue; set => PrivateValue = value; }

[Option("opt", Required = true)]
private string PrivateOption { get; set; }

[Value(0, Required = true)]
private string PrivateValue { get; set; }

[Usage(ApplicationAlias = "myApp.txt")]
private static IEnumerable<Example> PrivateUsage { get; } = new List<Example>
{
new Example("Do something very cool", new Options
{
PrivateOption = "test1",
PrivateValue = "test2"
})
};
}
}
}