Skip to content

Commit

Permalink
bugfix: added check on the model type before it proceeds accessing th…
Browse files Browse the repository at this point in the history
…e property (#519)

Addresses #518.
  • Loading branch information
ernstc authored Nov 19, 2022
1 parent f5afce9 commit 809f8f2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/CommandLineUtils/Conventions/ArgumentAttributeConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,30 @@ private void AddArgument(PropertyInfo prop,
if (r.SelectedCommand is IModelAccessor cmd)
{
if (argument.Values.Count == 0)
var model = cmd.GetModel();
if (prop.DeclaringType.IsAssignableFrom(model.GetType()))
{
if (!ReflectionHelper.IsSpecialValueTupleType(prop.PropertyType, out _))
if (argument.Values.Count == 0)
{
var value = getter.Invoke(cmd.GetModel());
if (value != null)
if (!ReflectionHelper.IsSpecialValueTupleType(prop.PropertyType, out _))
{
argument.TryParse(value.ToString());
argument.DefaultValue = value.ToString();
var value = getter.Invoke(model);
if (value != null)
{
argument.TryParse(value.ToString());
argument.DefaultValue = value.ToString();
}
}
}
}
else
{
setter.Invoke(
cmd.GetModel(),
parser.Parse(
argument.Name,
argument.Value,
convention.Application.ValueParsers.ParseCulture));
else
{
setter.Invoke(
model,
parser.Parse(
argument.Name,
argument.Value,
convention.Application.ValueParsers.ParseCulture));
}
}
}
});
Expand Down
43 changes: 43 additions & 0 deletions test/CommandLineUtils.Tests/ArgumentAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -94,5 +95,47 @@ public void KeepsDefaultValues()
Assert.Equal("a", app3.Model.Arg1);
Assert.Equal(new[] { "b", "c" }, app3.Model.Arg2);
}

[Subcommand(typeof(ACommand))]
public class Program
{
}

[Command("a")]
[Subcommand(typeof(BCommand))]
public class ACommand
{
[Argument(0)]
public string? Arg1 { get; set; }
}

[Command("b")]
public class BCommand
{
[Argument(0)]
public string? Arg1 { get; set; }
}

[Fact]
public void SameArgumentInSubcommandsCallingACommand()
{
var app1 = new CommandLineApplication<Program>();
app1.Conventions.UseDefaultConventions();
var result = app1.Parse("a", "any-value");
var command = result.SelectedCommand as CommandLineApplication<ACommand>;
Assert.NotNull(command);
Assert.Equal("any-value", command.Model.Arg1);
}

[Fact]
public void SameArgumentInSubcommandsCallingBCommand()
{
var app1 = new CommandLineApplication<Program>();
app1.Conventions.UseDefaultConventions();
var result = app1.Parse("a", "b", "any-value");
var command = result.SelectedCommand as CommandLineApplication<BCommand>;
Assert.NotNull(command);
Assert.Equal("any-value", command.Model.Arg1);
}
}
}

0 comments on commit 809f8f2

Please sign in to comment.