Skip to content

Commit

Permalink
merge develop
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Jul 23, 2019
2 parents f8d26bc + 86d7582 commit 9a9d27a
Show file tree
Hide file tree
Showing 17 changed files with 871 additions and 250 deletions.
33 changes: 25 additions & 8 deletions src/CommandLine/BaseAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public abstract class BaseAttribute : Attribute
private int min;
private int max;
private object @default;
private string helpText;
private Infrastructure.LocalizableAttributeProperty helpText;
private string metaValue;
private Type resourceType;

/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.BaseAttribute"/> class.
Expand All @@ -22,8 +23,9 @@ protected internal BaseAttribute()
{
min = -1;
max = -1;
helpText = string.Empty;
helpText = new Infrastructure.LocalizableAttributeProperty(nameof(HelpText));
metaValue = string.Empty;
resourceType = null;
}

/// <summary>
Expand Down Expand Up @@ -90,11 +92,8 @@ public object Default
/// </summary>
public string HelpText
{
get { return helpText; }
set
{
helpText = value ?? throw new ArgumentNullException("value");
}
get => helpText.Value??string.Empty;
set => helpText.Value = value ?? throw new ArgumentNullException("value");
}

/// <summary>
Expand All @@ -105,7 +104,12 @@ public string MetaValue
get { return metaValue; }
set
{
metaValue = value ?? throw new ArgumentNullException("value");
if (value == null)
{
throw new ArgumentNullException("value");
}

metaValue = value;
}
}

Expand All @@ -117,5 +121,18 @@ public bool Hidden
get;
set;
}

/// <summary>
/// Gets or sets the <see cref="System.Type"/> that contains the resources for <see cref="HelpText"/>.
/// </summary>
public Type ResourceType
{
get { return resourceType; }
set
{
resourceType =
helpText.ResourceType = value;
}
}
}
}
58 changes: 58 additions & 0 deletions src/CommandLine/Infrastructure/LocalizableAttributeProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CommandLine.Infrastructure
{
internal class LocalizableAttributeProperty
{
private string _propertyName;
private string _value;
private Type _type;
private PropertyInfo _localizationPropertyInfo;

public LocalizableAttributeProperty(string propertyName)
{
_propertyName = propertyName;
}

public string Value
{
get { return GetLocalizedValue(); }
set
{
_localizationPropertyInfo = null;
_value = value;
}
}

public Type ResourceType
{
set
{
_localizationPropertyInfo = null;
_type = value;
}
}

private string GetLocalizedValue()
{
if (String.IsNullOrEmpty(_value) || _type == null)
return _value;
if (_localizationPropertyInfo == null)
{
// Static class IsAbstract
if (!_type.IsVisible)
throw new ArgumentException("Invalid resource type", _propertyName);
PropertyInfo propertyInfo = _type.GetProperty(_value, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static);
if (propertyInfo == null || !propertyInfo.CanRead || propertyInfo.PropertyType != typeof(string))
throw new ArgumentException("Invalid resource property name", _propertyName);
_localizationPropertyInfo = propertyInfo;
}
return (string)_localizationPropertyInfo.GetValue(null, null);
}
}

}
100 changes: 27 additions & 73 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ ComparableOption ToComparableOption(Specification spec, int index)

private const int BuilderCapacity = 128;
private const int DefaultMaximumLength = 80; // default console width
/// <summary>
/// The number of spaces between an option and its associated help text
/// </summary>
private const int OptionToHelpTextSeparatorWidth = 4;
/// <summary>
/// The width of the option prefix (either "--" or " "
/// </summary>
private const int OptionPrefixWidth = 2;
/// <summary>
/// The total amount of extra space that needs to accounted for when indenting Option help text
/// </summary>
private const int TotalOptionPadding = OptionToHelpTextSeparatorWidth + OptionPrefixWidth;
private readonly StringBuilder preOptionsHelp;
private readonly StringBuilder postOptionsHelp;
private readonly SentenceBuilder sentenceBuilder;
Expand Down Expand Up @@ -671,7 +683,7 @@ public static IEnumerable<string> RenderUsageTextAsLines<T>(ParserResult<T> pars
var styles = example.GetFormatStylesOrDefault();
foreach (var s in styles)
{
var commandLine = new StringBuilder(2.Spaces())
var commandLine = new StringBuilder(OptionPrefixWidth.Spaces())
.Append(appAlias)
.Append(' ')
.Append(Parser.Default.FormatCommandLine(example.Sample,
Expand Down Expand Up @@ -728,37 +740,7 @@ internal static void AddLine(StringBuilder builder, string value, int maximumLen
value = value.TrimEnd();

builder.AppendWhen(builder.Length > 0, Environment.NewLine);
do
{
var wordBuffer = 0;
var words = value.Split(' ');
for (var i = 0; i < words.Length; i++)
{
if (words[i].Length < (maximumLength - wordBuffer))
{
builder.Append(words[i]);
wordBuffer += words[i].Length;
if ((maximumLength - wordBuffer) > 1 && i != words.Length - 1)
{
builder.Append(" ");
wordBuffer++;
}
}
else if (words[i].Length >= maximumLength && wordBuffer == 0)
{
builder.Append(words[i].Substring(0, maximumLength));
wordBuffer = maximumLength;
break;
}
else
break;
}
value = value.Substring(Math.Min(wordBuffer, value.Length));
builder.AppendWhen(value.Length > 0, Environment.NewLine);
}
while (value.Length > maximumLength);

builder.Append(value);
builder.Append(TextWrapper.WrapAndIndentText(value, 0, maximumLength));
}

private IEnumerable<Specification> GetSpecificationsFromType(Type type)
Expand Down Expand Up @@ -822,7 +804,7 @@ private HelpText AddOptionsImpl(

optionsHelp = new StringBuilder(BuilderCapacity);

var remainingSpace = maximumLength - (maxLength + 6);
var remainingSpace = maximumLength - (maxLength + TotalOptionPadding);

if (OptionComparison != null)
{
Expand Down Expand Up @@ -895,7 +877,7 @@ private HelpText AddOption(string requiredWord, int maxLength, Specification spe

optionsHelp
.Append(name.Length < maxLength ? name.ToString().PadRight(maxLength) : name.ToString())
.Append(" ");
.Append(OptionToHelpTextSeparatorWidth.Spaces());

var optionHelpText = specification.HelpText;

Expand All @@ -907,44 +889,13 @@ private HelpText AddOption(string requiredWord, int maxLength, Specification spe

if (specification.Required)
optionHelpText = "{0} ".FormatInvariant(requiredWord) + optionHelpText;

if (!string.IsNullOrEmpty(optionHelpText))
{
do
{
var wordBuffer = 0;
var words = optionHelpText.Split(' ');
for (var i = 0; i < words.Length; i++)
{
if (words[i].Length < (widthOfHelpText - wordBuffer))
{
optionsHelp.Append(words[i]);
wordBuffer += words[i].Length;
if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1)
{
optionsHelp.Append(" ");
wordBuffer++;
}
}
else if (words[i].Length >= widthOfHelpText && wordBuffer == 0)
{
optionsHelp.Append(words[i].Substring(0, widthOfHelpText));
wordBuffer = widthOfHelpText;
break;
}
else
break;
}

optionHelpText = optionHelpText.Substring(Math.Min(wordBuffer, optionHelpText.Length)).Trim();
optionsHelp.AppendWhen(optionHelpText.Length > 0, Environment.NewLine,
new string(' ', maxLength + 6));
}
while (optionHelpText.Length > widthOfHelpText);
}


//note that we need to indent trim the start of the string because it's going to be
//appended to an existing line that is as long as the indent-level
var indented = TextWrapper.WrapAndIndentText(optionHelpText, maxLength+TotalOptionPadding, widthOfHelpText).TrimStart();

optionsHelp
.Append(optionHelpText)
.Append(indented)
.Append(Environment.NewLine)
.AppendWhen(additionalNewLineAfterOption, Environment.NewLine);

Expand Down Expand Up @@ -1030,13 +981,13 @@ private int GetMaxOptionLength(OptionSpecification spec)
{
specLength += spec.LongName.Length;
if (AddDashesToOption)
specLength += 2;
specLength += OptionPrefixWidth;

specLength += metaLength;
}

if (hasShort && hasLong)
specLength += 2; // ", "
specLength += OptionPrefixWidth;

return specLength;
}
Expand Down Expand Up @@ -1083,5 +1034,8 @@ private static string FormatDefaultValue<T>(T value)
? builder.ToString(0, builder.Length - 1)
: string.Empty;
}



}
}
Loading

0 comments on commit 9a9d27a

Please sign in to comment.