Skip to content

Commit 6acf8e6

Browse files
authored
Merge pull request #324 from CreepyGnome/Issue263
Implemented Hidden Verbs, Options and Values
2 parents c98c643 + 4a47661 commit 6acf8e6

File tree

11 files changed

+204
-22
lines changed

11 files changed

+204
-22
lines changed

src/CommandLine/BaseAttribute.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,14 @@ public string MetaValue
123123
metaValue = value;
124124
}
125125
}
126+
127+
/// <summary>
128+
/// Gets or sets a value indicating whether a command line option is visible in the help text.
129+
/// </summary>
130+
public bool Hidden
131+
{
132+
get;
133+
set;
134+
}
126135
}
127136
}

src/CommandLine/Core/OptionSpecification.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ sealed class OptionSpecification : Specification
1616

1717
public OptionSpecification(string shortName, string longName, bool required, string setName, Maybe<int> min, Maybe<int> max,
1818
char separator, Maybe<object> defaultValue, string helpText, string metaValue, IEnumerable<string> enumValues,
19-
Type conversionType, TargetType targetType)
20-
: base(SpecificationType.Option, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType)
19+
Type conversionType, TargetType targetType, bool hidden = false)
20+
: base(SpecificationType.Option, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType, hidden)
2121
{
2222
this.shortName = shortName;
2323
this.longName = longName;
@@ -40,13 +40,14 @@ public static OptionSpecification FromAttribute(OptionAttribute attribute, Type
4040
attribute.MetaValue,
4141
enumValues,
4242
conversionType,
43-
conversionType.ToTargetType());
43+
conversionType.ToTargetType(),
44+
attribute.Hidden);
4445
}
4546

46-
public static OptionSpecification NewSwitch(string shortName, string longName, bool required, string helpText, string metaValue)
47+
public static OptionSpecification NewSwitch(string shortName, string longName, bool required, string helpText, string metaValue, bool hidden = false)
4748
{
4849
return new OptionSpecification(shortName, longName, required, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(),
49-
'\0', Maybe.Nothing<object>(), helpText, metaValue, Enumerable.Empty<string>(), typeof(bool), TargetType.Switch);
50+
'\0', Maybe.Nothing<object>(), helpText, metaValue, Enumerable.Empty<string>(), typeof(bool), TargetType.Switch, hidden);
5051
}
5152

5253
public string ShortName

src/CommandLine/Core/Specification.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class Specification
2525
{
2626
private readonly SpecificationType tag;
2727
private readonly bool required;
28+
private readonly bool hidden;
2829
private readonly Maybe<int> min;
2930
private readonly Maybe<int> max;
3031
private readonly Maybe<object> defaultValue;
@@ -37,7 +38,7 @@ abstract class Specification
3738

3839
protected Specification(SpecificationType tag, bool required, Maybe<int> min, Maybe<int> max,
3940
Maybe<object> defaultValue, string helpText, string metaValue, IEnumerable<string> enumValues,
40-
Type conversionType, TargetType targetType)
41+
Type conversionType, TargetType targetType, bool hidden = false)
4142
{
4243
this.tag = tag;
4344
this.required = required;
@@ -49,6 +50,7 @@ protected Specification(SpecificationType tag, bool required, Maybe<int> min, Ma
4950
this.helpText = helpText;
5051
this.metaValue = metaValue;
5152
this.enumValues = enumValues;
53+
this.hidden = hidden;
5254
}
5355

5456
public SpecificationType Tag
@@ -101,6 +103,11 @@ public TargetType TargetType
101103
get { return targetType; }
102104
}
103105

106+
public bool Hidden
107+
{
108+
get { return hidden; }
109+
}
110+
104111
public static Specification FromProperty(PropertyInfo property)
105112
{
106113
var attrs = property.GetCustomAttributes(true);

src/CommandLine/Core/SpecificationExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public static OptionSpecification WithLongName(this OptionSpecification specific
3333
specification.MetaValue,
3434
specification.EnumValues,
3535
specification.ConversionType,
36-
specification.TargetType);
36+
specification.TargetType,
37+
specification.Hidden);
3738
}
3839

3940
public static string UniqueName(this OptionSpecification specification)

src/CommandLine/Core/ValueSpecification.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ sealed class ValueSpecification : Specification
1313

1414
public ValueSpecification(int index, string metaName, bool required, Maybe<int> min, Maybe<int> max, Maybe<object> defaultValue,
1515
string helpText, string metaValue, IEnumerable<string> enumValues,
16-
Type conversionType, TargetType targetType)
17-
: base(SpecificationType.Value, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType)
16+
Type conversionType, TargetType targetType, bool hidden = false)
17+
: base(SpecificationType.Value, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType, hidden)
1818
{
1919
this.index = index;
2020
this.metaName = metaName;
@@ -33,7 +33,8 @@ public static ValueSpecification FromAttribute(ValueAttribute attribute, Type co
3333
attribute.MetaValue,
3434
enumValues,
3535
conversionType,
36-
conversionType.ToTargetType());
36+
conversionType.ToTargetType(),
37+
attribute.Hidden);
3738
}
3839

3940
public int Index

src/CommandLine/Core/Verb.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ sealed class Verb
1010
{
1111
private readonly string name;
1212
private readonly string helpText;
13+
private readonly bool hidden;
1314

14-
public Verb(string name, string helpText)
15+
public Verb(string name, string helpText, bool hidden = false)
1516
{
1617
if (name == null) throw new ArgumentNullException("name");
1718
if (helpText == null) throw new ArgumentNullException("helpText");
1819

1920
this.name = name;
2021
this.helpText = helpText;
22+
this.hidden = hidden;
2123
}
2224

2325
public string Name
@@ -30,11 +32,17 @@ public string HelpText
3032
get { return helpText; }
3133
}
3234

35+
public bool Hidden
36+
{
37+
get { return hidden; }
38+
}
39+
3340
public static Verb FromAttribute(VerbAttribute attribute)
3441
{
3542
return new Verb(
3643
attribute.Name,
37-
attribute.HelpText
44+
attribute.HelpText,
45+
attribute.Hidden
3846
);
3947
}
4048

src/CommandLine/Text/HelpText.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,8 @@ private IEnumerable<Specification> AdaptVerbsToSpecifications(IEnumerable<Type>
683683
verbTuple.Item1.Name,
684684
false,
685685
verbTuple.Item1.HelpText,
686-
string.Empty)).Concat(new[] { MakeHelpEntry(), MakeVersionEntry() });
686+
string.Empty,
687+
verbTuple.Item1.Hidden)).Concat(new[] { MakeHelpEntry(), MakeVersionEntry() });
687688
}
688689

689690
private HelpText AddOptionsImpl(
@@ -711,7 +712,8 @@ private OptionSpecification MakeHelpEntry()
711712
"help",
712713
false,
713714
sentenceBuilder.HelpCommandText(AddDashesToOption),
714-
string.Empty);
715+
string.Empty,
716+
false);
715717
}
716718

717719
private OptionSpecification MakeVersionEntry()
@@ -721,7 +723,8 @@ private OptionSpecification MakeVersionEntry()
721723
"version",
722724
false,
723725
sentenceBuilder.VersionCommandText(AddDashesToOption),
724-
string.Empty);
726+
string.Empty,
727+
false);
725728
}
726729

727730
private HelpText AddPreOptionsLine(string value, int maximumLength)
@@ -733,6 +736,9 @@ private HelpText AddPreOptionsLine(string value, int maximumLength)
733736

734737
private HelpText AddOption(string requiredWord, int maxLength, Specification specification, int widthOfHelpText)
735738
{
739+
if (specification.Hidden)
740+
return this;
741+
736742
optionsHelp.Append(" ");
737743
var name = new StringBuilder(maxLength)
738744
.BimapIf(
@@ -841,13 +847,15 @@ private int GetMaxLength(IEnumerable<Specification> specifications)
841847
{
842848
return specifications.Aggregate(0,
843849
(length, spec) =>
844-
{
845-
var specLength = spec.Tag == SpecificationType.Option
850+
{
851+
if (spec.Hidden)
852+
return length;
853+
var specLength = spec.Tag == SpecificationType.Option
846854
? GetMaxOptionLength((OptionSpecification)spec)
847855
: GetMaxValueLength((ValueSpecification)spec);
848856

849-
return Math.Max(length, specLength);
850-
});
857+
return Math.Max(length, specLength);
858+
});
851859
}
852860

853861

src/CommandLine/VerbAttribute.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ public string Name
3434
get { return name; }
3535
}
3636

37+
/// <summary>
38+
/// Gets or sets a value indicating whether a command line verb is visible in the help text.
39+
/// </summary>
40+
public bool Hidden
41+
{
42+
get;
43+
set;
44+
}
45+
3746
/// <summary>
3847
/// Gets or sets a short description of this command line option. Usually a sentence summary.
3948
/// </summary>

tests/CommandLine.Tests/Fakes/Help_Fakes.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System.Collections.Generic;
44
using CommandLine.Text;
5+
using Microsoft.FSharp.Collections;
56

67
namespace CommandLine.Tests.Fakes
78
{
@@ -12,6 +13,9 @@ class Simple_Options_Without_HelpText
1213

1314
[Option("input-file")]
1415
public string FileName { get; set; }
16+
17+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
18+
public string SecertOption { get; set; }
1519
}
1620

1721
class Simple_Options_With_HelpText_Set
@@ -21,6 +25,9 @@ class Simple_Options_With_HelpText_Set
2125

2226
[Option('i', "input-file", Required = true, HelpText = "Specify input file to be processed.")]
2327
public string FileName { get; set; }
28+
29+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
30+
public string SecertOption { get; set; }
2431
}
2532

2633
class Simple_Options_With_HelpText_Set_To_Long_Description
@@ -30,6 +37,9 @@ class Simple_Options_With_HelpText_Set_To_Long_Description
3037

3138
[Option("input-file", HelpText = "This is a very long description of the Input File argument that gets passed in. It should be passed in as a string.")]
3239
public string FileName { get; set; }
40+
41+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
42+
public string SecertOption { get; set; }
3343
}
3444

3545
class Simple_Options_With_HelpText_Set_To_Long_Description_Without_Spaces
@@ -39,6 +49,9 @@ class Simple_Options_With_HelpText_Set_To_Long_Description_Without_Spaces
3949

4050
[Option("input-file", HelpText = "Before 012345678901234567890123456789 After")]
4151
public string FileName { get; set; }
52+
53+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
54+
public string SecertOption { get; set; }
4255
}
4356

4457
class Options_With_Usage_Attribute
@@ -64,6 +77,9 @@ class Options_With_Usage_Attribute
6477
[Value(0, HelpText = "Value.")]
6578
public string Value { get; set; }
6679

80+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
81+
public string SecertOption { get; set; }
82+
6783
[Usage(ApplicationAlias = "mono testapp.exe")]
6884
public static IEnumerable<Example> Examples
6985
{
@@ -78,6 +94,16 @@ public static IEnumerable<Example> Examples
7894
}
7995
}
8096

97+
[Verb("secert", Hidden = true, HelpText = "This is a secert hidden verb that should never be visible to the user via help text.")]
98+
public class Secert_Verb
99+
{
100+
[Option('f', "force", SetName = "mode-f", HelpText = "Allow adding otherwise ignored files.")]
101+
public bool Force { get; set; }
102+
103+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
104+
public string SecertOption { get; set; }
105+
}
106+
81107
[Verb("add", HelpText = "Add file contents to the index.")]
82108
public class Add_Verb_With_Usage_Attribute
83109
{
@@ -92,6 +118,9 @@ public class Add_Verb_With_Usage_Attribute
92118
[Value(0)]
93119
public string FileName { get; set; }
94120

121+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
122+
public string SecertOption { get; set; }
123+
95124
[Usage(ApplicationAlias = "git")]
96125
public static IEnumerable<Example> Examples
97126
{
@@ -112,6 +141,9 @@ public class Commit_Verb_With_Usage_Attribute
112141
[Option("amend", HelpText = "Used to amend the tip of the current branch.")]
113142
public bool Amend { get; set; }
114143

144+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
145+
public string SecertOption { get; set; }
146+
115147
[Usage(ApplicationAlias = "git")]
116148
public static IEnumerable<Example> Examples
117149
{
@@ -133,6 +165,9 @@ public class Clone_Verb_With_Usage_Attribute
133165
HelpText = "Suppress summary message.")]
134166
public bool Quiet { get; set; }
135167

168+
[Option("secert-option", Hidden = true, HelpText = "This is a description for a secert hidden option that should never be visibile to the user via help text.")]
169+
public string SecertOption { get; set; }
170+
136171
[Value(0, MetaName = "URLS",
137172
HelpText = "A list of url(s) to clone.")]
138173
public IEnumerable<string> Urls { get; set; }

0 commit comments

Comments
 (0)