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
9 changes: 9 additions & 0 deletions src/CommandLine/BaseAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,14 @@ public string MetaValue
metaValue = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether a command line option is visible in the help text.
/// </summary>
public bool Hidden
{
get;
set;
}
}
}
11 changes: 6 additions & 5 deletions src/CommandLine/Core/OptionSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ sealed class OptionSpecification : Specification

public OptionSpecification(string shortName, string longName, bool required, string setName, Maybe<int> min, Maybe<int> max,
char separator, Maybe<object> defaultValue, string helpText, string metaValue, IEnumerable<string> enumValues,
Type conversionType, TargetType targetType)
: base(SpecificationType.Option, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType)
Type conversionType, TargetType targetType, bool hidden = false)
: base(SpecificationType.Option, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType, hidden)
{
this.shortName = shortName;
this.longName = longName;
Expand All @@ -40,13 +40,14 @@ public static OptionSpecification FromAttribute(OptionAttribute attribute, Type
attribute.MetaValue,
enumValues,
conversionType,
conversionType.ToTargetType());
conversionType.ToTargetType(),
attribute.Hidden);
}

public static OptionSpecification NewSwitch(string shortName, string longName, bool required, string helpText, string metaValue)
public static OptionSpecification NewSwitch(string shortName, string longName, bool required, string helpText, string metaValue, bool hidden = false)
{
return new OptionSpecification(shortName, longName, required, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(),
'\0', Maybe.Nothing<object>(), helpText, metaValue, Enumerable.Empty<string>(), typeof(bool), TargetType.Switch);
'\0', Maybe.Nothing<object>(), helpText, metaValue, Enumerable.Empty<string>(), typeof(bool), TargetType.Switch, hidden);
}

public string ShortName
Expand Down
9 changes: 8 additions & 1 deletion src/CommandLine/Core/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract class Specification
{
private readonly SpecificationType tag;
private readonly bool required;
private readonly bool hidden;
private readonly Maybe<int> min;
private readonly Maybe<int> max;
private readonly Maybe<object> defaultValue;
Expand All @@ -37,7 +38,7 @@ abstract class Specification

protected Specification(SpecificationType tag, bool required, Maybe<int> min, Maybe<int> max,
Maybe<object> defaultValue, string helpText, string metaValue, IEnumerable<string> enumValues,
Type conversionType, TargetType targetType)
Type conversionType, TargetType targetType, bool hidden = false)
{
this.tag = tag;
this.required = required;
Expand All @@ -49,6 +50,7 @@ protected Specification(SpecificationType tag, bool required, Maybe<int> min, Ma
this.helpText = helpText;
this.metaValue = metaValue;
this.enumValues = enumValues;
this.hidden = hidden;
}

public SpecificationType Tag
Expand Down Expand Up @@ -101,6 +103,11 @@ public TargetType TargetType
get { return targetType; }
}

public bool Hidden
{
get { return hidden; }
}

public static Specification FromProperty(PropertyInfo property)
{
var attrs = property.GetCustomAttributes(true);
Expand Down
3 changes: 2 additions & 1 deletion src/CommandLine/Core/SpecificationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public static OptionSpecification WithLongName(this OptionSpecification specific
specification.MetaValue,
specification.EnumValues,
specification.ConversionType,
specification.TargetType);
specification.TargetType,
specification.Hidden);
}

public static string UniqueName(this OptionSpecification specification)
Expand Down
7 changes: 4 additions & 3 deletions src/CommandLine/Core/ValueSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ sealed class ValueSpecification : Specification

public ValueSpecification(int index, string metaName, bool required, Maybe<int> min, Maybe<int> max, Maybe<object> defaultValue,
string helpText, string metaValue, IEnumerable<string> enumValues,
Type conversionType, TargetType targetType)
: base(SpecificationType.Value, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType)
Type conversionType, TargetType targetType, bool hidden = false)
: base(SpecificationType.Value, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType, hidden)
{
this.index = index;
this.metaName = metaName;
Expand All @@ -33,7 +33,8 @@ public static ValueSpecification FromAttribute(ValueAttribute attribute, Type co
attribute.MetaValue,
enumValues,
conversionType,
conversionType.ToTargetType());
conversionType.ToTargetType(),
attribute.Hidden);
}

public int Index
Expand Down
12 changes: 10 additions & 2 deletions src/CommandLine/Core/Verb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ sealed class Verb
{
private readonly string name;
private readonly string helpText;
private readonly bool hidden;

public Verb(string name, string helpText)
public Verb(string name, string helpText, bool hidden = false)
{
if (name == null) throw new ArgumentNullException("name");
if (helpText == null) throw new ArgumentNullException("helpText");

this.name = name;
this.helpText = helpText;
this.hidden = hidden;
}

public string Name
Expand All @@ -30,11 +32,17 @@ public string HelpText
get { return helpText; }
}

public bool Hidden
{
get { return hidden; }
}

public static Verb FromAttribute(VerbAttribute attribute)
{
return new Verb(
attribute.Name,
attribute.HelpText
attribute.HelpText,
attribute.Hidden
);
}

Expand Down
22 changes: 15 additions & 7 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ private IEnumerable<Specification> AdaptVerbsToSpecifications(IEnumerable<Type>
verbTuple.Item1.Name,
false,
verbTuple.Item1.HelpText,
string.Empty)).Concat(new[] { MakeHelpEntry(), MakeVersionEntry() });
string.Empty,
verbTuple.Item1.Hidden)).Concat(new[] { MakeHelpEntry(), MakeVersionEntry() });
}

private HelpText AddOptionsImpl(
Expand Down Expand Up @@ -711,7 +712,8 @@ private OptionSpecification MakeHelpEntry()
"help",
false,
sentenceBuilder.HelpCommandText(AddDashesToOption),
string.Empty);
string.Empty,
false);
}

private OptionSpecification MakeVersionEntry()
Expand All @@ -721,7 +723,8 @@ private OptionSpecification MakeVersionEntry()
"version",
false,
sentenceBuilder.VersionCommandText(AddDashesToOption),
string.Empty);
string.Empty,
false);
}

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

private HelpText AddOption(string requiredWord, int maxLength, Specification specification, int widthOfHelpText)
{
if (specification.Hidden)
return this;

optionsHelp.Append(" ");
var name = new StringBuilder(maxLength)
.BimapIf(
Expand Down Expand Up @@ -841,13 +847,15 @@ private int GetMaxLength(IEnumerable<Specification> specifications)
{
return specifications.Aggregate(0,
(length, spec) =>
{
var specLength = spec.Tag == SpecificationType.Option
{
if (spec.Hidden)
return length;
var specLength = spec.Tag == SpecificationType.Option
? GetMaxOptionLength((OptionSpecification)spec)
: GetMaxValueLength((ValueSpecification)spec);

return Math.Max(length, specLength);
});
return Math.Max(length, specLength);
});
}


Expand Down
9 changes: 9 additions & 0 deletions src/CommandLine/VerbAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public string Name
get { return name; }
}

/// <summary>
/// Gets or sets a value indicating whether a command line verb is visible in the help text.
/// </summary>
public bool Hidden
{
get;
set;
}

/// <summary>
/// Gets or sets a short description of this command line option. Usually a sentence summary.
/// </summary>
Expand Down
35 changes: 35 additions & 0 deletions tests/CommandLine.Tests/Fakes/Help_Fakes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using CommandLine.Text;
using Microsoft.FSharp.Collections;

namespace CommandLine.Tests.Fakes
{
Expand All @@ -12,6 +13,9 @@ class Simple_Options_Without_HelpText

[Option("input-file")]
public string FileName { get; set; }

[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.")]
public string SecertOption { get; set; }
}

class Simple_Options_With_HelpText_Set
Expand All @@ -21,6 +25,9 @@ class Simple_Options_With_HelpText_Set

[Option('i', "input-file", Required = true, HelpText = "Specify input file to be processed.")]
public string FileName { get; set; }

[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.")]
public string SecertOption { get; set; }
}

class Simple_Options_With_HelpText_Set_To_Long_Description
Expand All @@ -30,6 +37,9 @@ class Simple_Options_With_HelpText_Set_To_Long_Description

[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.")]
public string FileName { get; set; }

[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.")]
public string SecertOption { get; set; }
}

class Simple_Options_With_HelpText_Set_To_Long_Description_Without_Spaces
Expand All @@ -39,6 +49,9 @@ class Simple_Options_With_HelpText_Set_To_Long_Description_Without_Spaces

[Option("input-file", HelpText = "Before 012345678901234567890123456789 After")]
public string FileName { get; set; }

[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.")]
public string SecertOption { get; set; }
}

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

[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.")]
public string SecertOption { get; set; }

[Usage(ApplicationAlias = "mono testapp.exe")]
public static IEnumerable<Example> Examples
{
Expand All @@ -78,6 +94,16 @@ public static IEnumerable<Example> Examples
}
}

[Verb("secert", Hidden = true, HelpText = "This is a secert hidden verb that should never be visible to the user via help text.")]
public class Secert_Verb
{
[Option('f', "force", SetName = "mode-f", HelpText = "Allow adding otherwise ignored files.")]
public bool Force { get; set; }

[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.")]
public string SecertOption { get; set; }
}

[Verb("add", HelpText = "Add file contents to the index.")]
public class Add_Verb_With_Usage_Attribute
{
Expand All @@ -92,6 +118,9 @@ public class Add_Verb_With_Usage_Attribute
[Value(0)]
public string FileName { get; set; }

[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.")]
public string SecertOption { get; set; }

[Usage(ApplicationAlias = "git")]
public static IEnumerable<Example> Examples
{
Expand All @@ -112,6 +141,9 @@ public class Commit_Verb_With_Usage_Attribute
[Option("amend", HelpText = "Used to amend the tip of the current branch.")]
public bool Amend { get; set; }

[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.")]
public string SecertOption { get; set; }

[Usage(ApplicationAlias = "git")]
public static IEnumerable<Example> Examples
{
Expand All @@ -133,6 +165,9 @@ public class Clone_Verb_With_Usage_Attribute
HelpText = "Suppress summary message.")]
public bool Quiet { get; set; }

[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.")]
public string SecertOption { get; set; }

[Value(0, MetaName = "URLS",
HelpText = "A list of url(s) to clone.")]
public IEnumerable<string> Urls { get; set; }
Expand Down
Loading