Skip to content

Commit 5a68768

Browse files
committed
Ad showHidden option to UnParser. Only return hidden options if showHidden is true
1 parent fd14076 commit 5a68768

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/CommandLine/UnParserExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class UnParserSettings
1818
private bool preferShortName;
1919
private bool groupSwitches;
2020
private bool useEqualToken;
21+
private bool showHidden;
2122

2223
/// <summary>
2324
/// Gets or sets a value indicating whether unparsing process shall prefer short or long names.
@@ -46,6 +47,14 @@ public bool UseEqualToken
4647
set { PopsicleSetter.Set(Consumed, ref useEqualToken, value); }
4748
}
4849

50+
/// <summary>
51+
/// Gets or sets a value indicating whether unparsing process shall expose hidden options.
52+
/// </summary>
53+
public bool ShowHidden
54+
{
55+
get { return showHidden; }
56+
set { PopsicleSetter.Set(Consumed, ref showHidden, value); }
57+
}
4958
/// <summary>
5059
/// Factory method that creates an instance of <see cref="CommandLine.UnParserSettings"/> with GroupSwitches set to true.
5160
/// </summary>
@@ -119,6 +128,7 @@ public static string FormatCommandLine<T>(this Parser parser, T options, Action<
119128
var allOptSpecs = from info in specs.Where(i => i.Specification.Tag == SpecificationType.Option)
120129
let o = (OptionSpecification)info.Specification
121130
where o.TargetType != TargetType.Switch || (o.TargetType == TargetType.Switch && ((bool)info.Value))
131+
where !o.Hidden || settings.ShowHidden
122132
orderby o.UniqueName()
123133
select info;
124134

tests/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Link>Properties\SharedAssemblyInfo.cs</Link>
5959
</Compile>
6060
<Compile Include="CultureInfoExtensions.cs" />
61+
<Compile Include="Fakes\Hidden_Option.cs" />
6162
<Compile Include="Fakes\Options_With_Default_Set_To_Sequence.cs" />
6263
<Compile Include="Fakes\Options_With_Guid.cs" />
6364
<Compile Include="Fakes\Options_With_Option_And_Value_Of_String_Type.cs" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CommandLine.Tests.Fakes
8+
{
9+
public class Hidden_Option
10+
{
11+
[Option('h', "hiddenOption", Default="hidden", Hidden = true)]
12+
public string HiddenOption { get; set; }
13+
}
14+
}

tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public static void UnParsing_immutable_instance_returns_command_line(Immutable_S
4040
.ShouldBeEquivalentTo(result);
4141
}
4242

43+
[Theory]
44+
[MemberData("UnParseDataHidden")]
45+
public static void Unparsing_hidden_option_returns_command_line(Hidden_Option options, bool showHidden, string result)
46+
{
47+
new Parser()
48+
.FormatCommandLine(options, config => config.ShowHidden = showHidden)
49+
.ShouldBeEquivalentTo(result);
50+
}
51+
4352
#if !SKIP_FSHARP
4453
[Theory]
4554
[MemberData("UnParseDataFSharpOption")]
@@ -141,6 +150,14 @@ public static IEnumerable<object> UnParseDataImmutable
141150
}
142151
}
143152

153+
public static IEnumerable<object> UnParseDataHidden
154+
{
155+
get
156+
{
157+
yield return new object[] { new Hidden_Option { HiddenOption = "hidden" }, true, "--hiddenOption hidden" };
158+
yield return new object[] { new Hidden_Option { HiddenOption = "hidden" }, false, ""};
159+
}
160+
}
144161
#if !SKIP_FSHARP
145162
public static IEnumerable<object> UnParseDataFSharpOption
146163
{

0 commit comments

Comments
 (0)