|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using CommandLine.Text; |
| 4 | +using FluentAssertions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +// Issue #830 |
| 8 | +// Allow private properties as options and usage |
| 9 | +namespace CommandLine.Tests.Unit |
| 10 | +{ |
| 11 | + public class Issue830Tests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void Parse_options_with_private_value_and_option() |
| 15 | + { |
| 16 | + var expectedOptions = new Options |
| 17 | + { |
| 18 | + Option = "a", |
| 19 | + Value = "b" |
| 20 | + }; |
| 21 | + |
| 22 | + var result = Parser.Default.ParseArguments<Options>( |
| 23 | + new[] { "b", "--opt", "a" }); |
| 24 | + |
| 25 | + ((Parsed<Options>)result).Value.Should().BeEquivalentTo(expectedOptions); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void Print_private_usage() |
| 30 | + { |
| 31 | + var help = new StringWriter(); |
| 32 | + var sut = new Parser(config => config.HelpWriter = help); |
| 33 | + |
| 34 | + sut.ParseArguments<Options>(new string[] { "--help" }); |
| 35 | + var result = help.ToString(); |
| 36 | + |
| 37 | + var lines = result.ToLines().TrimStringArray(); |
| 38 | + lines[3].Should().BeEquivalentTo("Do something very cool:"); |
| 39 | + lines[4].Should().BeEquivalentTo("CommandLine --opt test1 test2"); |
| 40 | + } |
| 41 | + |
| 42 | + private class Options |
| 43 | + { |
| 44 | + public string Option { get => PrivateOption; set => PrivateOption = value; } |
| 45 | + |
| 46 | + public string Value { get => PrivateValue; set => PrivateValue = value; } |
| 47 | + |
| 48 | + [Option("opt", Required = true)] |
| 49 | + private string PrivateOption { get; set; } |
| 50 | + |
| 51 | + [Value(0, Required = true)] |
| 52 | + private string PrivateValue { get; set; } |
| 53 | + |
| 54 | + [Usage] |
| 55 | + private static IEnumerable<Example> PrivateUsage { get; } = new List<Example> |
| 56 | + { |
| 57 | + new Example("Do something very cool", new Options |
| 58 | + { |
| 59 | + PrivateOption = "test1", |
| 60 | + PrivateValue = "test2" |
| 61 | + }) |
| 62 | + }; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments