Skip to content

Commit

Permalink
Portable version of the CommandLine library
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed May 21, 2016
1 parent a4200dc commit f211c33
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
63 changes: 26 additions & 37 deletions lib/commandline/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
using System.IO;
using System.Reflection;
using System;
using System.Runtime.Serialization;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;
using System.Globalization;
using System.Linq;
#endregion

namespace CommandLine
Expand Down Expand Up @@ -308,7 +309,7 @@ public ValueListAttribute(Type concreteType)
if (concreteType == null)
throw new ArgumentNullException("concreteType");

if (!typeof(IList<string>).IsAssignableFrom(concreteType))
if (!typeof(IList<string>).GetTypeInfo().IsAssignableFrom(concreteType.GetTypeInfo()))
throw new CommandLineParserException("The types are incompatible.");

_concreteType = concreteType;
Expand Down Expand Up @@ -401,7 +402,7 @@ protected void DefineOptionThatViolatesFormat(OptionInfo option)

public static ArgumentParser Create(string argument, bool ignoreUnknownArguments)
{
if (argument.Equals("-", StringComparison.InvariantCulture))
if (argument.Equals("-", StringComparison.Ordinal))
return null;

if (argument[0] == '-' && argument[1] == '-')
Expand All @@ -416,7 +417,7 @@ public static ArgumentParser Create(string argument, bool ignoreUnknownArguments
public static bool IsInputValue(string argument)
{
if (argument.Length > 0)
return argument.Equals("-", StringComparison.InvariantCulture) || argument[0] != '-';
return argument.Equals("-", StringComparison.Ordinal) || argument[0] != '-';

return true;
}
Expand Down Expand Up @@ -793,7 +794,6 @@ public override ParserState Parse(IArgumentEnumerator argumentEnumerator, Option
}
}

[DebuggerDisplay("ShortName = {ShortName}, LongName = {LongName}")]
internal sealed class OptionInfo
{
private readonly OptionAttribute _attribute;
Expand Down Expand Up @@ -881,7 +881,7 @@ public bool SetValue(IList<string> values, object options)
lock (_setValueLock)
{
//array.SetValue(Convert.ChangeType(values[i], elementType, CultureInfo.InvariantCulture), i);
array.SetValue(Convert.ChangeType(values[i], elementType, Thread.CurrentThread.CurrentCulture), i);
array.SetValue(Convert.ChangeType(values[i], elementType, CultureInfo.CurrentCulture), i);
_property.SetValue(options, array, null);
}
}
Expand All @@ -898,7 +898,7 @@ private bool SetValueScalar(string value, object options)
{
try
{
if (_property.PropertyType.IsEnum)
if (_property.PropertyType.GetTypeInfo().IsEnum)
{
lock (_setValueLock)
{
Expand All @@ -910,7 +910,7 @@ private bool SetValueScalar(string value, object options)
lock (_setValueLock)
{
//_property.SetValue(options, Convert.ChangeType(value, _property.PropertyType, CultureInfo.InvariantCulture), null);
_property.SetValue(options, Convert.ChangeType(value, _property.PropertyType, Thread.CurrentThread.CurrentCulture), null);
_property.SetValue(options, Convert.ChangeType(value, _property.PropertyType, CultureInfo.CurrentCulture), null);
}
}
}
Expand All @@ -932,14 +932,12 @@ private bool SetValueScalar(string value, object options)

private bool SetNullableValue(string value, object options)
{
var nc = new NullableConverter(_property.PropertyType);

try
{
lock (_setValueLock)
{
//_property.SetValue(options, nc.ConvertFromString(null, CultureInfo.InvariantCulture, value), null);
_property.SetValue(options, nc.ConvertFromString(null, Thread.CurrentThread.CurrentCulture, value), null);
_property.SetValue(options, Convert.ChangeType(value, Nullable.GetUnderlyingType(_property.PropertyType)));
}
}
// the FormatException (thrown by ConvertFromString) is thrown as Exception.InnerException,
Expand Down Expand Up @@ -1507,7 +1505,6 @@ internal PostParsingState InternalLastPostParsingState
/// <summary>
/// This exception is thrown when a generic parsing error occurs.
/// </summary>
[Serializable]
public sealed class CommandLineParserException : Exception
{
internal CommandLineParserException()
Expand All @@ -1523,11 +1520,6 @@ internal CommandLineParserException(string message, Exception innerException)
: base(message, innerException)
{
}

internal CommandLineParserException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}

/// <summary>
Expand Down Expand Up @@ -1860,18 +1852,18 @@ public static IList<Pair<PropertyInfo, TAttribute>> RetrievePropertyList<TAttrib
IList<Pair<PropertyInfo, TAttribute>> list = new List<Pair<PropertyInfo, TAttribute>>();
if (target != null)
{
var propertiesInfo = target.GetType().GetProperties();
var propertiesInfo = target.GetType().GetTypeInfo().DeclaredProperties;

foreach (var property in propertiesInfo)
{
if (property != null && (property.CanRead && property.CanWrite))
{
var setMethod = property.GetSetMethod();
var setMethod = property.SetMethod;
if (setMethod != null && !setMethod.IsStatic)
{
var attribute = Attribute.GetCustomAttribute(property, typeof(TAttribute), false);
var attribute = property.GetCustomAttributes().OfType<TAttribute>().FirstOrDefault();
if (attribute != null)
list.Add(new Pair<PropertyInfo, TAttribute>(property, (TAttribute)attribute));
list.Add(new Pair<PropertyInfo, TAttribute>(property, attribute));
}
}
}
Expand All @@ -1883,16 +1875,15 @@ public static IList<Pair<PropertyInfo, TAttribute>> RetrievePropertyList<TAttrib
public static Pair<MethodInfo, TAttribute> RetrieveMethod<TAttribute>(object target)
where TAttribute : Attribute
{
var info = target.GetType().GetMethods();
var info = target.GetType().GetTypeInfo().DeclaredMethods;

foreach (MethodInfo method in info)
{
if (!method.IsStatic)
{
Attribute attribute =
Attribute.GetCustomAttribute(method, typeof(TAttribute), false);
TAttribute attribute = method.GetCustomAttributes().OfType<TAttribute>().FirstOrDefault();
if (attribute != null)
return new Pair<MethodInfo, TAttribute>(method, (TAttribute)attribute);
return new Pair<MethodInfo, TAttribute>(method, attribute);
}
}

Expand All @@ -1902,16 +1893,15 @@ public static Pair<MethodInfo, TAttribute> RetrieveMethod<TAttribute>(object tar
public static TAttribute RetrieveMethodAttributeOnly<TAttribute>(object target)
where TAttribute : Attribute
{
var info = target.GetType().GetMethods();
var info = target.GetType().GetTypeInfo().DeclaredMethods;

foreach (MethodInfo method in info)
{
if (!method.IsStatic)
{
Attribute attribute =
Attribute.GetCustomAttribute(method, typeof(TAttribute), false);
TAttribute attribute = method.GetCustomAttributes().OfType<TAttribute>().FirstOrDefault();
if (attribute != null)
return (TAttribute)attribute;
return attribute;
}
}

Expand All @@ -1922,18 +1912,18 @@ public static IList<TAttribute> RetrievePropertyAttributeList<TAttribute>(object
where TAttribute : Attribute
{
IList<TAttribute> list = new List<TAttribute>();
var info = target.GetType().GetProperties();
var info = target.GetType().GetTypeInfo().DeclaredProperties;

foreach (var property in info)
{
if (property != null && (property.CanRead && property.CanWrite))
{
var setMethod = property.GetSetMethod();
var setMethod = property.SetMethod;
if (setMethod != null && !setMethod.IsStatic)
{
var attribute = Attribute.GetCustomAttribute(property, typeof(TAttribute), false);
var attribute = property.GetCustomAttributes().OfType<TAttribute>().FirstOrDefault();
if (attribute != null)
list.Add((TAttribute)attribute);
list.Add(attribute);
}
}
}
Expand All @@ -1944,14 +1934,13 @@ public static IList<TAttribute> RetrievePropertyAttributeList<TAttribute>(object
public static TAttribute GetAttribute<TAttribute>()
where TAttribute : Attribute
{
object[] a = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(TAttribute), false);
if (a == null || a.Length <= 0) return null;
return (TAttribute)a[0];
var assembly = typeof(ReflectionUtil).GetTypeInfo().Assembly;
return assembly.GetCustomAttributes().OfType<TAttribute>().FirstOrDefault();
}

public static bool IsNullableType(Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
return type.GetTypeInfo().IsGenericType && type.GetTypeInfo().GetGenericTypeDefinition() == typeof(Nullable<>);
}
}

Expand Down
10 changes: 8 additions & 2 deletions lib/commandline/readme.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
The Command Line Parser Library is hosted at
https://github.com/gsscoder/commandline

We build the source files into ZoneInfoCompiler, to avoid requiring a
We build the source files into NodaTime.TzdbCompiler, to avoid requiring a
strongly-named copy of the assembler. Please see the LICENSE file
for further information. We don't anticipate any need to keep this
library particularly up-to-date, and it's not currently included in the
binaries distributed with Noda Time.
binaries distributed with Noda Time.

Modification
------------

This copy of the library has been modified to be compatible with
.NET Core.

0 comments on commit f211c33

Please sign in to comment.