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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

* Fix [#251] by [@mattnischan] - remove API that was marked as obsolete in 2.x releases
* Fix [#294] by [@natemcmaster] - change dependencies on McMaster.Extensions.Hosting.CommandLine to just use Microsoft.Extensions.Hosting.Abstractions
* Fix [#337] by [@natemcmaster] - removed .NET Standard 1.6 target from library

[@mattnischan]: https://github.com/mattnischan
[@natemcmaster]: https://github.com/natemcmaster

[#251]: https://github.com/natemcmaster/CommandLineUtils/issues/251
[#294]: https://github.com/natemcmaster/CommandLineUtils/issues/294
[#337]: https://github.com/natemcmaster/CommandLineUtils/issues/337

## [v2.5.1](https://github.com/natemcmaster/CommandLineUtils/compare/v2.5.0...v2.5.1)

Expand Down
18 changes: 18 additions & 0 deletions docs/v3.0/upgrade-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ uid: 2.x-to-3.0-upgrade
---
# Upgrading to CommandLineUtils 3.0

For more technical details, see [this list of GitHub issues](https://github.com/natemcmaster/CommandLineUtils/issues?q=label%3Abreaking-change+milestone%3A3.0).

## NuGet compatibility with older platforms

3.0 removed support for older .NET platforms, like .NET Standard 1.6, .NET Core 1.x, and UWP 8.0. The library still supports .NET Framework 4.5 and .NET Standard 2.0.

## Symptom

NuGet fails to install your project with an error like

> error NU1202: Package McMaster.Extensions.CommandLineUtils 3.0.0 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package McMaster.Extensions.CommandLineUtils 3.1.0 supports:
> error NU1202: - net45 (.NETFramework,Version=v4.5)
> error NU1202: - netstandard2.0 (.NETStandard,Version=v2.0)

## Resolution

Either keep using CommandLineUtils 2.x, or upgrade your application to something newer. See https://dotnet.microsoft.com/platform/dotnet-standard for a list of .NET platforms compatible with .NET Standard 2.0.

## Upgrading McMaster.Extensions.Hosting.CommandLine

In order to fix [#294], McMaster.Extensions.Hosting.CommandLine 3.0's dependency on Microsoft.Extensions.Hosting
Expand Down
22 changes: 10 additions & 12 deletions src/CommandLineUtils/Abstractions/ValueParserProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal ValueParserProvider()
public CultureInfo ParseCulture { get; set; } = CultureInfo.CurrentCulture;

private static readonly MethodInfo s_GetParserGeneric
= typeof(ValueParserProvider).GetTypeInfo()
= typeof(ValueParserProvider)
.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Single(m => m.Name == nameof(GetParser) && m.IsGenericMethod);

Expand Down Expand Up @@ -95,16 +95,14 @@ public IValueParser GetParser(Type type)
return parser;
}

var typeInfo = type.GetTypeInfo();

if (typeInfo.IsEnum)
if (type.IsEnum)
{
return EnumParser.Create(type);
}

if (ReflectionHelper.IsNullableType(typeInfo, out var wrappedType) && wrappedType != null)
if (ReflectionHelper.IsNullableType(type, out var wrappedType) && wrappedType != null)
{
if (wrappedType.GetTypeInfo().IsEnum)
if (wrappedType.IsEnum)
{
return new NullableValueParser(EnumParser.Create(wrappedType));
}
Expand All @@ -115,20 +113,20 @@ public IValueParser GetParser(Type type)
}
}

if (!typeInfo.IsGenericType)
if (!type.IsGenericType)
{
return null;
}

var typeDef = typeInfo.GetGenericTypeDefinition();
if (typeDef == typeof(ValueTuple<,>) && typeInfo.GenericTypeArguments[0] == typeof(bool))
var typeDef = type.GetGenericTypeDefinition();
if (typeDef == typeof(ValueTuple<,>) && type.GenericTypeArguments[0] == typeof(bool))
{
var innerParser = GetParser(typeInfo.GenericTypeArguments[1]);
var innerParser = GetParser(type.GenericTypeArguments[1]);
if (innerParser == null)
{
return null;
}
var method = typeof(ValueTupleValueParser).GetTypeInfo().GetMethod(nameof(ValueTupleValueParser.Create)).MakeGenericMethod(typeInfo.GenericTypeArguments[1]);
var method = typeof(ValueTupleValueParser).GetMethod(nameof(ValueTupleValueParser.Create)).MakeGenericMethod(type.GenericTypeArguments[1]);
return (IValueParser)method.Invoke(null, new object[] { innerParser });
}

Expand Down Expand Up @@ -197,7 +195,7 @@ private void SafeAdd(IValueParser parser, bool andReplace = false)
}

// strip nullable wrappers since we have a dedicated nullable value parser
targetType = ReflectionHelper.IsNullableType(targetType.GetTypeInfo(), out var wrappedType) && wrappedType != null
targetType = ReflectionHelper.IsNullableType(targetType, out var wrappedType) && wrappedType != null
? wrappedType
: targetType;

Expand Down
5 changes: 0 additions & 5 deletions src/CommandLineUtils/Attributes/AllowedValuesAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public bool IgnoreCase
get
{
return Comparer == StringComparison.CurrentCultureIgnoreCase
#if (NETSTANDARD2_0 || NET45)
|| Comparer == StringComparison.InvariantCultureIgnoreCase
#elif NETSTANDARD1_6
#else
#error Target frameworks should be updated
#endif
|| Comparer == StringComparison.OrdinalIgnoreCase;
}
set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public virtual void Apply(ConventionContext context)
var assembly = Assembly.GetEntryAssembly();
if (assembly == null && context.ModelType != null)
{
assembly = context.ModelType.GetTypeInfo().Assembly;
assembly = context.ModelType.Assembly;
}

if (assembly != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void AddArgument(PropertyInfo prop,

argument.MultipleValues =
prop.PropertyType.IsArray
|| (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(prop.PropertyType)
|| (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)
&& prop.PropertyType != typeof(string));

if (argPropOrder.TryGetValue(argumentAttr.Order, out var otherProp))
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLineUtils/Conventions/AttributeConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Apply(ConventionContext context)
return;
}

foreach (var attr in context.ModelType.GetTypeInfo().GetCustomAttributes().OfType<IConvention>())
foreach (var attr in context.ModelType.GetCustomAttributes().OfType<IConvention>())
{
attr.Apply(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public virtual void Apply(ConventionContext context)
return;
}

var attribute = context.ModelType.GetTypeInfo().GetCustomAttribute<CommandAttribute>();
var attribute = context.ModelType.GetCustomAttribute<CommandAttribute>();
attribute?.Configure(context.Application);

foreach (var subcommand in context.Application.Commands)
Expand All @@ -38,7 +38,7 @@ public virtual void Apply(ConventionContext context)
}
}

foreach (var attr in context.ModelType.GetTypeInfo().GetCustomAttributes<ValidationAttribute>())
foreach (var attr in context.ModelType.GetCustomAttributes<ValidationAttribute>())
{
context.Application.Validators.Add(new AttributeValidator(attr));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ private static readonly MethodInfo s_applyMethod
private void ApplyImpl<TModel>(ConventionContext context)
where TModel : class
{
var constructors = typeof(TModel)
.GetTypeInfo()
.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
var constructors = typeof(TModel).GetConstructors(BindingFlags.Public | BindingFlags.Instance);

var factory = FindMatchedConstructor<TModel>(constructors, context.Application,
constructors.Length == 1);
Expand Down
11 changes: 4 additions & 7 deletions src/CommandLineUtils/Conventions/DefaultHelpOptionConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ public void Apply(ConventionContext context)
return;
}

if (context.ModelType != null)
if (context.ModelType != null
&& (context.ModelType.GetCustomAttribute<SuppressDefaultHelpOptionAttribute>() != null
|| context.ModelType.Assembly.GetCustomAttribute<SuppressDefaultHelpOptionAttribute>() != null))
{
var typeInfo = context.ModelType.GetTypeInfo();
if (typeInfo.GetCustomAttribute<SuppressDefaultHelpOptionAttribute>() != null
|| typeInfo.Assembly.GetCustomAttribute<SuppressDefaultHelpOptionAttribute>() != null)
{
return;
}
return;
}

var help = new CommandOption(_template, CommandOptionType.NoValue)
Expand Down
5 changes: 2 additions & 3 deletions src/CommandLineUtils/Conventions/ExecuteMethodConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ private async Task<int> OnExecute(ConventionContext context, CancellationToken c
{
const BindingFlags binding = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

var typeInfo = context.ModelType.GetTypeInfo();
MethodInfo? method;
MethodInfo? asyncMethod;
try
{
method = typeInfo.GetMethod("OnExecute", binding);
asyncMethod = typeInfo.GetMethod("OnExecuteAsync", binding);
method = context.ModelType.GetMethod("OnExecute", binding);
asyncMethod = context.ModelType.GetMethod("OnExecuteAsync", binding);
}
catch (AmbiguousMatchException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public virtual void Apply(ConventionContext context)
return;
}

var helpOptionAttrOnType = context.ModelType.GetTypeInfo().GetCustomAttribute<HelpOptionAttribute>();
var helpOptionAttrOnType = context.ModelType.GetCustomAttribute<HelpOptionAttribute>();
helpOptionAttrOnType?.Configure(context.Application);

var props = ReflectionHelper.GetProperties(context.ModelType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public virtual void Apply(ConventionContext context)
return;
}

var parentProp = context.ModelType.GetTypeInfo().GetProperty("Parent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var parentProp = context.ModelType.GetProperty("Parent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (parentProp == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public virtual void Apply(ConventionContext context)
return;
}

var typeInfo = context.ModelType.GetTypeInfo();
var prop = typeInfo.GetProperty("RemainingArguments", PropertyBindingFlags);
prop ??= typeInfo.GetProperty("RemainingArgs", PropertyBindingFlags);
var prop = context.ModelType.GetProperty("RemainingArguments", PropertyBindingFlags);
prop ??= context.ModelType.GetProperty("RemainingArgs", PropertyBindingFlags);
if (prop == null)
{
return;
Expand All @@ -43,9 +42,9 @@ public virtual void Apply(ConventionContext context)
return;
}

if (!typeof(IReadOnlyList<string>).GetTypeInfo().IsAssignableFrom(prop.PropertyType))
if (!typeof(IReadOnlyList<string>).IsAssignableFrom(prop.PropertyType))
{
throw new InvalidOperationException(Strings.RemainingArgsPropsIsUnassignable(typeInfo));
throw new InvalidOperationException(Strings.RemainingArgsPropsIsUnassignable(context.ModelType));
}

context.Application.OnParsingComplete(r =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public virtual void Apply(ConventionContext context)
return;
}

var attributes = context.ModelType.GetTypeInfo().GetCustomAttributes<SubcommandAttribute>();
var attributes = context.ModelType.GetCustomAttributes<SubcommandAttribute>();

foreach (var attribute in attributes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public virtual void Apply(ConventionContext context)
return;
}

var subcommandProp = context.ModelType.GetTypeInfo().GetProperty("Subcommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var subcommandProp = context.ModelType.GetProperty("Subcommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (subcommandProp == null)
{
return;
Expand Down
9 changes: 3 additions & 6 deletions src/CommandLineUtils/Conventions/ValidateMethodConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public void Apply(ConventionContext context)

const BindingFlags MethodFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

var method = context.ModelType
.GetTypeInfo()
.GetMethod("OnValidate", MethodFlags);

var method = context.ModelType.GetMethod("OnValidate", MethodFlags);
if (method == null)
{
return;
Expand All @@ -48,11 +45,11 @@ public void Apply(ConventionContext context)
{
var methodParam = methodParams[i];

if (typeof(ValidationContext).GetTypeInfo().IsAssignableFrom(methodParam.ParameterType))
if (typeof(ValidationContext).IsAssignableFrom(methodParam.ParameterType))
{
arguments[i] = ctx;
}
else if (typeof(CommandLineContext).GetTypeInfo().IsAssignableFrom(methodParam.ParameterType))
else if (typeof(CommandLineContext).IsAssignableFrom(methodParam.ParameterType))
{
arguments[i] = context.Application._context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public virtual void Apply(ConventionContext context)

const BindingFlags MethodFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

var method = context.ModelType
.GetTypeInfo()
.GetMethod("OnValidationError", MethodFlags);

var method = context.ModelType.GetMethod("OnValidationError", MethodFlags);
if (method == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public virtual void Apply(ConventionContext context)
return;
}

var versionOptionAttrOnType = context.ModelType.GetTypeInfo().GetCustomAttribute<VersionOptionAttribute>();
var versionOptionAttrOnType = context.ModelType.GetCustomAttribute<VersionOptionAttribute>();
versionOptionAttrOnType?.Configure(context.Application);

var props = ReflectionHelper.GetProperties(context.ModelType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public virtual void Apply(ConventionContext context)
return;
}

var versionOptionFromMember = context.ModelType.GetTypeInfo().GetCustomAttribute<VersionOptionFromMemberAttribute>();
var versionOptionFromMember = context.ModelType.GetCustomAttribute<VersionOptionFromMemberAttribute>();
versionOptionFromMember?.Configure(context.Application, context.ModelType, modelAccessor.GetModel);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ protected virtual string Format(CommandOption option)

private string[] ExtractNamesFromEnum(Type type)
{
if (type == null || !type.GetTypeInfo().IsEnum)
if (type == null || !type.IsEnum)
{
return Util.EmptyArray<string>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLineUtils/IO/Pager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Pager(IConsole console)
#if NET45
// if .NET Framework, assume we're on Windows unless it's running on Mono.
_enabled = Type.GetType("Mono.Runtime") != null;
#elif NETSTANDARD1_6 || NETSTANDARD2_0
#elif NETSTANDARD2_0
_enabled = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !console.IsOutputRedirected;
#else
#error Update target frameworks
Expand Down
5 changes: 2 additions & 3 deletions src/CommandLineUtils/Internal/CollectionParserProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ private CollectionParserProvider()
return new ArrayParser(elementType, elementParser, valueParsers.ParseCulture);
}

var typeInfo = type.GetTypeInfo();
if (typeInfo.IsGenericType)
if (type.IsGenericType)
{
var typeDef = type.GetGenericTypeDefinition();
var elementType = typeInfo.GetGenericArguments().First();
var elementType = type.GetGenericArguments().First();
var elementParser = valueParsers.GetParser(elementType);

if (typeof(IList<>) == typeDef
Expand Down
Loading