Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #862. Properties with required modifier should be required. #863

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion src/CommandLine/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>CommandLine</AssemblyName>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;net40;net45;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net40;net45;net461;net7</TargetFrameworks>
<DefineConstants>$(DefineConstants);CSX_EITHER_INTERNAL;CSX_REM_EITHER_BEYOND_2;CSX_ENUM_INTERNAL;ERRH_INTERNAL;CSX_MAYBE_INTERNAL;CSX_REM_EITHER_FUNC;CSX_REM_CRYPTORAND;ERRH_ADD_MAYBE_METHODS</DefineConstants>
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
30 changes: 22 additions & 8 deletions src/CommandLine/Core/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CommandLine.Infrastructure;
using CSharpx;

Expand Down Expand Up @@ -54,7 +56,7 @@ protected Specification(SpecificationType tag, bool required, Maybe<int> min, Ma
this.hidden = hidden;
}

public SpecificationType Tag
public SpecificationType Tag
{
get { return tag; }
}
Expand Down Expand Up @@ -110,28 +112,40 @@ public bool Hidden
}

public static Specification FromProperty(PropertyInfo property)
{
{
var attrs = property.GetCustomAttributes(true);

#if NET7_0_OR_GREATER
var isRequired = attrs.OfType<RequiredMemberAttribute>().Any();
#else
var isRequired = false;
#endif
Comment on lines +118 to +122

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if NET7_0_OR_GREATER
var isRequired = attrs.OfType<RequiredMemberAttribute>().Any();
#else
var isRequired = false;
#endif
var isRequired = false;
#if NET7_0_OR_GREATER
isRequired = attrs.OfType<RequiredMemberAttribute>().Any();
#endif


var oa = attrs.OfType<OptionAttribute>();
if (oa.Count() == 1)
{
var spec = OptionSpecification.FromAttribute(oa.Single(), property.PropertyType,
ReflectionHelper.GetNamesOfEnum(property.PropertyType));
var optionAttribute = oa.Single();
optionAttribute.Required |= isRequired;
var spec = OptionSpecification.FromAttribute(optionAttribute, property.PropertyType,
ReflectionHelper.GetNamesOfEnum(property.PropertyType));

if (spec.ShortName.Length == 0 && spec.LongName.Length == 0)
{
return spec.WithLongName(property.Name.ToLowerInvariant());
}

return spec;
}

var va = attrs.OfType<ValueAttribute>();
if (va.Count() == 1)
{
return ValueSpecification.FromAttribute(va.Single(), property.PropertyType,
property.PropertyType.GetTypeInfo().IsEnum
? Enum.GetNames(property.PropertyType)
: Enumerable.Empty<string>());
var valueAttribute = va.Single();
valueAttribute.Required |= isRequired;
return ValueSpecification.FromAttribute(valueAttribute, property.PropertyType,
property.PropertyType.GetTypeInfo().IsEnum
? Enum.GetNames(property.PropertyType)
: Enumerable.Empty<string>());
}

throw new InvalidOperationException();
Expand Down
2 changes: 0 additions & 2 deletions src/CommandLine/Core/TokenPartitioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ Tuple<IEnumerable<KeyValuePair<string, IEnumerable<string>>>, IEnumerable<string
IEnumerable<Token> tokens,
Func<string, Maybe<TypeDescriptor>> typeLookup)
{
IEqualityComparer<Token> tokenComparer = ReferenceEqualityComparer.Default;

var tokenList = tokens.Memoize();
var partitioned = PartitionTokensByType(tokenList, typeLookup);
var switches = partitioned.Item1;
Expand Down
2 changes: 1 addition & 1 deletion tests/CommandLine.Tests/CommandLine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net461;net7;netcoreapp3.1</TargetFrameworks>
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand Down
29 changes: 29 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue862Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using FluentAssertions;
using Xunit;

namespace CommandLine.Tests.Unit
{
#if NET7_0_OR_GREATER
public class Issue862Tests
{

[Fact]
public void Properties_with_required_modifier_are_required()
{
var arguments = Array.Empty<string>();
var result = new Parser().ParseArguments<Options>(arguments);

result.Errors.Should().ContainSingle(e => e.Tag == ErrorType.MissingRequiredOptionError);
result.Tag.Should().Be(ParserResultType.NotParsed);
}

private class Options
{
[Option("cols")]
public required int Cols { get; set; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you have a required modifier and [Option(Required = false)]? I would say, it should throw because of conflicting settings. Please add test for such case

Copy link
Author

@Orace Orace Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a trivia 🤔
So I moved the discussion on #862 thread, here

}
}

#endif
}